C Multiple Choice Questions & Answers on “Stringizers”.
1. Which of the following is a stringizing operator?
a) < >
b) #
c) %
d) ##
Answer: b
Clarification: # is the stringizing operator. It allows formal arguments within a macro definition to be converted to a string.
2. What will be the output of the following C code?
#define (s,n) #s #n main() { printf((hello,world)); }
a) (hello,world)
b)
c) hello,world
d) helloworld
Answer: d
Clarification: The output to this code will be helloworld because when we use the stringizing operator, the resulting string will automatically be concatenated (combined) with any adjacent strings.
3. What will be the output of the following C code?
#define display(text) printf(#text "@") main() { display(hello.); display(good morning!); }
a) [email protected] morning!
b) error
c) hello.good [email protected]
d) [email protected] [email protected]
Answer: d
Clarification: Each actual argument is converted into string within the printf function. Each argument is concatenated with ‘@’, which is written as a separate string within the macro definition.
4. What will be the output of the following C code?
#define display(a) #a main() { printf(display("56#7")); }
a) Error
b) “56#7”
c) 56#7
d) 567
Answer: b
Clarification: In this case, it is not necessary for the argument in the printf function to be enclosed in double quotes. However, if the argument is enclosed in double quotes, no error is thrown. The output of the code shown will be “56#7”.
5. What will be the output of the following C code?
#define HELLO(a) #a main() { printf(HELLO(good morning)); }
a) good morning
b) goodmorning
c) good morning
d) error
Answer: c
Clarification: The output of the code shown above will be: good morning
In the resulting string, the consecutive blank spaces are replaced by a single blank space when we use the stringizing operator.
6. What will be the output of the following C code?
#include#define (x) #x int main() { int marks=100; printf("value of %s is = %dn",(marks),marks); return 0; }
a) error
b) value of marks=100
c) value of=100
d) 100
Answer: b
Clarification: In the code shown above, the variable name(marks) is passed as an argument. By using the # operator, we can print the name of the variable as a string.
7. What will be the output of the following C code?
#define hello(c) #c main() { printf(hello(i,am)); }
a) i,am
b) iam
c) i am
d) error
Answer: d
Clarification: The above code will result in an error. This is because we have passed to arguments to the macro hello, but it should be talking only one.
8. What will be the output of the following C code?
#define hello(c,d) #c #d main() { printf(hello(i,"am")); }
a) iam
b) i“am”
c) am
d) “am”
Answer: b
Clarification: The output for the following code will be i”am”. Since 2 arguments are passed and the macro hello takes two arguments, there is no error.
9. What will be the output of the following C code?
#define F abc #define B def #define FB(arg) #arg #define FB1(arg) FB(arg) main() { printf(FB(F B)); FB1(F B); }
a) F B
b) Error
c) FB
d) “FB”
Answer: a
Clarification: The argument F B(only one space between F and B) is passed to the macro FB. This argument is converted to a string with the by the stringizing operator. Thus F B is printed.
10. What will be the output of the following C code?
#define display(text) "$" #text main() { printf(display(hello world)); }
a) hello world
b) $helloworld
c) $hello world
d) error
Answer: c
Clarification: The output of the code shown above is $hello world
The argument “hello world” is passed to the macro text. The symbol “$” is present from before. In the resulting string, all the blank spaces are replaced by a single blank space. In addition to this, “$” is concatenated to the beginning of the resultant string.