Reputation: 93
main()
{
printf("Hello World.");
}
Why does no warning is produced in gcc compiler even though we declare main() with return type 'int'
Upvotes: 2
Views: 366
Reputation: 91
Your main function return nothing. so modify in void main(). Usually is:
int main() { printf("Hello world"); return 0; }
Upvotes: 1
Reputation: 13182
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.
Upvotes: 0
Reputation: 90995
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
Upvotes: 14
Reputation: 76531
Did you forget to compile with warnings enabled:
gcc -Wall ...
Upvotes: 2