# define directive in c


# 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;
}

1 comment:

Unknown said...

what is the difference between #ifdef & #if defined