Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are particular C data types declared in more than one standard header file?

Tags:

For example, C11 dictates that size_t should be declared in the following header files:

  • stddef.h
  • stdio.h
  • stdlib.h
  • string.h
  • time.h
  • uchar.h
  • wchar.h

When reading C11, I found there are many other data types declared in more than one standard header files.

Questions

  1. Let's say in the case of size_t. Why not just in stddef.h for simplicity?
  2. 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?
like image 917
Astaroth Avatar asked Aug 15 '14 12:08

Astaroth


People also ask

Why we use different header files in C?

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.

Why do we need header files name a few standard header files in C?

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.

What happens if we include a header file twice in C?

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.

Can we use same header files in multiple C programs are not?

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.


2 Answers

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.

like image 158
trent Avatar answered Sep 22 '22 12:09

trent


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.

like image 44
Klas Lindbäck Avatar answered Sep 22 '22 12:09

Klas Lindbäck