Preprocessors in C
Preprocessors in C
• A line starting with a sharp sign „#‟ is a C preprocessor directive.
• This directive instructs the preprocessor to include the contents of a specified header file
into the source code before compilation.
• Preprocessor commands are known as directives.
• Preprocessor directives can be placed anywhere on the program.
• Note that preprocessor commands do not end in a semicolon “;”.
• The classification of C preprocessor directives into four categories can be summarized as:
1. Include Files /File Inclusion
2. Macro Definitions
3. Conditional Compilation
4. Miscellaneous Features
#include preprocessor directive (File Inclusion)
• The "#include" directive causes a copy of a specified file to be included in place of the directive.
• Two forms:
1. #include <filename>
• In this case the file name is enclosed between angle-brackets < > .
• the file is searched directly where the compiler is configured to look for the standard
header files.
2. #include "filename"
• In this case where the file name is specified between double-quotes.
• Directs the compiler to search in the current(same) directory and if it is not found there,
to look into the standard directories.
• Therefore, standard header files are usually included in angle-brackets, while other specific
header files(user defined header file) are included using quotes.
Example :
#include "myheader.h" would be used for including a user-defined header file.
#include <stdio.h> would be used for including a standard library header file
Note:Name of the header file should end in .h
MACROS( #define Preprocessor Directive )
• To define preprocessor macros we can use #define
• Its syntax is: #define identifier replacement
• When the preprocessor encounters this directive, it replaces any occurrence of identifier
in the rest of the code by replacement.
• This replacement can be an expression, a statement, a block or simply anything.
• A macro can either have parameters or not:
• A macro without parameters behaves like a constant, replacing its name with the
defined value.
• A macro with parameters works like a function, where the parameters are replaced
with the arguments provided when the macro is used.
• Symbolic constants and macros can be discarded using the "#undef" preprocessor
directive.
• Once undefined, a name can be redefined with "#define".
Macros without parameter
( The #define Preprocessor Directive as Constant)
#include<stdio.h>
#define PI 3.14
void main()
{
int r;
float area;
printf("Enter radius : ");
scanf("%d",&r);
area = PI * r * r;
printf("\nArea of circle is : %f",area);
}
Output : Enter radius : 5 Area of circle is : 78.5
Macros with parameter
( The #define Preprocessor Directive as function)
#include<stdio.h>
#define SQUARE(x) x*x
void main()
{
int num;
printf("Enter any number : ");
scanf("%d",&num);
printf("\nThe square is : %d",SQUARE(num));
}
Output :
Enter any number : 5
The square is : 25
Note :Function macros should be used with caution.
CONDITIONAL COMPILATION
(Conditional Inclusions)
• Used to compile only a portion of a program
• Conditional compilation is used when a single source code file needs to be
adapted for multiple platforms or different cases.
• Conditional compilation enables the programmer to control the execution of
preprocessor directives, and the compilation of program code.
• Conditional compilation
• #ifdef name
• #ifndef name
• #if expr
• #elif expr
• #else
• #endif
E.g.
• If NULL is not defined, it is defined by "#define NULL 0“
• If it is defined, the "#define" directive is skipped.
• Every "#if" ends with "#endif".
• Directives "#ifdef name" and "#ifndef name" are shorthand for "#if
define(name)" and "#if !defined(name)" respectively.
• A multiple-part conditional preprocessor may be tested using the "#elif" and
the "#else" directives.
• To comment out a large portions of code with comment,
• To insert some debugging statement, you may
Miscellaneous Directives
• #error
• #pragma
• #line
#error: Triggers a compilation error with a custom message.
#pragma: Provides compiler-specific instructions, like
optimizations or warnings (depends on the compiler).
#line: Alters the file and line number for error reporting.
Line control (#line)
• When we compile a program and an error occurs, the compiler shows an error message
with the file name and line number, making it easier to locate the problem in the code.
Syntax: #line number "filename"
Where,
• number is the new line number that will be assigned to the next code line.
• "filename" is an optional parameter that will be error in the error message.
Example:
Error directive(#error)
• To stop the compilation process intentionally.
• To display custom error messages when specific conditions are not
satisfied, ensuring that the code does not proceed unless certain requirements
are met.
Syntax:
#error "Error message“
"Error message": The custom message that will be displayed in the
error report, explaining the reason for the compilation failure.
Error directive(#error) (cont..)
example 1: example 2:
void main() void main()
{ {
#ifndef MAX #ifndef MAX
printf("\nMAX is not defined."); //Statement 1 #error MAX is not defined. //Statement1
#else #else
printf("\nMAX is defined."); //Statement 2 printf("\nMAX is defined."); //Statement 2
#endif #endif
printf("\nEnd of program."); //Statement 3 printf("\nEnd of program."); //Statement 3
} }
Output : Output:
MAX is not defined. 5 | #error MAX is not defined. //Statement1
End of program.
• The only difference between example 1 and 2 is the statement 1.
• In example 1,statement 1 will display message and continue to compile the rest of the code.
• In example 2, statement 1 will force the compiler to give fatal error and stop compiling
the rest of the code.
Pragma directive (#pragma)
• #pragma directive provides compiler-specific options to control various compilation
features, like optimization, warnings, or alignment.
• It is compiler-dependent, and its syntax can vary.
• Unsupported arguments are ignored without errors.
Example
(#pragma once (Prevent multiple inclusions of the same header file))
// my_header.h
#pragma once
void myFunction();
This ensures that the my_header.h file is included only once in a single
compilation, preventing duplicate declarations.
Predefined Symbolic Constants
• The following Predefined Symbolic Constants names are defined at any
time