C Programming - C Preprocessor - Discussion
Discussion Forum : C Preprocessor - Find Output of Program (Q.No. 12)
12.
What will be the output of the program?
#include<stdio.h>
#define str(x) #x
#define Xstr(x) str(x)
#define oper multiply
int main()
{
char *opername = Xstr(oper);
printf("%s\n", opername);
return 0;
}
Answer: Option
Explanation:
The macro #define str(x) #x replaces the symbol 'str(x)' with 'x'.
The macro #define Xstr(x) str(x) replaces the symbol 'Xstr(x)' with 'str(x)'.
The macro #define oper multiply replaces the symbol 'oper' with 'multiply'.
Step 1: char *opername = Xstr(oper); The varible *opername is declared as an pointer to a character type.
=> Xstr(oper); becomes,
=> Xstr(multiply);
=> str(multiply)
=> char *opername = multiply
Step 2: printf("%s\n", opername); It prints the value of variable opername.
Hence the output of the program is "multiply"
Discussion:
8 comments Page 1 of 1.
Rogerthatrambo said:
1 decade ago
#include<stdio.h>
#define str(x) #x
//#define Xstr(x) str(x)
#define oper multiply
int main()
{
char *opername = str(oper);
printf("%s\n", opername);
return 0;
}
// I got "oper" as output.
Can anyone elaborate ?
#define str(x) #x
//#define Xstr(x) str(x)
#define oper multiply
int main()
{
char *opername = str(oper);
printf("%s\n", opername);
return 0;
}
// I got "oper" as output.
Can anyone elaborate ?
Asheesh said:
1 decade ago
What is use of # symbol in str(x) #x, answer please?
Abhishek said:
1 decade ago
@Rogerthatrambo.
You have missed out 'X'. In place of Xstr(oper) , you have written str(oper).
That's why you are getting output as oper.
You have missed out 'X'. In place of Xstr(oper) , you have written str(oper).
That's why you are getting output as oper.
Aman said:
1 decade ago
str(multiply) will be replaced with #mutiply, So the output will be #multiply not multiply.
Mandar said:
1 decade ago
In given problem no double codes are specified so answer should be an error.
DIA said:
10 years ago
@Abhishek.
But #define oper multiply still exist, why not got "multiply"?
But #define oper multiply still exist, why not got "multiply"?
Ritika said:
9 years ago
Why we don't get #multiply?
Deepak said:
8 years ago
It converts the "x" parameter into a null-terminated string. Whatever is given to "x" when the macro is invoked, the exact value of "x" will be placed into the string.
So, basically, the '#' is used to typecast any input to a string.
So, basically, the '#' is used to typecast any input to a string.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers