Thread: help me with this guys.what will happen if i put "return(0)" outside main? why?

  1. #1
    Registered User
    Join Date
    Apr 2014
    Posts
    1

    help me with this guys.what will happen if i put "return(0)" outside main? why?

    insert
    Code:
    main()
      {
      }
    return(0);

  2. #2
    Registered User
    Join Date
    Apr 2014
    Posts
    6
    i think you will receive a error, because return MUST TO BE IN A FUNCTION BODY!

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    why don't you try it and find out for yourself?
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  4. #4
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Sort of the same thing that happens when you ask the air a question, except your annoying friend (who has all the cool stuff at his house) the compiler hears your question and answers instead.

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    It will not be an error if you include the following before your main function
    Code:
    #define return(...)

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by c99tutorial View Post
    It will not be an error if you include the following before your main function
    Code:
    #define return(...)
    It might be. The result of #define'ing language keywords is undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    Quote Originally Posted by grumpy View Post
    The result of #define'ing language keywords is undefined behaviour.
    Isn't a macro with parameters technically distinct from the keyword. For example, the following program clearly returns 1.

    Code:
    #define return(...)
    
    int main()
    {
        return 1;
    }
    
    return(0);

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by c99tutorial View Post
    Isn't a macro with parameters technically distinct from the keyword. For example, the following program clearly returns 1.

    Code:
    #define return(...)
    
    int main()
    {
        return 1;
    }
    
    return(0);
    what happens then, if you #define sizeof(...)? The syntax for using the macro or the keyword would be identical. Sounds like a recipe for disaster if you ask me.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by grumpy View Post
    It might be. The result of #define'ing language keywords is undefined behaviour.
    Hmm...I'm not sure about that. It seems that it's semi-illegal in C++: macros - C++ preprocessor #define-ing a keyword. Is it standards conforming? - Stack Overflow.

    In C99/C11, section 6.10.3 seems to be the right place to look, and I did a decent scan but I don't have time for a very thorough reading. However, I couldn't find anything in the standard that forbids it, or says it results in undefined behavior. I did find this Macros - The C Preprocessor. Far from authoritative, but suggestive at least that it's allowed.

    Feel free to point out anything I missed in my perusal of the standard.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I believe it is legal:
    Quote Originally Posted by C99 Clause 6.4.1 Paragraph 2
    The above tokens (case sensitive) are reserved (in translation phases 7 and 8) for use as keywords, and shall not be used otherwise.
    The preprocessor does its work in translation phase 4.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Hmmm ... that's interesting. I could have sworn I remember reading it was undefined behaviour.

    I just looked up in the 1999 C standard (only one handy on my current machine) and the only reference I can find which is relevant is Section J.2, where it says that the behaviour is undefined if any standard header is #include'd while a macro is #define'd as a keyword.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't get "return()" to work.Problem in "if" part
    By coder1 in forum C Programming
    Replies: 13
    Last Post: 09-10-2013, 01:08 AM
  2. " void main() is evil. Use int main(). "
    By Brw_Abhi in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2007, 12:16 PM
  3. Anyone recently bought "OpengGL Game Programming" by the guys at nehe?
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 03-08-2003, 10:49 AM
  4. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  5. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread