Professional Documents
Culture Documents
Limbajul de Programare C: Faculty of Mathematics and Informatics
Limbajul de Programare C: Faculty of Mathematics and Informatics
Programming I
Limbajul de programare C
08.01.2015
Programming I
The C Preprocessor
A pre-compiling step which performs some textual substitutions on a C
source text, based on a set of directives.
C source text with
pre-processor directives
preprocessing
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!
08.01.2015
Programming I
#include
Macro replacement:
#define, #undef
Conditional inclusion:
Line control :
#line
Error directive:
#error
Pragma directive:
#pragma
08.01.2015
Programming 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!
08.01.2015
Programming I
E.g.
#define FALSE
0
#define TRUE
1
typedef long Coord;
extern Coord x0, y0;
extern globalCount;
Coord getX();
Coord getY();
Module.h
#ifndef H_MODULE
#define H_MODULE
/*content to be included*/
#endif
08.01.2015
Programming I
Action
The name (and parameters if they exist) and the substituion string are placed in a symbol table.
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
#define
#define
#define
#define
PI
TRUE
FALSE
GET_HIGH_BYTE_MASK
CLEAR_HIGH_BYTE_MASK
3.14159
1
0
0xFF00
0x00FF
Note:
Programming I
08.01.2015
Programming I
#define square(x)
(x) * (x)
- calls:
max(x1, x2)
max(z, 1000)
max(r, -1.75)
square(a)
square(1.5)
macroexpansion
08.01.2015
Programming I
08.01.2015
Programming I
A > B
x * x
? A : B
macro expansion
result=square(n);
result=square(n+1);
result=n*n
macro expansion
result=n+1*n+1
#define swap(a, b)
#define swap(a, b)
08.01.2015
{type temp;
temp=a;
a=b;
b=temp;
}
\
\
\
\
10
Programming I
#idef macro_name
...
#endif
#ifndef
...
#endif
macro_name
#if constant_expression
...
#endif
#if !constant_expression
...
#endif
08.01.2015
11
Programming I
08.01.2015
12
Programming I
08.01.2015
13
Programming I
__STDC__ is set to 1 for all compilers that are built up according to the ANSI
standard.
08.01.2015
14