4

What will happen if I don't include the header files when running a c program? I know that I get warnings, but the programs runs perfectly.

I know that the header files contain function declarations. Therefore when I don't include them, how does the compiler figure it out? Does it check all the header files?

DesirePRG
  • 6,122
  • 15
  • 69
  • 114
  • possible duplicate of [Why don't we get a compile time error even if we don't include stdio.h in a C program?](http://stackoverflow.com/questions/68843/why-dont-we-get-a-compile-time-error-even-if-we-dont-include-stdio-h-in-a-c-pr) – P.P Apr 26 '13 at 16:48

5 Answers5

7

I know that I get warnings, but the programs runs perfectly.

That is an unfortunate legacy of pre-ANSI C: the language did not require function prototypes, so the standard C allows it to this day (usually, a warning can be produced to find functions called without a prototype).

When you call a function with no prototype, C compiler makes assumptions about the function being called:

  • Function's return type is assumed to be int
  • All parameters are assumed to be declared (i.e. no ... vararg stuff)
  • All parameters are assumed to be whatever you pass after default promotions, and so on.

If the function being called with no prototype fits these assumptions, your program will run correctly; otherwise, it's undefined behavior.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • if i called printf function without the header, if the compiler assumes the above mentioned things, how does printf work perfectly? – DesirePRG Apr 26 '13 at 16:55
  • @DesirePRG Most modern compilers have knowledge about the standard C functions, such as `printf`. They would go beyond the regular parameter checking for the functions from the C standard - I/O, memory management, etc. For example, some compilers would warn you about passing a `float` to `printf` when `%d` is in the corresponding position of the format string. However, not all compilers do that, and none of the compilers are required to do that. – Sergey Kalinichenko Apr 26 '13 at 17:00
  • @dasblinkenlight: And at least in the case of gcc, the compiler *doesn't* use that knowledge to generate an implicit conversion to make the code "work" such as converting a `float` to `int` for `"%d"`. It just uses it to generate a warning. `printf("%d\n", 1.25)` will still produce nonsensical results. – Keith Thompson Apr 26 '13 at 17:03
  • @KeithThompson Yes, that's exactly right. I was running up against the 600-character limit, so I didn't go into much detail on that second part. Thanks for the note, though! – Sergey Kalinichenko Apr 26 '13 at 17:15
  • Parameter aren't assumed to be `int`, they're assumed to match what you passed (after argument promotion. So if you call `sqrt(1.0)` without `#include `, the compiler will assume `int sqrt(double)` -- which is still wrong, of course. That's as of C90; C99 dropped the "implicit `int`" rule. – Keith Thompson Apr 26 '13 at 17:24
  • C99 also dropped implicit function declarations. So while you don't necessarily need a prototype, you must have a declaration in scope when using a function (and the call must match the declaration). – Daniel Fischer Apr 26 '13 at 19:54
5

Before the 1989 ANSI C standard, there was no way to declare a function and indicate the types of its parameters. You just had to be very careful to make each call consistent with the called function, with no warning from the compiler if you got it wrong (like passing an int to sqrt()). In the absence of a visible declaration, any function you call was assumed to return int; this was the "implicit int" rule. A lot of standard functions do return int, so you could often get away with omitting a #include.

The 1989 ANSI C standard (which is also, essentially, the 1990 ISO C standard) introduced prototypes, but didn't make them mandatory (and they still aren't). So if you call

int c = getchar();

it would actually work, because getchar() returns an int.

The 1999 ISO C standard dropped the implicit int rule, and made it illegal (actually a constraint violation) to call a function with no visible declaration. So if you call a standard function without the required #include, a C99-conforming compiler must issue a diagnostic (which can be just a warning). Non-prototype function declarations (ones that don't specify the types of the arguments) are still legal, but they're considered obsolescent.

(The 2011 ISO C standard didn't change much in this particular area.)

But there's still plenty of code out there that was written for C90 compilers, and most modern compilers still support the older standard.

So if you call a standard function without the required #include, what will probably happen is that (a) the compiler will warn you about the missing declaration, and (b) it will assume that the function returns int and takes whatever number and type(s) of arguments you actually passed it (also accounting for type promotion, such as short to int and float to double). If the call is correct, and if you compiler is lenient, then your code will probably work -- but you'll have one more thing to worry about if it fails, perhaps for some unrelated reason.

Variadic functions like printf are another matter. Even in C89/C90, calling printf with no visible prototype had undefined behavior. A compiler can use an entirely different calling convention for variadic functions, so printf("hello") and puts("hello") might generate quite different code. But again, for compatibility with old code, most compilers use a compatible calling convention, so for example the first "hello world" program in K&R1 will probably still compile and run.

You can also write your own declarations for standard functions; the compiler doesn't care whether it sees a declaration in a standard header or in your own source file. But there's no point in doing so. Declarations have changed subtly from one version of the standard to the next, and the headers that came with your implementation should be the correct ones.

So what will actually happen if you call a standard function without the corresponding #include?

In a typical working environment, it doesn't matter, because with any luck your program won't survive code review.

In principle, any compiler that conforms to C99 or later may reject your program with a fatal error message. (gcc will behave this way with -std=c99 -pedantic-errors) In practice, most compilers will merely print a warning. The call will probably work if the function returns int (or if you ignore the result) and if you get all the argument types correct. If the call is incorrect, the compiler may not be able to print good diagnostics. If the function doesn't return int, the compiler will probably assume that it does, and you'll get garbage results, or even crash your program.

So you can study this answer of mine, follow up by reading the various versions of the C standard, find out exactly which edition of the standard your compiler conforms to, and determine the circumstances in which you can safely omit a #include header -- with the risk that you'll mess something up next time you modify your program.

Or you can pay attention to your compiler's warnings (Which you've enabled with whatever command-line options are available), read the documentation for each function you call, add the required #includes at the top of each source file, and not have to worry about any of this stuff.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

First of all: just include them.

If you don't the compiler will use the default prototype for undeclared functions, which is:

int functionName(int argument);

So it will compile, and link if the functions are available. But you will have problems at runtime.

Kninnug
  • 7,992
  • 1
  • 30
  • 42
0

There are a lot of things you can't do if you leave out headers:

(I'm hoping to get some more from the comments since my memory is failing on this ...)

  • You can't use any of the macros defined in the headers. This can be significant.
  • The compiler can't check that you are calling functions properly since the headers define their parameters for it.
Lee Meador
  • 12,829
  • 2
  • 36
  • 42
0

For compatibility with old program C compilers can compile code calling functions which have not been declared, assuming the parameters and return value is of type int. What can happen? See for example this question: Troubling converting string to long long in C I think it's a great illustration of the problems you can run into if you don't include necessary headers and so don't declare functions you use. What happened to the guy was he tried to use atoll without including stdlib.h where atoll is declared:

char s[30] = { "115" };
long long t = atoll(s);
printf("Value is: %lld\n", t);

Surprisingly, this printed 0, not 115, as expected! Why? Because the compiler didn't see the declaration of atoll and assumed it's return value is an int, and so picked only part of the value left on stack by the function, in other words the return value got truncated.

That's why of the reasons it is recommended to compile your code with -Wall (all warnings on).

Community
  • 1
  • 1
piokuc
  • 25,594
  • 11
  • 72
  • 102