There are lots of header files in C. Is there any common header file that satisfies all the conditions of the program?
-
@Xenikh: That's for GCC's C++ headers, not for GCC's C headers. – MSalters Jun 21 '21 at 14:16
-
@MSalters by Bad :( – Abhishek Dutt Jun 21 '21 at 14:52
2 Answers
It totally depends on the program you write. In fact, even in common situations, you may end up in writing a program that requires no header at all.
This can happen for instance when doing computations only, then returning a status code to shell as an anwser. Here's an example that checks if two parameters at least are passed and if the two first string are the same:
int main (int argc, char **argv)
{
if (argc < 3)
return 1;
while (*argv[1] == *argv[2])
{
if (*argv[1] == '\0')
return 0;
++argv[1];
++argv[2];
}
return 1;
}
This program needs no header.
For the rest, it will totally depend on your dependencies, that is: what you basing your program on in order to get it working. Most of composite libraries do offer, indeed, a global header that includes all others, allowing to subdivise it for more efficiency, but in general, this remains restricted to the library itself.

- 3,719
- 8
- 17
- 30
No, there's no common header in C. If this were a C++ question, one possible(not recommended) solution would be the infamous <bits/stdc++.h>
. There's a good reason why there is no single file.
The preprocessor essentially pastes the contents of the header files included. Compare the size of the compiled binary of a simple C program:
int main() {
char const * str = "hello";
return 0;
}
which, on my system, generates a binary of size 16536 bytes. On adding some code:
#include <string.h>
int main() {
char const * str = "hello";
size_t len = strlen(str);
return len;
}
the size goes up to 49424 bytes. Now if you keep on adding headers to this file but don't actually use the structures/APIs from them, the generated code would still be the same size since unused code won't be there in the output binary but the build times would go up. So don't be lulled into the false sense of comfort that since the generated binary doesn't increase in size, all is probably fine. I can't back this up with perf numbers but there is a slightly noticeable increase in the compilation times on my system if I change that file to be
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include <ctype.h>
#include <locale.h>
#include <math.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <complex.h>
#include <stdalign.h>
#include <errno.h>
#include <locale.h>
#include <stdatomic.h>
#include <stdnoreturn.h>
#include <fenv.h>
#include <wchar.h>
#include <tgmath.h>
#include <stdarg.h>
#include <stdbool.h>
int main() {
char const * str = "hello";
size_t len = strlen(str);
return len;
}
where the compilation time increases from the earlier ~0.07s to ~0.10s. The code size is still the same but the compilation time increases. Obviously these are not numbers that would bother you if this was just a single file but imagine a project with 100s of files and such an increase would be in the order of seconds.
Also, having separate headers helps signal at a glance what your code is all about. If there are network header files like <arpa/inet.h>
etc. it gives the code reader a fair insight that your code is dealing with some network stuff. If it's stdio.h
, then your code uses I/O and so on. This also serves as a guiding principle to header-file crafters to avoid using unnecessary header files in their code(headers) and clearly delineate separate logic in separate header files, easing their usage by the header clients.
Any way out?
You could look at Pre-Compiled Headers, explained here on MSDN and LLVM site to see if the solution suits your purpose. It's essentially a list of header files that have very infrequent changes made to them so that the compiler can just generate code once and then re-use it one every compilation. Standard library header files are a good candidate to be added to such headers. That way, you could add your frequently used header files in it and just be done with them for once, and finally just include the pch.h
in your code. You, usually, are never going to change the headers files and thus the PCH need not be re-generated.

- 3,273
- 1
- 16
- 27