Gaurav Kushwaha
Gaurav Kushwaha

Reputation: 93

C question: no warning?

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

Answers (4)

user305912
user305912

Reputation: 91

Your main function return nothing. so modify in void main(). Usually is:

int main() { printf("Hello world"); return 0; }

Upvotes: 1

JoeG
JoeG

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

dan04
dan04

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

R Samuel Klatchko
R Samuel Klatchko

Reputation: 76531

Did you forget to compile with warnings enabled:

gcc -Wall ...

Upvotes: 2

Related Questions