Programare I (Curs 7)
Programare I (Curs 7)
Limbajul de programare C
The C Preprocessor
Usually, the preprocessor is integrated with the compiler in a single executable file.
Some implementations supply also a standalone preprocessor, located in the bin folder of the installation
* The names of preprocessor directives are not reserved keywords of the language!
06.02.2018 Lucian Cucu - The C Programming Language 2
Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I
Action: the lines of the cited file are included in the current file, starting with the line following the
include-directive
Note:
Although any text file may be included, the files which are included that way should be header files!
Module.h
#ifndef H_MODULE
#define H_MODULE
/*content to be included*/
…
#endif
During preprocessing of subsequent source lines, untill a #undef macro_name directive or untill
the end of the source file, each occurrence of the macro_name is replaced by the substitution
string. This is also called macro substitution or macro expansion.
Ex.
#define PI 3.14159
#define TRUE 1
#define FALSE 0
#define GET_HIGH_BYTE_MASK 0xFF00
#define CLEAR_HIGH_BYTE_MASK 0x00FF
Note:
Traditionally, macro names are all-upper-case-letters
06.02.2018 Lucian Cucu - The C Programming Language 6
Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I
Action:
Eliminates the name macro_name from the symbol table managed by the
preprocessor. Any subsequent reference to the name will result in an error message
of the type: "Undefined symbol".
Eg.
- definitions:
#define max(A, B) ((A) > (B) ? (A) : (B))
#define square(x) (x) * (x)
- calls:
max(x1, x2) (x1)>(x2) ? (x1) : (x2)
max(z, 1000) macro- (z)>(1000) ? (z) : (1000)
max(r, -1.75) expansion (r)>(-1.75) ? (r) : (-1.75)
square(a) (a)*(a)
square(1.5) (1.5)*(1.5)
macro expansion
result=square(n); result=n*n
macro expansion
result=square(n+1); result=n+1*n+1
• __STDC__ is set to 1 for all compilers that are built up according to the ANSI
standard.