Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hello.c: In function ‘main’: Hello.c:13: warning: return type of ‘main’ is not ‘int’? [duplicate]

Tags:

c

int

Possible Duplicate:
What should main() return in C/C++?

Just started coding C about an hour ago, after a few months of basic java coding, and am encountering a problem compiling the basic hello world program.

Here is my code:

#include < stdio.h>

void main()
{
    printf("\nHello World\n");
}

and this is what i get back when i try to compile:

 Hello.c: In function ‘main’:
 Hello.c:13: warning: return type of ‘main’ is not ‘int’

any help would be much apprecated, thanks!

like image 286
Joe Perkins Avatar asked Jan 31 '13 21:01

Joe Perkins


3 Answers

The standard signatures for main are either

int main(void)

or

int main(int argc, char **argv)

Your compiler is simply enforcing the standard.

Note that an implementation may support void main(), but it must be explicitly documented, otherwise the behavior is undefined. Like dandan78 says, a large number of books and online references get this wrong.

like image 136
John Bode Avatar answered Sep 22 '22 05:09

John Bode


it should be

int main() {}

then you should return 0 if the program is terminating correctly or any other number if there was an error. That's an Unix convention, so scripts can check if the program was terminated correctly or an error occurred.

like image 25
LtWorf Avatar answered Sep 19 '22 05:09

LtWorf


main-function in c has to return an int:

#include < stdio.h>

int main()
{
  printf("\nHello World\n");
  return 0;
}
like image 29
pbhd Avatar answered Sep 21 '22 05:09

pbhd