tags:

views:

179

answers:

4
main()
{
  printf("Hello World.");
}

Why does no warning is produced in gcc compiler even though we declare main() with return type 'int'

+1  A: 

Did you forget to compile with warnings enabled:

gcc -Wall ...
R Samuel Klatchko
+12  A: 

Because you're not using the -Wall flag. When you do, you should get:

foo.c:1: warning: return type defaults to ‘int’
foo.c: In function ‘main’:
foo.c:1: warning: implicit declaration of function ‘printf’
foo.c:1: warning: incompatible implicit declaration of built-in function ‘printf’
foo.c:1: warning: control reaches end of non-void function
dan04
i mam not talking about other warnngs .. i just want to know why there is no warning like "return type of main is int" even if i compile using gcc -Wall
Grv
sorry I get it now .. the last line of warning is for main() itself thanxx...
Grv
@Grv: don't forget to accept an answer (click the checkmark) if it answers your question.
Daniel
@Grv: In fact, the *first* line is the warning you're looking for, though the last line also refers to `main()`.
Jefromi
+1  A: 

Your main function return nothing. so modify in void main(). Usually is: int main() { printf("Hello world"); return 0; }

a small comment: I have learned from Gman here at SO that even if it is *int main(){ .. }* return 0; isn't needed. Not sure if it produces warnings or whatnot :)
Default
A: 

No warning is produced because that's legal ANSI C89. Functions without a specified return type are implicitly assumed to return int.

If you want to compile as C89, but be warned about using implicit int, you should pass either -Wimplicit-int as a command line argument (or -Wall, which enables that warning, along with a number of others).

If you want to compile as C99, you should pass -std=c99 and -pedantic-errors, which will cause the compiler to issue an error if you use implicit int.

Joe Gauterin