What does 'return' return? I'm puzzled by this snippet...

This is a discussion on What does 'return' return? I'm puzzled by this snippet... within the C Programming forums, part of the General Programming Boards category; Hi all... I have the following snippet of C: { return 5 ? printf("Evaluated true") : printf("Evaluated false"); } So ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    Question What does 'return' return? I'm puzzled by this snippet...

    Hi all...

    I have the following snippet of C:

    {
    return 5 ? printf("Evaluated true") : printf("Evaluated false");
    }

    So I'm puzzled how this works - the return obviously must be done to be evaluated - but then this code actually does print either "Evaluated true" or "Evaluated false". So how does further code get run from this after the return statement? Is it because this one conditional line of code is kind of 'atomic'? And that's why we don't just blow off printing the result straight after the return statement?

    Also, 'return 5' evaluates to true, 'return 0' evaluates to false. In fact, I think anything but 0 evaluates to true.

    So can someone tell me what goes on with this line of code, and how it is processed and run through in an application? (Been looking through my ansi_c.pdf but can't find any info on this).

    Thanks a lot!

    - Nex

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Heh, that's some fun code. In that case, it will return the return value of printf("Evaluated true"). It evaluates the function before return the value.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,529
    Yup.

    Code:
    return 5 ? printf("Evaluated true") : printf("Evaluated false");
    is logically equivalent to
    Code:
    if (5)
       return printf("Evaluated true");
    else
       return printf("Evaluated false");
    which (as 5 is non-zero) is effectively the same as
    Code:
       return printf("Evaluated true");
    In other words it will print "Evaluated true", and return 14 (the length of that string) to the caller.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >and return 14 (the length of that string) to the caller.
    Or less if there is an output error.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    Fantastic - cheers for the clarification guys - much appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 11:00 PM
  3. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 12:52 PM
  4. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 01:34 PM
  5. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 02:08 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21