In this chapter you will be introduced to the C preprocessor and macros . Besides, you will learn to use pre-processors like conditional, pragma, line control etc. in your program.
C Preprocessor and Macros
C preprocessors are macro preprocessors that allow you to define preprocessors in your program and that transform your program before compiling. This transformation may include header files, macro extensions, etc.
#
All preprocessors begin with the directive symbol.
#define PI 3.14
Attaching the header file
#include
C programs add pre-processors for attaching header files . For example:
#include
Here "stdio.h"
is the header file. which consists of function and macro definitions. #include
The pre-processor directive stdio.h
replaces the above line with the contents of the header file.
scanf()
And that’s the main reason to use a preprocessor printf()
before using a function .#include <stdio.h>
You can also create your own header files containing various function definitions and include them in your program using preprocessor directives.
C #if, #elif, #else, #endif pre-processors
These preprocessor directives create conditional parameters for compiling the source code that control the compilation of the source code. They must start on different lines.
syntax
#if constant_expression
#else constant_expression
#endif
or
#if constant_expression
#elif constant_expression
#endif
- If and only if the value of the #if expression is true (non-zero) then the compiler compiles the following codes.
- But if the value of the #if expression is false(0) then the compiler skips the lines up to the next #else, #elif or #endif.
- Lines between #else and #endif are compiled if the #if expression evaluates to false(0) and any #else evaluates to true(non-zero).
- Lines between #elif and #endif are compiled if the value of #if expression is false(0) and if the constant_expression of an #elif is true(non-zero).
Example 1:
int main(void)
{
#if 1
printf("Hello SATT!\n");
#else
printf("Hello Aziz!\n");
#endif
return 0;
}
output
"Hello SATT!"
In the above example only “Hello SATT!” has been printed. Because the value of #if is 1, the compiler skips the following codes.
Example 2:
int main(void)
{
#if 1
printf("Mango\n");
#elif 1
printf("Orange\n");
#endif
return 0;
}
output
"Mango"
Here too only “Mango” is printed. Because only if the first line #if is 0 then “Orange” will be printed.
Example 3:
#if OS==1
printf("Version 1.0");
#elif OS==2
printf("Version 2.0");
#else
printf("Version unknown");
#endif
will be printed based on the OS setting and defined by the #define preprocessor.
C #define, #undef, #ifdef, #ifndef pre-processors
#define and #undef preprocessor directives agree to define identifiers that carry specific values. These identifiers can usually be constants or macro functions.
The #ifdef and #ifndef directives allow a specified number of lines of code to be compiled based on the condition that the identifier is defined.
syntax
#define identifier replacement-code
#undef identifier
#ifdef identifier
#else or #elif
#endif
#ifndef identifier
#else or #elif
#endif
- #ifdef identifier and #if defined( identifier) are used in the same sense.
- #ifndef identifier and #if !defined(identifier) are used in the same sense.
- An identifier defined by #define exists anywhere in the source code until #undef is reached.
Macro functions can be defined by #define in the following ways:
#define identifier(parameter-list) (replacement-text)
The value of parameter-list is replaced by the value of replacement-text.
Example 1:
#define PI 3.141
printf("%f",PI);
#define DEBUG
#ifdef DEBUG
printf("This is a debug message.");
#endif
#define QUICK(x) printf("%s\n",x);
QUICK("Hi!")
#define ADD(x, y) x + y
z=3 * ADD(4,5)
The output of the above program will be 17. Because the work of guna takes place before addition.
Example 2:
#define ADD(x,y) (x + y)
z=3 * ADD(4,5)
The output of the above program will be 17. Because sum is enclosed in parentheses and parentheses take precedence over multiplication.
C #include directive
External header files can be processed by the compiler through the #include directive.
syntax
#include
or
#include "source-file"
When a file is enclosed between < and > , the compiler (implementation) searches the known header directory (defined by compile) for that file and compiles it.
When the file is enclosed in double quotes, the entire content of the source-file is replaced. This search method for files is implementation-specific.
Example:
#include
#include "my_header.h"
C#line pre-processor
The #line directive allows changing the current line number and the current source code file name.
syntax
#line line-number filename
Note that if no file name is given, it remains the same. The line number of the current line is one before the new line. So the first line is number 1.
Example:
#line 50 user.c
#line 23
C #error pre-processor
The #error directive stops compilation and returns the specified error message.
syntax
#error message
Example:
#ifndef VERSION
#error Version number not specified.
#endif
C #pragma pre-processor
The #pragma directive allows defining directives. It controls the behavior of the compiler. If the pragma does not support it, it is skipped.
syntax
#pragma directive
C macro
Macro (macro) is a fragment of code (a fragment of code) which is given a specific name. You can use this code fragment in your program by using the name.
Predefined macros
The following macros are already defined in the compiler and cannot be changed.
Predefined macros | description |
---|---|
__LINE__ | Integer constant to indicate the current line number |
__FILE__ | The name of the executable source code file is the string for the directive |
__DATE__ | String indicating the start date of compilation of the current source file. This is exactly the same format as the asctime function returns the date. eg “mmm dd yyyy”. |
__TIME__ | String for the directive when compiling the running source file. This is exactly the same format as the asctime function returns the date. eg “hh:mm:ss”. |
__STDC__ | If following the ANSI standard, the value will be 1 (non-zero). |
Example:
C program for determining running time using predefined macros
#include
int main()
{
printf("Current time: %s",__TIME__); // Determine the current time
}
Output
Current time: 19:54:39