Two main types of include file directives in C:
1. #include <filename.h>:
The use of this syntax is to include system header file in C program. It tells the compiler to look for or find the file in the standard system directory. For example, #include<stdio.h> contains a standard I/O header file containing declarations of I/O functions such as printf and scanf.
2. #include “filename”:
This syntax is used to add a header file. It tells the compiler to first look for the file in the current directory and then look in the system directory. For example, #include "myheader.h" will include a header file called myheader.h that you created and saved in the same directory as the C source file.
These instructions are processed by a preprocessor, a program that processes the source code before it is compiled. The preprocessor effectively copies all the contents of the include file into the source code file, replacing the #include directive.
The use of file inclusion directives in the C Language is essential to organize C program code into many files for better code reuse, maintainability, and separation of the interface from the implementation.
For example, function definitions are placed in source file(*.c) while declarations are placed in header files (*.h)
This approach allows multiple source files to include the similar header file and use the functions stated there.
Examples how directives are used in C Language:
Standard Library Inclusion:
These headers provide access to standard macros, functions and types and are part of the C Standard Library.
The syntax:
#include <filename>
For example:
#include <stdio.h> includes the standard input/output header, providing functions like printf() and scanf().
#include <stdlib.h> includes the standard library header, providing functions like malloc(), free(), and exit().
User-defined header files.
These are header files that you or someone else created.
The syntax #include "filename". This instructs the compiler to look for the file in the current directory or in a list of directories specified during the compilation process. Once you have a custom header file, such as myheader.h, you can include it using the #include "filename" directive.
For example,
#include "myheader.h". This tells the compiler to look for myheader.h in the current directory or in the directories specified in the compiler's internal path.
Conditional Inclusion:
Every so often you can include a files conditionally. This can be completed using preprocessor directives such as #ifdef, #ifndef and #endif.
For example:
#ifdef use_math
# Include <math.h>
# endif
In this case, math.h will be included only if USE_MATH is set.
Nested Inclusion:
In C program file can be included as nested. Nested includes is where a file included by one file includes another.
For example, if stheader.h includes math.h, when you include stheader.h, math.h will also be included.
// In stheader.h
#include <math.h>
// In our main program file
#include “stheader.h”
Including the Same File Multiple Times:
If you include a header file several times in a source code file, the compiler will include the contents of the header file each time it meets the #include directive for that file. Here's what happens if you add the header file multiple times:
For example:
// In myheader.h
#ifndef MYHEADER_H
#define MYHEADER_H
// header file contents
#endif
This confirms that if myheader.h is included multiple times, its contents are processed only once.
Using Standard Library Functions:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Here, stdio.h is included to use the printf() function.
Including Multiple Headers:
#include <stdio.h>
#include <stdlib.h>
int main() {
char *dynamicString;
dynamicString = malloc(50 * sizeof(char));
if (dynamicString == NULL) {
fprintf(stderr, "Allocation failed\n");
exit(1);
}
printf("Memory allocated.\n");
free(dynamicString);
return 0;
}
In this example, stdlib.h and are included for memory allocation and IO input,output functions.
Including User-Defined Headers:
// In file: mathstat.h
int add(int a, int b);
// In file: main.c
#include "mathstat.h"
int main() {
int sum = add(5, 3);
printf("Sum: %d\n", sum);
return 0;
}
Here, mathstat.h is a user-defined header file included in main.c. These examples demonstrate the main ways in which file inclusion directives are used in C . The choice between #include <filename> and #include “filename” be determined by on whether the file is a standard library or a user-defined header file.
Previous Topic:-->> macros in C. || Next topic:-->>Conditional Compilation.