© 2014 Firstsoft Technologies (P) Limited. login
Hi 'Guest'
Home SiteMap Contact Us Disclaimer
enggedu

C and C++ Interview Questions

(5) What is use of # and ## operator in c program ?
Ans:

There are two operator in 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.
e.g
#define string(s) #s
void main()
{
char str[15]=string(World is our ) ;
printf(“%s”,str);
}
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.
e.g
#define merge(p,q,r) p##q##r
Void main()
{
int merge(a,b,c)=45;
printf(“%d”,abc);
}
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;

Ex - 1: What is the output?
        #define FALSE -1
         #define TRUE   1
         #define NULL   0
         main() {
         if(NULL)
            puts("NULL");
         else if(FALSE)
            puts("TRUE");
         else
            puts("FALSE");
         }
Answer: TRUE
Explanation: The input program to the compiler after processing by the preprocessor is,
      main(){
            if(0)
                  puts("NULL");
      else if(-1)
                  puts("TRUE");
      else
                  puts("FALSE");
            }
Preprocessor doesn't replace the values given inside the double quotes. The check by if condition is boolean value false so it goes to else. In second if -1 is boolean value true hence "TRUE" is printed.


SLogix Student Projects
bottom