#
define directive by examples and questions with explanation in c
This directive is also called as Macro substitution directive. Syntax:
#define <Macro_constant> [(<Para1>, <Para2>,...)] <Token_string>
Note: [] indicates optional term.
Task of macro substitution directive is to replace the identifier with corresponding Token_string. For example:
#include<stdio.h>
#define pie 3.14
int main() {
float r=3,area;
area = 3 * r * pie;
printf("%f",area);
return 0;
}
In the above c code we have defined a macro constant pie. Before the starting of actual compilation an intermediate is formed which is:
#include<stdio.h>
int main() {
float r=3,area;
area = 3 *r * 3.14;
printf("%f",area);
return 0;
}
We can see, only in place of macro constant pie corresponding token string i.e. 3.14 has pasted.
If define statement is very long and we want to write in next line then end first line by \. For example:
#include<stdio.h>
#define word c is powerful \
language.
int main(){
printf("%s",word);
return 0;
}
Output: c is powerful language
Preprocessor definitions in c
Preprocessor directive in c
#include directive in c
# define directive in c
Pragma directive in c
Warning directive
Preprocessor operators in c
# if directive in c
#line directive in c
# error directive in c
# elif in c
# ifdef and #endif in c
# ifndef in c example
#undef in c
What is header file in ?
C preprocessor questions
C tutorial home.
1 comment:
what is the difference between #ifdef & #if defined
Post a Comment