Thread: main( ) without return

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    13

    main( ) without return

    According to the coding below:-

    Code:
    main()
    
    {
    
      printf( "Welcome to C Programming.\n" );
    
    }
    
    Are there any unexpected syntax error will occur since there's no return value to caller function during compilation?

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    This should be the first thing you ever learn about C/C++.

    Code:
    int main(){
    
      return 0;
    }
    What you posted may compile and function ok but it is against the standard.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,396
    Quote Originally Posted by adnilsah017
    Are there any unexpected syntax error will occur since there's no return value to caller function during compilation?
    If I told you what syntax errors to expect, then there would not be an unexpected syntax error

    Anyway, there should not be any syntax errors in that snippet. However, you should:
    • #include <stdio.h> for printf. The problems with not doing this may not appear in this example, but better safe than sorry.
    • Explicitly state the return type of main as int instead of relying on it defaulting to int. This defaulting to int feature is obsolescent.
    • Explicitly return 0; from the end of main, especially if you are not compiling with respect to C99 (in which case it would be optional).

    You may also wish to explicitly state that the parameter list is empty with the void keyword.
    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

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    13
    i have read through book that give me example of the program as above.... but after i compile it have an error and ask me to return value.

    why it happen?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,396
    Quote Originally Posted by adnilsah017
    i have read through book that give me example of the program as above....
    Your book is probably old. If the example really did not #include <stdio.h>, or if the book did not inform you that you should include a certain set of headers with the examples, then your book would also be wrong.

    Quote Originally Posted by adnilsah017
    but after i compile it have an error and ask me to return value.

    why it happen?
    Because your compiler is looking out for you

    Well, I don't know why that happened: I don't recall that a diagnostic is required, and I don't think there's undefined behaviour as long as the return value is not used. I could be wrong, but it does not matter: just fix it.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. return value from main()
    By megastar in forum C Programming
    Replies: 18
    Last Post: 06-28-2007, 07:27 PM
  2. Why does main need to return a value?
    By jimboob in forum C++ Programming
    Replies: 6
    Last Post: 11-06-2004, 10:48 PM
  3. return to the main()
    By Dummies102 in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2002, 01:23 PM
  4. return from main
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 01-11-2002, 01:29 PM
  5. return from main
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 01-11-2002, 10:04 AM