0% found this document useful (0 votes)
62 views14 pages

Limbajul de Programare C: Faculty of Mathematics and Informatics

This document discusses the C preprocessor, which performs textual substitutions on C source code based on preprocessor directives before compilation. It describes common preprocessor directives like #include for including header files, #define for defining macros, and #if/#endif for conditional compilation. It also covers preprocessor pitfalls with macros and provides examples of standard predefined macros like __FILE__ and __LINE__.

Uploaded by

sandu405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views14 pages

Limbajul de Programare C: Faculty of Mathematics and Informatics

This document discusses the C preprocessor, which performs textual substitutions on C source code based on preprocessor directives before compilation. It describes common preprocessor directives like #include for including header files, #define for defining macros, and #if/#endif for conditional compilation. It also covers preprocessor pitfalls with macros and provides examples of standard predefined macros like __FILE__ and __LINE__.

Uploaded by

sandu405
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 14

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

Limbajul de programare C

08.01.2015

Lucian Cucu - The C Programming Language

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

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

pure* C source text


(no pre-processor directives)

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

Lucian Cucu - The C Programming Language

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

The C Preprocessor: types of actions

Source file inclusion:

#include

Macro replacement:

#define, #undef

Conditional inclusion:

#if, #ifdef, #ifndef, #else, #elif, #endif

Line control :

#line

Error directive:

#error

Pragma directive:

#pragma

08.01.2015

Lucian Cucu - The C Programming Language

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

C Preprocessor directives: #include


Usage: source file inclusion
Syntax:
#include <file_name>
#include file_name
The difference between the two syntax forms:
- in the first form, the search of the file to be included starts in an installation defined path
- in the second form, the search starts in the folder where the source resides

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

Lucian Cucu - The C Programming Language

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

C Preprocessor directives: #include avoiding repetead inclusions


Header files should not contain code, just:
- preprocessor directives
- typedefs
- external declarations
- function prototypes

Header files should not be repeatedely


included.

E.g.
#define FALSE
0
#define TRUE
1
typedef long Coord;
extern Coord x0, y0;
extern globalCount;
Coord getX();
Coord getY();

There is a standard way of preventing this:

Module.h
#ifndef H_MODULE
#define H_MODULE
/*content to be included*/

#endif

08.01.2015

Lucian Cucu - The C Programming Language

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

C preprocessor directives: #define

Usage: Associates a name to a substitution string.


The name is usually called: a macro name or simply macro.
Syntax:
#define macro_name substitution string
#define macro_name(parameters)
substitution string

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:

Traditionally, macro names are all-upper-case-letters


08.01.2015

Lucian Cucu - The C Programming Language

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

C preprocessor directives: #undef

Usage: un-defines a macro name


Syntax:
#undef macro_name
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".

08.01.2015

Lucian Cucu - The C Programming Language

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

The C Preprocessor: parameterized macros


Eg.
- definitions:
#define max(A, B)

((A) > (B) ? (A) : (B))

#define square(x)

(x) * (x)

- calls:
max(x1, x2)
max(z, 1000)
max(r, -1.75)
square(a)
square(1.5)

macroexpansion

(x1)>(x2) ? (x1) : (x2)


(z)>(1000) ? (z) : (1000)
(r)>(-1.75) ? (r) : (-1.75)
(a)*(a)
(1.5)*(1.5)

Sintactically, a macro call is similar to a function call!

During preprocessing, the macro-call is replaced by the substitution string in


which the formal parameters are replaced with the actual arguments
(macro-expansion)

08.01.2015

Lucian Cucu - The C Programming Language

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

Why macros? Macros vs functions

Using macros results in faster code because no call overhead is incurred!


No type checking is performed, a macro may be called with arguments of
different types
Because macros are expanded in-line, they may change the actual
arguments (see swap example!
They are a means of making code more lizible

On the other hand :


functions are safer, because of the type checking performed on the actual
arguments

using functions reduces the size of the executable code

08.01.2015

Lucian Cucu - The C Programming Language

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

The C Preprocessor: parameterized macros - pitfalls


#define max(A, B)
#define square(x)

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)

{ a=a^b; b=a^b; a=a^b;}

#define swap(a, b)

{int temp; temp=a; a=b; b=temp;}

#define swap(a, b, type)

08.01.2015

{type temp;
temp=a;
a=b;
b=temp;
}

\
\
\
\

Lucian Cucu - The C Programming Language

10

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

C Preprocessor directives: conditional compilation

#idef macro_name
...
#endif

#ifndef
...
#endif

macro_name

#if defined macro_name


...
#endif

#if !defined macro_name


...
#endif

#if constant_expression
...
#endif

#if !constant_expression
...
#endif

08.01.2015

Lucian Cucu - The C Programming Language

11

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

C Preprocessor directives: #error

Usage: diagnostic messages during preprocessing


Syntax:
#error text
Action: produces a diagnostic message that includes the specified text

08.01.2015

Lucian Cucu - The C Programming Language

12

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

C Preprocessor directives: #pragma

Usage: implementation defined actions


Syntax:
#pragma implementation_specific_directive
Action: Announces an implementation specific action (e.g. grouping of
variables in special memory sections)

08.01.2015

Lucian Cucu - The C Programming Language

13

West University of Timisoara

Programming I

Faculty of Mathematics and Informatics

The Cpreprocessor: Predefined ANSI-C Macros

__FILE__ is replaced by the name without extension of the current file.

__LINE__ is replaced by the current line number.

__TIME__ is replaced by a string containing the time when the compilation


was started.

__DATE__ is replaced by a string containing the date when the compilation


was started.

__STDC__ is set to 1 for all compilers that are built up according to the ANSI
standard.

08.01.2015

Lucian Cucu - The C Programming Language

14

You might also like