0% found this document useful (0 votes)
84 views

Programare I (Curs 7)

The 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, #define, and #undef. #include inserts the contents of a header file. #define creates macros by substituting text. #undef removes macros. The document warns of potential issues with macros, like unexpected argument evaluation order, and recommends functions for safety.

Uploaded by

Lucian Houdini
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Programare I (Curs 7)

The 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, #define, and #undef. #include inserts the contents of a header file. #define creates macros by substituting text. #undef removes macros. The document warns of potential issues with macros, like unexpected argument evaluation order, and recommends functions for safety.

Uploaded by

Lucian Houdini
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 14

Universitatea de Vest Timisoara

Facultatea de Matematica si Informatica Programare I

Limbajul de programare C

06.02.2018 Lucian Cucu - The C Programming Language 1


Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare 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 preprocessing


pre-processor directives “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!
06.02.2018 Lucian Cucu - The C Programming Language 2
Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I

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

06.02.2018 Lucian Cucu - The C Programming Language 3


Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I

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!

06.02.2018 Lucian Cucu - The C Programming Language 4


Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I

C Preprocessor directives: #include – avoiding repetead inclusions

Header files should not contain code, just:


E.g.
- preprocessor directives
- typedefs #define FALSE 0
- external declarations #define TRUE 1
- function prototypes typedef long Coord;
extern Coord x0, y0;
extern globalCount;
Coord getX();
Header files should not be repeatedely Coord getY();
included.
There is a standard way of preventing this:

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

#endif

06.02.2018 Lucian Cucu - The C Programming Language 5


Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I

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 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

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".

06.02.2018 Lucian Cucu - The C Programming Language 7


Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I

The C Preprocessor: parameterized macros

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)

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)

06.02.2018 Lucian Cucu - The C Programming Language 8


Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I

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

06.02.2018 Lucian Cucu - The C Programming Language 9


Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I

The C Preprocessor: parameterized macros - pitfalls

#define max(A, B) A > B ? A : B


#define square(x) x * x

macro expansion
result=square(n); result=n*n

macro expansion
result=square(n+1); 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) {type temp; \


temp=a; \
a=b; \
b=temp; \
}
06.02.2018 Lucian Cucu - The C Programming Language 10
Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I

C Preprocessor directives: conditional compilation

#idef macro_name #ifndef macro_name


... ...
#endif #endif

#if defined macro_name #if !defined macro_name


... ...
#endif #endif

#if constant_expression #if !constant_expression


... ...
#endif #endif

06.02.2018 Lucian Cucu - The C Programming Language 11


Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I

C Preprocessor directives: #error

Usage: diagnostic messages during preprocessing


Syntax:
#error text

Action: produces a diagnostic message that includes the specified text

06.02.2018 Lucian Cucu - The C Programming Language 12


Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I

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)

06.02.2018 Lucian Cucu - The C Programming Language 13


Universitatea de Vest Timisoara
Facultatea de Matematica si Informatica Programare I

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.

06.02.2018 Lucian Cucu - The C Programming Language 14

You might also like