What is use of # and ## operator in c programming language?
There are two operators in preprocessor in c:
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. For 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 is:
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. For 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;
1 comment:
the example for ## is actually a code ..
it returns plenty of errors
Post a Comment