Preprocessor operators in c


There are two operators in c preprocessor:
1. # : This operator is called stringizing operator which convert any argument in the macro function in the string. So we can say pound sign # is string maker. Example

#include<stdio.h>
#define string(s) #s
int main(){
    char str[15]= string(World is our );
    printf("%s",str);
    return 0;
}

Output: World is our
Explanation : Its intermediate file will look like:
int main(){
    char str[15]=”World is our”;
    printf("%s",str);
    return 0;
}
Argument of string macro function ‘World is our’ is converted into string by the operator # .Now the string constant “World is our” is replaced the macro call function in line number 4.

2. ## : This operator is called token pasting operator. When we use a macro function with various argument then we can merge the argument with the help of ## operator. Example

#include<stdio.h>
#define merge(p,q,r) p##q##r
int main(){
    int merge(a,b,c)=45;
    printf("%d",abc);
    return 0;
}
Output : 45
Explanation :
Arguments a,b,c in merge macro call function is merged in abc by ## operator .So in the intermediate file declaration statement is converted as :
int abc = 45;







  • 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.
  • 3 comments:

    dinhpq said...

    Thanks

    Anonymous said...

    thanks a lot!!!

    Unknown said...

    i don't understand the use of ## in merge what significance we get from it