3

I have the following code:

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

I compiled it using the following command:

gcc -o myprogram myfile.c

And it compiled without any error even though I did not #include <stdio.h>. So did gcc include this header file automatically?

My gcc version is 4.3.3

1
  • It might not have given an error: but it did give a warning (if you looked). It made assumptions about the function prototype (in the missing header), and linked the library automatically. Commented Jun 14, 2016 at 21:58

2 Answers 2

3

In ANSI C, you can call functions you didn't declare. Such functions are implicitly declared the first time you call them. They are assumed to return int and take arguments according to the default argument promotions. Since you didn't include <stdio.h>, the compiler uses this rule and implicitly declares printf. Note that this is undefined behaviour as functions that take variable argument lists like printf must be declared explicitly. gcc usually warns you if it uses the implicit declaration rule since it's usually not intentionally used.

Sign up to request clarification or add additional context in comments.

12 Comments

gcc did not give me any warning when I used the following command: gcc -o myprogram myfile.c. Also the program worked fine and printed "Hello".
Doubtful about "this is undefined behaviour as functions that take variable argument lists like printf must be declared explicitly" Any citation available?
@Tom compile with -Wall
@chux C11 6.5.2.2/6 (C89 and C99 had similar text). Standard library functions are considered to be defined with a prototype.
@chux ISO 9899 §3.3.2.2 ¶6 is quite clear in this regard: If the expression that denotes the called function has a type that does not include a prototype, the integral promotions are performed on each argument and arguments that have type float are promoted to double. [...] If the function is defined with a type that includes a prototype, and the types of the arguments after promotion are not compatible with the types of the parameters, or if the prototype ends with an ellipsis ( ", ..." ), the behavior is undefined.
|
1

No, gcc did not include any header files you didn't request. #include statements are C preprocessor macros (like #define or #if) that are actually evaluated before the actual C compilation. You can see what your code looks like after all macros are resolved by calling gcc -E myfile.c. As you will see, printf will still not be declared.

If you compile with -Wall, you should get a warning that printf is undeclared. However gcc "guesses" how printf is used (probably from its arguments, but it could also simply know the routine internally). Since it finds a matching symbol name while linking, you don't get an error and your program runs just fine.

BTW, gcc 5.3.0 shows the following warning:

myfile.c: In function 'main':
myfile.c:3:5: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
     printf("Hello\n");
     ^
myfile.c:3:5: warning: incompatible implicit declaration of built-in function 'printf'
myfile.c:3:5: note: include '<stdio.h>' or provide a declaration of 'printf'

2 Comments

Is there a way to prevent gcc from doing these "guesses"?
Or try compile with -Werror-implicit-function-declaration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.