main() question

This is a discussion on main() question within the C Programming forums, part of the General Programming Boards category; Hi, i know that main() returns an int, but, check these out: Code: #include <stdio.h> main() { printf("Hello, world."); return ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    24

    main() question

    Hi,

    i know that main() returns an int,
    but, check these out:

    Code:
    #include <stdio.h>
    
    main()
    {
     printf("Hello, world.");
    
    return 0;
    }
    Code:
    #include <stdio.h>
    
    int main()
    {
     printf("Hello, world.");
    
    return 0;
    }
    These programs both work properly,
    so isnt the 'int' keyword in front of main() useless?
    Because why put an int in front of it if without an int works also fine...

    thank you

    encrypted

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    The latest standard states that you MUST declare the return type. If you don't then your code is not valid C code. It may work on one compiler, but it may not on another. Always explicitly specify "int" as the return type of main.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Polymorphic is right...

    Maybe your code without 'int' works fine in your compile, and don't give you any warning or error.

    Now, I tried to compile under unix (with gcc) a code that contains, void main(), and guess what?

    yonis.cpp:300: `main' must return `int'
    Now, if you want to see, a more specific example, try looking
    here

    Good luck.

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    24
    Yeah, well i knew that void main() would give an error with gcc,

    anyway 'int main()' is necessary because it becomes a standard to specify the return type right

    thats what i wanted to know

    thank you guys

    encrypted
    Last edited by Encrypted; 01-21-2003 at 03:43 PM.

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    It used to be that if you didn't specify a return type it was implied you meant int. It's not like that anymore however.

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    99
    My motto from now on :"If you are to lazy for three little letters then you are to lazy to program." Not the best in the world but it gets the point across with main().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 01:14 PM
  2. main and return question
    By BuzzBuzz in forum C Programming
    Replies: 9
    Last Post: 02-25-2009, 03:38 AM
  3. void main question..
    By transgalactic2 in forum C Programming
    Replies: 22
    Last Post: 11-19-2008, 11:59 AM
  4. C99 and int main()
    By cwr in forum C Programming
    Replies: 8
    Last Post: 09-19-2005, 07:54 AM
  5. int main (), main (), main (void), int main (void) HELP!!!
    By SmokingMonkey in forum C++ Programming
    Replies: 7
    Last Post: 05-31-2003, 10:46 PM

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