Thread: function with no return type

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    18

    function with no return type

    hello all.

    just observe this piece of code

    Code:
    fun()
    {
         printf("Hello world");
    }
    
    int main()
    {
          int c = 5;
    
          printf("c = %d\n",c);
          c=fun();
          printf("c = %d\n",c);
          
          return 0;
    }
    on compilation and execution usgin gcc, i get the answer as
    c = 5
    c = 11 (-----> the no of chracters printed by printf())
    but since there is no return statement in fun(), the second value of c should contain a junk value.

    according to k & r if a function doesnt have a return statement, its return valu is junk value.
    but why does c contain the no of characters printed by printf ???

  2. #2
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Let's look at it on the assembly level. eax is always the register of return value.
    printf() sets eax to the no of characters it printed and eax is not changed anymore. So eax is still the number of characters printed by printf() when fun() is returned.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    18
    wow!!!! thanks.can you please tell me which book should i refer to for learning such concepts.
    and also is the ANSI draft good enough to improve your knowledge of the C language or is it just to learn the standards

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    What makes you think 11 isn't a junk value?

    The fact that it seems consistent doesn't make it any less junk.
    The fact that it seems to be the number of characters printf printed doesn't make it any less junk.

    > but why does c contain the no of characters printed by printf ???
    Because that's what printf() returns, the number of characters it printed (not that anybody ever checks this).
    And so by being the last thing delivering an answer inside fun(), by "proxy" as it were, it also becomes the result of fun(), because that's the only 'answer' going on at the moment.
    But that's only one possible scenario.



    This is why we have warnings enabled to spot dumb stuff
    $ gcc -W -Wall -ansi -pedantic -O2 new.c
    new.c:3: warning: return type defaults to `int'
    new.c: In function `fun':
    new.c:5: warning: control reaches end of non-void function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Best soluation would be to get a decent book on C programming. The detiel series is an excellent one as everything it teaches you is told in exuisit detail. It is also crammed with exercises to do after each chapter too. I use the C++ fifth edition advanced personally, but they do sell a C programming beginners book. Look on amazon or ebay.
    I highly encourage you to check it out.
    Double Helix STL

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > can you please tell me which book should i refer to for learning such concepts.
    Would be a complete waste of your time.

    maxorator gave a plausable explanation of how it works for you AT THE MOMENT.
    Knowing such detail of how it's done now will do you no good at all in the future. You can't use that information in any meaningful way, so there's no point in learning it.

    Enable the compiler warnings, and pay attention to writing correct code which will work everywhere.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C 1337 Meshal's Avatar
    Join Date
    Nov 2006
    Posts
    70
    Salem why you don`t like the man to learn how to do it ?!!
    is it illegal !

    who say it`s useless to learn this stuff .. ?

    Hackers who interest with those kinds of problems and flaws.

    i think learning assembly and how compilers works is the way to learn this things

    i recommanded to read this book : http://download.savannah.nongnu.org/...0-booksize.pdf

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Because I've seen far too many programmers wander in with some crappy code saying "But this used to work with my old compiler".

    The usual reason being they've made some vast assumption about how C works where in fact what they've really done is figured out what their old compiler did (and which their new compiler doesn't).

    Now since C compilers only refer to the C standard (and not each other), figuring out the minutia of your current compiler isn't going to do you any good for your future compiler.

    I'm not going to tell you want to learn and what not to learn.

    But I can sure tell you that 'knowing' that fun() will return what printf() returns in the example poorly written program isn't worth knowing, because the answer is unusable in any other C program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Knowing such detail of how it's done now will do you no good at all in the future. You can't use that information in any meaningful way, so there's no point in learning it.
    Knowing such details comes with learning assembly. You can't only learn the details
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Why are you even talking about assembler - this is C.

    More traps and pitfalls, just in case you think that getting too "chummy" with the compiler by peering at the generated assembler will be useful.
    http://cboard.cprogramming.com/showt...ight=undefined
    http://cboard.cprogramming.com/showt...ight=undefined
    http://cboard.cprogramming.com/showt...ight=undefined
    The board is full of this type of stuff.

    Case in point
    > printf() sets eax to the no of characters it printed and eax is not changed anymore
    My processor is a MIPS, what on earth are you talking about?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Nov 2006
    Location
    Greece
    Posts
    10
    My processor is a MIPS, what on earth are you talking about?
    In that case the return value would be stored in $v0

  12. #12
    Registered User zouyu1983's Avatar
    Join Date
    Nov 2006
    Location
    Fuzhou University, Fujian, China
    Posts
    35
    the declaration of the printf is
    int printf(
    const char *format [,
    argument]...
    );

    it returns the number of characters printed, or a negative value if an error occurs

    you can download the MSDN labrary,which will be helpful to you

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    1) The code under discussion cannot be compiled if you make the warning level higher.
    2) Compiler warnings are good, because they can significantly reduce the time spent on debugging. Let the compiler tell you if it sees something suspicious. Sooner or later one of those bad practices will strike back otherwise.
    3) Is it really so hard to write it properly?

    Code:
    int fun()
    {
         return printf("Hello world\n");
    }
    There's no point in learning to write bad code...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM