File.c: Preprocessor GCC - E File.c - o Out.i
File.c: Preprocessor GCC - E File.c - o Out.i
c (Source File)
#include
#define #if
#warning #else
#elif
#error
#ifdef
Conditional Directives
#ifndef
#endif
This directive includes a file content into another file, it just copy the file content
without checking it.
#include
File2
File1 File2
= File1
File 1 will now get the content of file 2 whatever was this content, even if it is not
C code !
Example:
Syntax 2 #include “ file path “
This is used to include a file from any directory. The path may be absolute or relative.
Absolute Path
1- It is very long.
2- It is not portable, if you moved the code to another machine it wouldn't work
because the new machine doesn’t have the same path, So you have to edit the code
Relative Path Folder_1
The path from the source file till the header file
The header file in the same folder of the source file
File.c File.h
Folder_1
File.h
Relative Path
The path from the source file till the header file
Folder_1
The header file in outer folder of the source file
File.h
Folder_2
File.c
Folder_1
file1.h Folder_2
Write C code to print the value of two
variables, each one of them is defined in a file2.c
Folder_3
header file structured as follow.
file3.h
Also called Macro, and it has two types …
Macros
The preprocessor then will replace x in all upcoming code with 100
Example 1
Preprocessor expand it to
Preprocessor expand it to
Preprocessor expand it to
Preprocessor expand it to
1- Configurability
2- Readability
4- Consumes no memory
Note, the object like macro is not a variable, so you cann’t assign a value to it or
change its value in the run time ! it is like a nickname for certain value
Example
1- Set_Bit(Var,BitNo) -> This macro will set the bit number (BitNo) in the variable (Var) to 1
2- Clr_Bit(Var,BitNo) -> This macro will set the bit number (BitNo) in the variable (Var) to 0
3- Toggle_Bit(Var,BitNo) -> This macro will toggle the bit number (BitNo) in the variable (Var)
If you have a function like macro that consists of many lines, use the backslash \ and
the end of each line except for the last line.
The user here relied on calling the function print is one line so no need for brackets !
He doesn’t know that the print is a function like macro.
#if
/* Code */
#elif
/* Code */
#else
/* Code */
#endif
Notes:
Will be expanded to
The header file guard
Used to avoid many inclusion of the same header file, if you by mistake included the
same header file many times, the guard will allow you to include it only one !
HeaderFile