Chapter 3 Preprocessor Directives 1
Chapter 3 Preprocessor Directives 1
Chapter-2
Dr. Chintan Thacker, Associate Professor
AI_ML Department, PIET
Outline
File Inclusion
Macros CHAPTER-1
Conditional Compilation
Preprocessor Directives
Pragmas
What is PREPROCESSOR DIRECTIVES
• In C programming, Preprocessors are programs that process the source code before compilation. Several steps are
involved between writing a program and executing a program in C
1) Macros
2) File Inclusion
3) Conditional Compilation
4) Other Directives
What is File Inclusion?
• File inclusion is a way to add the content of one file into another using the #include directive.
• It is commonly used in programming to include header files, which contain important things.
• We can use a file inclusive directive and include the header files in the program
• We can integrate function declaration, macros, and declarations of the external variables in the top
header file
Types of File Inclusion
File inclusion in C/C++ can be of two types: Standard File Inclusion and User-Defined File
Inclusion.
1. Standard File Inclusion : This type uses angle brackets < > to include header files that
are part of the standard library.
2. User-Defined File Inclusion : This type uses double quotes “ " to include files that you
have created.
Standard File Inclusion
• This type uses angle brackets < > to include header files that are part of the standard
library.
• These are pre-written files provided by the language and contain commonly used
functions or utilities.
#include <stdio.h> // Includes the standard input/output functions like printf, scanf
Example :
• Input/Output Operations: Functions like printf, scanf, fopen, etc., which help with reading from and writing
to the console or files.
• Mathematical Functions: Functions like sqrt, pow, sin, cos, etc., which perform mathematical operations.
• String Manipulation: Functions like strlen, strcpy, strcmp, etc., which work with strings
Standard File Inclusion(Conti...)
Example Code :
int main() {
double num = 9.0;
return 0;
}
Standard File Inclusion(Conti...)
Explanation of the Code:
• #include <stdio.h>: This includes the standard input/output functions, specifically the
printf function, which is used to display output to the console.
• #include <math.h>: This includes the standard math functions, specifically the sqrt
function, which calculates the square root of a number.
• In this case, the compiler will look for stdio.h and math.h in its standard library locations
(e.g., /usr/include on Linux or the default include folder in Windows).
User-Defined File Inclusion
• This type uses double quotes “ “ to include files that you have created.
custom-defined function.
Improved Readability: Reduces clutter in File Duplication: If the same file is included
the main program by separating multiple times, it can lead to redundancy
declarations unless include guards are used.
Encapsulation: Encapsulates Complexity: Mismanagement of header file
implementation in .c files and exposes only dependencies can make debugging
necessary interfaces via .h. challenging.
What is Macros ?
• Macros are a feature in C/C++ that allow you to define reusable constants or expressions that are
replaced by the preprocessor before the code is compiled.
• Whenever the name is used in program, it is replaced by the contents of the macro
• They are created using the #define directive. Do not put a semicolon (;) at the end of #define
statements
• Macros are a way to simplify your code, make it more readable, and reduce repetition.
Types of Macros
Macros in C/C++ can be of two types: Object-like Macros and Function-like Macros.
2. Function-like Macros : These macros are used to define expressions that take parameters,
behaving like inline functions.
Object-like Macros
• These macros are used to define constants.
• They act like a replacement for specific values, making the code easier to read and
maintain.
Simplicity: Easy to use for constants and No Debugging Info: Errors involving macros
simple calculations. are hard to trace as they are replaced
before compilation
Efficiency: Expanded inline, avoiding No Type Safety: Macros do not check the
function call overhead. data type, leading to potential unexpected
results.
Maintainability: Changing a macro updates Code Bloat: Large macros can make the
all its occurrences in the program final code size larger.
• #if, #elif, #else, #endif: Used to check a condition and include code based
on its evaluation.
• #ifdef and #ifndef: Used to check whether a macro is defined or not.
Common Use Cases
• Debugging:
Include debugging code during development and exclude it in the final
release.
• Feature Enabling/Disabling:
Compile code for specific features based on flags.
• Platform-Specific Code:
Compile different code depending on the operating system or hardware.
Example
Example
Example 2: Using #ifdef and #ifndef
#include <stdio.h>
#define FEATURE_X // Uncomment to enable FEATURE_X
Output (with #define FEATURE_X)
int main() { Feature X is enabled.
#ifdef FEATURE_X Output (without #define
printf("Feature X is enabled.\n"); FEATURE_X):
#else Feature X is disabled.
printf("Feature X is disabled.\n");
#endif
return 0;
}
Example 1: Debugging Using Conditional Compilation
#include <stdio.h>
#define DEBUG 1 // Enable debugging
#if DEBUG
#define LOG(x) printf("DEBUG: %s\n", x) // Macro to log debug messages
#else DEBUG: Debugging is enabled.
#define LOG(x) // Empty macro (does nothing)
#endif
int main() {
LOG("Debugging is enabled."); // Prints only if DEBUG is 1
return 0;
}
Conditional Compilation Advantages and Disadvantages
Advantages Disadvantages