File.
c (Source File)
Preprocessor gcc -E file.c -o out.i
File.i (Intermediate File)
Compiler gcc -S file.c -o out.asm
File.asm (Assembly File)
Assembler gcc -c file.c -o out.o
File.asm (Assembly File)
Linker gcc file.c -o out.exe
File.exe (Executable File)
Write C code to print your name, generate
the following files:
1- The intermediate file
2- The assembly file
3- The object file
4- The executable file
The C Preprocessor:
1- First part of the building process
2- It concerns only with its directives which always start with # sign
3- It generates an intermediate file written in C language after executing all directives
The Preprocessor Directives:
#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 !
Preprocessor is a just text replacement
#include < file name > #include “ file path “
In case of standard or default file In case of user file
Syntax 1 #include < file name >
This is used to include a file from a predetermined directory.
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
The absolute path is not recommended because:
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
The header file is in inner folder from the source file
File.c
Folder_2
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
Object Like Macro Function Like Macro
Object Like Macro
The preprocessor then will replace x in all upcoming code with 100
Example 1
Preprocessor expand it to
x will be replaced by 100 in the preprocessor stage and before the
compilation stage
Example 2
Preprocessor expand it to
x here will not be replaced as it is x here is replaced by 100
not alone, it is a part from a string
Example 3
Preprocessor expand it to
x here is not replaced as it is note
alone, it is a part from a word
Example 4
Preprocessor expand it to
x will be replaced and the compiler
will give an error
What are the benefits of using object like macros … ?
1- Configurability
2- Readability
3- Avoid magic numbers
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
Opening a bracket after the macro name make it a
function like macro
At any line of the code if you wrote for example:
It would be expanded to:
Write a header file that provides the following functions like macros:
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.
If the user call the macro in his code like this:
It will be expanded to:
Consider the following piece of code:
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.
This code will expand to:
Compilation error: else without if !
The only valid solution is to use do while (0) …
This directive will generate a warning on the console while building the project, the
waning doesn’t stop the building process
This directive will generate an error on the console while building the project, the
error stops the building process, no output file will be generated
Syntax:
#if
/* Code */
#elif
/* Code */
#else
/* Code */
#endif
Notes:
1- #elif and #else are optional
2- the #if statement must end with #endif
3- No { } are used
Once the preprocessor finds the
true condition, it will include the
statements under that condition
and remove all other code
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