CTSD2 UNIT-3 Preprocessor Directives-1
CTSD2 UNIT-3 Preprocessor Directives-1
Structured Design-II(303105151)
Mrs. Praptiba Solanki
Computer Science & Engineering Department (PIT)
CHAPTER-3
Preprocessor Directives
Contents
• Preprocessor Directives
• File Inclusion
• Macros
• Conditional Compilation
• Pragmas
Preprocessor Directives
• A preprocessor is a program that processes our source program before it is
passed to the compiler.
• The preprocessor works on the source code and creates expanded source
code.
• The preprocessor offers several features called preprocessor directives.
• Preprocessor starts with # symbol.
• The directives can be placed anywhere in a program but are most often
placed at the beginning of the program, before the function definition.
Types of preprocessor directives
1. FILE INCLUSION
FILE INCLUSION
• File inclusion allows us to include the contents of one source code file into
another.
• This is done using the #include directive, which tells the C preprocessor to
insert the contents of the specified file into the current file at the point where
the #include directive appears.
• File inclusion is commonly used to separate code into different files for
better organization and maintainability.
• It also allows us to reuse code that is common to multiple source files
without having to duplicate it.
FILE INCLUSION
• Syntax:
• For Standard Library Inclusion: #include<header_file_name.h>
• For Custom Header File Inclusion: #include "header_file_name.h"
Standard Library Inclusion
• The standard library inclusion is used to include standard header files that
define functions, constants, and macros used in C programming.
• These header files are usually provided by the compiler or the operating
system, and are included using angle brackets (<>) as follows:
Standard Library Inclusion
• Explanation:
• We include the standard I/O header file stdio.h using the #include
directive.
• This header file defines the printf() function, which we use to print "Hello,
world!" to the console.
• Because we included the header file using the angle bracket notation (<
>), the 4 compiler will search for the header file in its standard library
directories.
Custom Header File Inclusion
• Custom header files are created by the programmer to define functions,
constants, and macros that are specific to a particular program or module.
• These header files are included using double quotes ("")
• When a file is included using the #include directive, its contents are treated
as if they were part of the current file.
• This means that any function or macro definitions in the included file are
available for use in the current file, and any global variables in the included
file are visible in the current file.
Custom Header File Inclusion
Custom Header File Inclusion
• Output: Addition is 9
• We include a custom header file add.h using the #include directive.
• This header file contains a function add() that takes two integers as input and
returns their sum.
• We use this function to calculate the sum of a and b, and then print the result
to the console using printf().
• Because we included the header file using the double quote notation (" "), the
compiler will search for the header file in the current directory or in any
directories specified by 6 the -I flag at compilation time.
2. MACROS
MACROS
• A macro is a piece of code in a program that is replaced by the value of
the macro.
• Macro is defined by #define directive.
• Whenever a macro name is encountered by the compiler, it replaces the
name with the definition of the macro
Types of macros
1. Object-Like Macros
2. Chain Macros
3. Multiline Macros
4. Function-Like Macros
1. Object-Like Macros
• An object-like macro is a simple identifier that will be replaced by a code
fragment.
• It is called object-like because it looks like an object in code that uses it.
• It is popularly used to replace a symbolic name with a numerical/variable
represented as a constant.
1. Object-Like Macros
2. Chain Macros
• Macros inside macros are termed chain macros. In chain macros first of
all parent macro is expanded then the child macro is expanded.
3. Multiline Macros
• An object-like macro could have a multi-line. So to create a multi-line macro
you have to use backslash-newline.
4. Function-Like Macros
• These macros are the same as a function call.
• It replaces the entire code instead of a function name.
• A pair of parentheses immediately after the macro name is necessary.
• Note: If we put a space between the macro name and the parentheses in
the macro definition, then the macro will not work.
4. Function-Like Macros
3. CONDITIONAL COMPILATION
CONDITIONAL COMPILATION
• Six directives are available to control conditional compilation.
• They delimit blocks of program text that are compiled only if a specified
condition is true.
• These directives can be nested.
• The program text within the blocks is arbitrary and may consist of
preprocessor directives, C statements, and so on.
• The beginning of the block of program text is marked by one of three
directives:
CONDITIONAL COMPILATION
• #if
• #ifdef
• #ifndef
Optionally, an alternative block of text can be set aside with one of two
directives:
▪ #else
▪ #elif
The end of the block or alternative block is marked by the #endif directive.
#ifdef
• The #elif directive performs a task similar to the combined use of the else-if
statements in C.
• This directive delimits alternative source lines to be compiled if the constant
expression in the corresponding #if , #ifdef , #ifndef , or another #elif
directive is false and if the additional constant expression presented in the
#elif line is true.
• An #elif directive is optional
#else and #endif
#else:
• This directive delimits alternative source text to be compiled if the condition
tested for in the corresponding #if, #ifdef, or #ifndef directive is false. An
#else directive is optional.
#endif
• This directive ends the scope of the #if, #ifdef, #ifndef, #else, or #elif
directive.
Example
4. PRAGMAS
PRAGMAS
• #pragma is a special purpose directive that is used to turn on or off some
features.
• #pragma allows us to provide some additional information to the compiler.
• Pragmas are compiler specific i.e., the behavior of pragma varies from
compiler to compiler.
1. #pragma startup and #pragma exit
• These directives help us to specify the functions that are needed to run
before the program starts and just before the program exits i.e., before the
control passes to the main() and just before the control returns from the
main().
• #pragma warn -rvl: This directive hides those warning which are raised when
a function which is supposed to return a value does not return a value.
• #pragma warn -par: This directive hides those warning which are raised when
a function does not uses the parameters passed to it.
• #pragma warn -rch: This directive hides those warning which are raised when
a code is unreachable. For example: any code written after the return
statement in a 19 function is unreachable.
2. #pragma warn
2. #pragma warn
Explanation: In this program, warnings are visible. Now those can be hidden
by using #pragma warn directive.
2. #pragma warn
2. #pragma warn
• The #pragma GCC dependency allows you to check the relative dates of
the current file and another file.
• If the other file is more recent than the current file, a warning is issued.
• This is useful if the current file is derived from the other file, and should be
regenerated.
• Syntax: #pragma GCC dependency "main.c"
4. #pragma GCC dependency
Explanation: At the compile time compiler will check whether the “parse.y” file is more
23 recent than the current source file. If it is, a warning will be issued during
compilation.
5. #pragma GCC system_header
• The #pragma GCC system_header directive in GCC is used to tell the
compiler that the given file should be treated as if it is a system header file.
• warnings that will be generated by the compiler for code in that file will be
suppressed hence it is extremely useful when the programmer doesn’t want
to see warnings or errors.
• This pragma takes no arguments.
• It causes the rest of the code in the current file to be treated as if it came
from a system header.
• Syntax: #pragma GCC system_header
5. #pragma GCC system_header