void

This is a discussion on void within the C Programming forums, part of the General Programming Boards category; hello everyone! i found a strange thing. 1.i inputed the codes followed in " CodeBlocks ",but it returned this " ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    luoyang ,china
    Posts
    17

    Smile void

    hello everyone!
    i found a strange thing.
    1.i inputed the codes followed in "CodeBlocks",but it returned this "error:'main' must return 'int'".why?
    Code:
    #include<stdio.h>
    void main()
    {
        printf("hello world!");
    }
    2.but it run smoothly,when writing in this way(just without "void"):
    Code:
    #include<stdio.h>
     main()
    {
        printf("hello world!");
    }
    3.the two case all can run smoothly in "TC".

    why?

    i find the reason in thispage.

    say thanks to laserlight,tabstop,adak,nkrao@gmail haha
    Last edited by luoyangke; 10-26-2009 at 09:09 AM. Reason: i find the reason

  2. #2
    kotin
    Join Date
    Oct 2009
    Posts
    132
    in wich compiler u getting that error? i mean in "TC" or "GCC" ?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because "TC" doesn't believe in standards. The standard for the C language requires that main return int. (The C standard allows you, but discourages you, to declare a function without a return type and have it default to int.)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by luoyangke
    1.i inputed the codes followed in "CodeBlocks",but it returned this "error:'main' must return 'int'".why?
    Because according to the C standard (or at least its intended interpretation, for a hosted environment like the one you are using, the main function must be defined with a return type of int.

    Quote Originally Posted by luoyangke
    2.but it run smoothly,when writing in this way(just without "void"):
    When the return type is not specified, it defaults to int. However, you should not rely on this. (Unfortunately, I was not able to quickly determine if the feature is obsolescent or simply not recommended).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    luoyang ,china
    Posts
    17

    Smile in codeblocks

    Quote Originally Posted by nkrao123@gmail. View Post
    in wich compiler u getting that error? i mean in "TC" or "GCC" ?
    a thing named "CodeBlocks" thank you!

  6. #6
    Registered User
    Join Date
    Oct 2009
    Location
    luoyang ,china
    Posts
    17

    Unhappy thank you

    Quote Originally Posted by tabstop View Post
    Because "TC" doesn't believe in standards. The standard for the C language requires that main return int. (The C standard allows you, but discourages you, to declare a function without a return type and have it default to int.)
    thank you ! but i still don't understand

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Turbo C assumes all functions without a return type, return an int, even though you did not specify a return at all.

    Conversely, if you did specify a return type, then it will expect you also to specify the return explicitly, at the end of that function. If the function has a return type of void, then it will give you an error if you specify a return type.

    so,
    Code:
    main() { //int return is assumed and handled by the compiler - OK
    
    }
    
    int main() { //int is specified and return is, as well - OK
    
      return 0;
    }
    
    but 
    int main() { //int return type specified, but not present. Causes a compiler error
    
    }

  8. #8
    Registered User
    Join Date
    Oct 2009
    Location
    luoyang ,china
    Posts
    17

    Smile thank you

    Quote Originally Posted by laserlight View Post
    Because according to the C standard (or at least its intended interpretation, for a hosted environment like the one you are using, the main function must be defined with a return type of int.


    When the return type is not specified, it defaults to int. However, you should not rely on this. (Unfortunately, I was not able to quickly determine if the feature is obsolescent or simply not recommended).
    thank you! not just for this answer 谢谢!

  9. #9
    Registered User
    Join Date
    Oct 2009
    Location
    luoyang ,china
    Posts
    17

    Smile thanks

    Quote Originally Posted by Adak View Post
    Turbo C assumes all functions without a return type, return an int, even though you did not specify a return at all.

    Conversely, if you did specify a return type, then it will expect you also to specify the return explicitly, at the end of that function. If the function has a return type of void, then it will give you an error if you specify a return type.

    so,
    Code:
    main() { //int return is assumed and handled by the compiler - OK
    
    }
    
    int main() { //int is specified and return is, as well - OK
    
      return 0;
    }
    
    but 
    int main() { //int return type specified, but not present. Causes a compiler error
    
    }
    thanks for the second time

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,457
    Quote Originally Posted by Adak
    Code:
    int main() { //int return type specified, but not present. Causes a compiler error
    
    }
    However, third version as quoted above is equivalent to the second version according to the 1999 edition of the C standard (which Turbo C presumably does not support).

    Quote Originally Posted by luoyangke
    thank you! not just for this answer
    You're welcome
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. game window rejected painting !
    By black in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2007, 01:10 AM
  4. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

Tags for this Thread


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