For example, C11 dictates that size_t
should be declared in the following header files:
When reading C11, I found there are many other data types declared in more than one standard header files.
size_t
. Why not just in stddef.h
for simplicity?size_t
in those header files. Are they guaranteed to have the same definition in those header files?A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.
Explanation: A header file is generally used to define all of the functions, variables and constants contained in any function library that you might want to use. The header file stdio. h should be used if you want to use the two standard I/O functions printf and scanf.
If a header file happens to be included twice, the compiler will process its contents twice. This is very likely to cause an error, e.g. when the compiler sees the same structure definition twice. Even if it does not, it will certainly waste time. This construct is commonly known as a wrapper #ifndef.
Yes you have to include them both. I mostly work in C++ and I try to include files in my . cpp files whenever possible and in my . h files only if I must.
As an example of a function declared in stdio.h
that requires size_t be predeclared, consider snprintf()
. As it is, if you want to use it in your code, all you need to do is #include <stdio.h>
. If size_t were declared only in stddef.h
, you would have to
#include <stddef.h> #include <stdio.h>
Not only that, but since stdio.h
declares snprintf
whether you use it or not, you would have to include both files every time you needed anything in stdio.h
to avoid compiler errors; stdio.h
would have an artificial dependency on stddef.h
. That causes your source code to become longer and more brittle (notice that if you reverse the order of the two directives it would also break). Instead, we write header files so that they stand alone and do not depend on other headers, and this is what the C standardization committee decided on for the standard library.
Let's say in case of size_t. Why not just in stddef.h for simplicity?
The type is used in the declaration of functions in all those files. If it wasn't declared in <stdio.h>
you would get a compilation error unless you first include <stddef.h>
.
Let's say a C compiler implements size_t in those header files. Are they guaranteed to have the same definition in those header files?
Yes, they will have the same definition. Usually, the value is defined in a single place in a separate include file that is included by the others.
In some cases it may be possible to modify the definition with compiler options or defines, for example a compiler that allows 32/64 bit compilation may define size_t
as a 32 or 64 bit unsigned entity depending on the target defined on the compiler command line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With