Thread: return type of 'main' is not `int'

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    30

    return type of 'main' is not `int'

    The compiler keeps returning this message:

    Code:
    return type of 'main' is not `int'
    and stops compiling.
    But I should be able to use void type with main shouldn't I?

    Code:
    void main()
    {
    	double pi,x,y;
    
    	pi=atan(1.0)*4.0;
    	printf("acos(0.5)  %lf\n",acos(0.5));
    	printf("asin(0.5)  %lf\n",asin(0.5));
    	printf("atan2(-2.0,1.0)  %lf\n",atan2(-2.0,1.0));
    	printf("abs(-7)  %d\n",abs(-7));
    	printf("ceil(-3.3)  %lf\n",ceil(-3.3));
    	printf("exp(0.5)  %lf\n",exp(0.5));
    	printf("fabs(-3.3)  %lf\n",fabs(-3.3));
    	printf("floor(-3.3)  %lf\n",floor(-3.3));
    	printf("log(0.5)  %lf\n",log(0.5));
    	printf("log10(0.5)  %lf\n",log10(0.5));
    	printf("pow(2.0,3.5)  %lf\n",pow(2.0,3.5));
    	printf("%d  %d\n",RAND_MAX,rand());
    	printf("sqrt(0.5)  %lf\n",sqrt(0.5));
    	printf("%e\n",HUGE_VAL);
    }

  2. #2

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    On anything but freestanding implementations, main is required to return an int. Freestanding implementations are typically embedded systems and similar that never return from the main loop, but you are not using such a system and should thus return an int.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > But I should be able to use void type with main shouldn't I?
    Nope.

    What's next in your broken book - gets() ? fflush(stdin) ?
    They're wrong as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Well gets() isn't actually wrong in the way the others are. It's just dangerous and should never be used.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by Salem View Post
    > But I should be able to use void type with main shouldn't I?
    Nope.

    What's next in your broken book - gets() ? fflush(stdin) ?
    They're wrong as well.
    Lol, well, the C99 standard DOES allow for void main().

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Epy
    Lol, well, the C99 standard DOES allow for void main().
    No, it does not. Not in intention at least, since the language that allows for such a loophole in the interpretation is an oversight in the wording of the text of C99. (Of course, I am assuming you are talking about hosted implementations.)

    EDIT:
    Oh, sorry, I am mistaken about the intention, since section 5.1.2.2.3 does make it appear that the intention may have been to allow that semi-colon to stand. Anyway, read void main() is not legal in C++ but is legal in C.
    Last edited by laserlight; 03-01-2010 at 01:59 PM.
    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

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Sorry, but what dictionary definition of 'shall' are you looking at?
    5.1.2.2.1 Program startup
    1 The function called at program startup is named main. The implementation declares no
    prototype for this function. It shall be defined with a return type of int and with no
    parameters:
    Code:
      int main(void) { /* ... */ }
    or with two parameters (referred to here as argc and argv, though any names may be
    used, as they are local to the function in which they are declared):
    Code:
      int main(int argc, char *argv[]) { /* ... */ }
    or equivalent; or in some other implementation-defined manner.
    And if your current implementation doesn't define something, or it re-defines it in a different way to what you're used to, then you're S-o-L (just like you would be relying on any other implementation defined behaviour).

    5.1.2.2.3 digs an even bigger hole with "If the return type is not compatible with int, the
    termination status returned to the host environment is unspecified."
    UNSPECIFIED!?
    Not only are you rolling the dice on what might happen, there is no sure way of knowing what the answer is!
    This is NOT how you write reliable programs.

    The foo compiler documents void main as being the equivalent of return 0;
    The bar compiler accepts void main, but doesn't document that it reformats your HD.
    You compile for foo, and all is wonderful (or so you believe...).
    You then try bar; it compiles - woopeedoo - then the head scratching starts.


    Generally speaking, when we say "this works", it is implied that in the standard it is predicated by "shall". That means the reader can take it with them and be pretty sure that they're going to get good and consistent results wherever they go.



    It's been true since C was invented - if you want it to work, use int main. Every compiler HAS to accept int main.
    If you use anything else, don't go round with sob stories because your latest compiler tells you where to get off.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Feb 2010
    Posts
    7
    This might be due to printf statements, because printf statements return int (i.e. no of characters printed). So, just use "return;" in the end. It should solve the problem.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sunny1778
    This might be due to printf statements, because printf statements return int (i.e. no of characters printed). So, just use "return;" in the end. It should solve the problem.
    Sorry, but the return type of printf has nothing to do with the return type of the main function. You might want to read the other replies in this thread.
    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
    Feb 2010
    Posts
    37
    Just change the return type of main from void to int
    Code:
    #include<stdio.h>
    #include<math.h>
    #include<limits.h>
    #include<stdlib.h>
    int main()
    {
            double pi,x,y;
    
            pi=atan(1.0)*4.0;
            printf("acos(0.5)  %lf\n",acos(0.5));
            printf("asin(0.5)  %lf\n",asin(0.5));
            printf("atan2(-2.0,1.0)  %lf\n",atan2(-2.0,1.0));
            printf("abs(-7)  %d\n",abs(-7));
            printf("ceil(-3.3)  %lf\n",ceil(-3.3));
            printf("exp(0.5)  %lf\n",exp(0.5));
            printf("fabs(-3.3)  %lf\n",fabs(-3.3));
            printf("floor(-3.3)  %lf\n",floor(-3.3));
            printf("log(0.5)  %lf\n",log(0.5));
            printf("log10(0.5)  %lf\n",log10(0.5));
            printf("pow(2.0,3.5)  %lf\n",pow(2.0,3.5));
            printf("%d  %d\n",RAND_MAX,rand());
            printf("sqrt(0.5)  %lf\n",sqrt(0.5));
            printf("%e\n",HUGE_VAL);
    }
    And cc -lm program_name.c

    Now the program gives the following output
    Code:
    acos(0.5)  1.047198
    asin(0.5)  0.523599
    atan2(-2.0,1.0)  -1.107149
    abs(-7)  7
    ceil(-3.3)  -3.000000
    exp(0.5)  1.648721
    fabs(-3.3)  3.300000
    floor(-3.3)  -4.000000
    log(0.5)  -0.693147
    log10(0.5)  -0.301030
    pow(2.0,3.5)  11.313708
    2147483647  1804289383
    sqrt(0.5)  0.707107
    inf
    Last edited by thillai_selvan; 03-02-2010 at 05:30 AM.

  12. #12
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by laserlight View Post
    No, it does not. Not in intention at least, since the language that allows for such a loophole in the interpretation is an oversight in the wording of the text of C99. (Of course, I am assuming you are talking about hosted implementations.)

    EDIT:
    Oh, sorry, I am mistaken about the intention, since section 5.1.2.2.3 does make it appear that the intention may have been to allow that semi-colon to stand. Anyway, read void main() is not legal in C++ but is legal in C.
    Ha, well that works out. It seems I was mistaken in my initial thought though, I was thinking of int main() as opposed to int main(void), although I swear I remember being corrected about the legality of void main() in the numerous times I've pointed out to others that you should use int main().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Conversion of pointers to functions
    By hzmonte in forum C Programming
    Replies: 0
    Last Post: 01-20-2009, 01:56 AM
  2. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  3. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM
  4. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  5. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM