Table of Contents
This guide covers header file in c with practical context and easy-to-follow details. Use it to understand the subject and apply the information confidently.
You require using the file Header in the program by adding it to the program, with the #include Preprocessor character as you add Stdio. H To the Header file, it will accompany your compiler.
Including the Header file is equivalent to copying the Header file's content, but you don't need to do it, just #include, your code will be more compact and still use the content of the Header file.
In fact C and C ++ programs we store most constants and macros and global variables and function prototypes in the Header files and include these files when you need to use them.
Syntax Include in C
Both the user file and the system header are included using the #include Preprocessor instruction. Here are 2 types:
ch?a
This format is used for system Files . It will find the file with the file name in the list of system directories.
include "file"
This format is used for files in your program. It will search for files with the file name in the same directory as the current File .
Include Activities
The #include directive Works by directing the preprocessor in the C language to scan certain files to add to the current file before starting with the existing source code. For example, if you have a Header file as header. H as follows:
char * test (void);
And the main program calls Program. C To use the File header, like:
int x;
#include "header.h" int main (void)
{
puts (test ());
}
And the compiler will see the same token stream, when the program. C program reads as follows:
int x;
char * test (void); int main (void)
{
puts (test ());
}
Once-Only Header
If the file header is Included Twice, the compiler will report an error and print an error. The standard way to avoid this case is to use conditional expressions as follows:
ifndef HEADER_FILE #define HEADER_FILEthe entire header file file # endif
In case it is already included, the above program will not include it again.
Include with the Conditions
Sometimes you need to choose one of the headers to include in the program, you must have the operating system configuration parameter to use. You can do this with a range of conditions as follows:
if SYSTEM_1 # include "system_1.h" #elif SYSTEM_2 # include "system_2.h" #elif SYSTEM_3 . #endif
But when this number of conditions is many, it becomes tedious, instead the preprocessor provides the ability to use a macro for the Header name. This is called Conditional Include . Instead of writing a Header name as the direct parameter of #include , you simply put a macro name there instead of it:
define SYSTEM_H "system_1.h" . #include SYSTEM_H
SYSTEM_H will be expanded, and the preprocessor will search for system_1. H if #include Has been written that way initially. SYSTEM_H can be defined by the file you create with -D option.
According to Tutorialspoint
Previous article: The preprocessor in C
Next lesson: Press type in C
FAQ
What should I check before following these steps?
Confirm device and software compatibility, save important data, and make sure you have the required permissions, files, and account access.
Why might the process not work?
Common causes include outdated software, missing permissions, incompatible hardware, an unstable connection, or completing a step in the wrong order.
Can I undo the changes if necessary?
That depends on the tool or setting. Use built-in restore options when available, keep a backup, and record the original configuration first.
Reader Comments 0
Sign in with email or Google to join the discussion.