C lecture 7.1
C lecture 7.1
The C preprocessor is a macro processor that is used automatically by the C compiler to transform your program
before actual compilation (Pre-processor directives are executed before compilation.). It is called a macro processor
because it allows you to define macros, which are brief abbreviations for longer constructs. A macro is a segment of
code which is replaced by the value of macro. Macro is defined by #define directive.
Preprocessing directives are lines in your program that start with #. The # is followed by an identifier that is the
directive name. For example, #define is the directive that defines a macro. Whitespace is also allowed before and
after the #.
The # and the directive name cannot come from a macro expansion. For example, if foo is defined as a macro
expanding to define, that does not make #foo a valid preprocessing directive.
All preprocessor directives starts with hash # symbol.
List of preprocessor directives :
#include
#define
#undef
#ifdef
#ifndef
#if
#else
#elif
#endif
#error
#pragma
#include:
The #include preprocessor directive is used to paste code of given file into current file. It is used include system-defined
and user-defined header files. If included file is not found, compiler renders error.
Macro's (#define):
A macro is a segment of code which is replaced by the value of macro. Macro is defined by #define directive.
#undef:
To undefine a macro means to cancel its definition. This is done with the #undef directive.
Example:
#include <stdio.h>
#define PI 3.1415
#undef PI
main()
{
printf("%f",PI);
}
#ifdef:
The #ifdef preprocessor directive checks if macro is defined by #define. If yes, it executes the code.
#ifndef:
The #ifndef preprocessor directive checks if macro is not defined by #define. If yes, it executes the code.
#if:
The #if preprocessor directive evaluates the expression or condition. If condition is true, it executes the code.
#else:
The #else preprocessor directive evaluates the expression or condition if condition of #if is false. It can be used
with #if, #elif, #ifdef and #ifndef directives.
#include <stdio.h>
#include <conio.h>
#define NUMBER 1
void main()
{ #if NUMBER==0
printf("Value of Number is: %d",NUMBER);
#else
print("Value of Number is non-zero");
#endif
getch();
}
#error:
The #error preprocessor directive indicates error. The compiler gives fatal error if #error directive is found and skips further
compilation process.
#include<stdio.h>
#ifndef __MATH_H
#error First include then compile
#else
void main()
{
float a;
a=sqrt(7);
printf("%f",a);
}
#endif
#pragma:
The #pragma preprocessor directive is used to provide additional information to the compiler. The #pragma directive is used by the
compiler to offer machine or operating-system feature. Different compilers can provide different usage of #pragma directive.
#include<stdio.h>
#include<conio.h>
void func() ;
#pragma startup func
#pragma exit func
void main()
{
printf("\nI am in main");
getch();
}
void func()
{
printf("\nI am in func");
getch();
}
C Macros:
A macro is a segment of code which is replaced by the value of macro. Macro is defined by #define directive. There are two
types of macros:
Object-like Macros
Function-like Macros
Object-like Macros:
The object-like macro is an identifier that is replaced by value. It is widely used to represent numeric constants. For example:
#define PI 3.14
Here, PI is the macro name which will be replaced by the value 3.14.
Function-like Macros:
The function-like macro looks like function call. For example:
#define MIN(a,b) ((a)<(b)?(a):(b))
Here, MIN is the macro name.
C Predefined Macros:
Example:
#include<stdio.h>
int main(){
printf("File :%s\n", __FILE__ );
printf("Date :%s\n", __DATE__ );
printf("Time :%s\n", __TIME__ );
printf("Line :%d\n", __LINE__ );
printf("STDC :%d\n", __STDC__ );
return 0;
}
Rules for using macros:
When we use include directive, the contents of included header file (after preprocessing) are copied to the current
file.
When we use define for a constant, the preprocessor produces a C program where the defined constant is searched
and matching tokens are replaced with the given expression.
The macros can take function like arguments, the arguments are not checked for data type.
The macro arguments are not evaluated before macro expansion.
The tokens passed to macros can be concatenated using operator ## called Token-Pasting operator.
A token passed to macro can be converted to a string literal by using # before it.
The macros can be written in multiple lines using ‘\’. The last line doesn’t need to have ‘\’.
The macros with arguments should be avoided as they cause problems sometimes. And Inline functions should be
preferred as there is type checking parameter evaluation in inline functions.
Preprocessors also support if-else directives which are typically used for conditional compilation.
Distinction between function and macros: