· Before a C
program is compiled in a compiler, source code is processed by a program called
preprocessor. This process is called preprocessing.
·
Commands
used in preprocessor are called preprocessor directives and they begin with “#”
symbol.
·
Below is
the list of preprocessor directives that C language offers.
S.no
|
Preprocessor
|
Syntax
|
Description
|
1
|
Macro
|
#define,#undef
|
#define
macro defines constant value and can be any of the basic data types.
#undef is used to undefine a
defined macro variable.
|
2
|
Header file inclusion
|
#include <file_name>
|
The source code of the file
“file_name” is included in the main program at the specified place
|
3
|
Conditional compilation
|
#ifdef, #endif, #if, #else,
#ifndef
|
Set of commands are included or
excluded in source program before compilation with respect to the condition
|
4
|
Other directives
|
#pragma,#error
|
#Pragma is used to call a
function before and after main function in a C program
|
A program
in C language involves into different processes. Below diagram will help
you to understand all the processes that a C program comes across.
Example program
for #define, #include preprocessors in C:
·
#define -
This macro defines constant value and can be any of the basic data
types.
·
#include
<file_name> - The source code of the file
“file_name” is included in the main C program where “#include
<file_name>” is mentioned.
#include
<stdio.h>
#define
height 100
#define
number 3.14
#define
letter 'A'
#define
letter_sequence "ABC"
#define
backslash_char '\?'
void
main()
{
printf("value of height : %d \n", height );
printf("value of number : %f
\n", number );
printf("value of letter : %c
\n", letter );
printf("value of letter_sequence : %s
\n", letter_sequence);
printf("value of backslash_char : %c \n", backslash_char);
}
OutPut
value of
height : 100
value of
number : 3.140000
value of
letter : A
value of
letter_sequence : ABC
value of
backslash_char : ?
|
Example
program for conditional compilation directives:
a) Example program for #ifdef,
#else and #endif in C:
·
“#ifdef”
directive checks whether particular macro is defined or not. If it is defined,
“If” clause statements are included in source file.
·
Otherwise,
“else” clause statements are included in source file for compilation and
execution.
#include
<stdio.h>
#define
RAJU 100
void main()
{
#ifdef RAJU
printf("RAJU is defined. So, this
line will be added in " \
"this C file\n");
#else
printf("RAJU is not defined\n");
#endif
}
Output:
RAJU is defined. So, this line will be
added in this C file
|
b) Example program
for #ifndef and #endif in C:
·
#ifndef
exactly acts as reverse as #ifdef directive. If particular macro is not
defined, “If” clause statements are included in source file.
·
Otherwise,
else clause statements are included in source file for compilation and
execution.
#include
<stdio.h>
#define
RAJU 100
void main()
{
#ifndef SELVA
{
printf("SELVA is not defined. So,
now we are going to " \
"define here\n");
#define SELVA 300
}
#else
printf("SELVA is already defined in
the program”);
#endif
}
Output:
SELVA is not defined. So, now we are
going to define here
|
c) Example program for #if,
#else and #endif in C:
·
“If” clause
statement is included in source file if given condition is true.
·
Otherwise,
else clause statement is included in source file for compilation and execution.
#include
<stdio.h>
#define a
100
void main()
{
#if (a==100)
printf("This line will be added in this
C file since " \
"a \= 100\n");
#else
printf("This line will be added in
this C file since " \
"a is not equal to
100\n");
#endif
}
Output:
This line will be added in this C file
since a = 100
|
Example program for undef in C:
This directive undefines existing
macro in the program.
#include
<stdio.h>
#define
height 100
void
main()
{
printf("First defined value for
height : %d\n",height);
#undef height // undefining variable
#define height 600 // redefining the same for new value
printf("value of height after undef
\& redefine:%d",height);
}
Output:
First defined value for height :
100
value of height after undef &
redefine : 600
|
Example program for pragma in C:
Pragma is
used to call a function before and after main function in a C program.
#include <stdio.h>
void function1( );
void function2( );
#pragma startup function1
#pragma exit function2
void main( )
{
printf ( "\n Now we are in main function" ) ;
}
void function1( )
{
printf("\nFunction1 is called before main function call");
}
void function2( )
{
printf ( "\nFunction2 is called just before end of " \
"main function" )
;"
}
Output:
Function1 is called before main
function call
Now we are in main function
Function2 is called just before
end of main function
|
More on Other directive in C:
Sr.no
|
Pragma command
|
Description
|
1
|
#pragma
|
The pragma directive is used to
access compiler-specific pre-processor extensions. A common use of #pragma is
the #pragma once directive, which asks the compiler to include a header file
only a single time, no matter how many times it has been imported
|
2
|
#error
|
The #error macro allows you to
make compilation fail and issue a statement that will appear in the list of
compilation errors. It is most useful when combined with #if/#elif/#else to fail compilation if some
condition is not true
|
0 comments:
Post a Comment