This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Stringizers”.
Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.
1. Which of the following is a stringizing operator?
a) < >
b) #
c) %
d) ##
View Answer
Explanation: # 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 sanfoundry(s,n) #s #n main() { printf(sanfoundry(hello,world)); }
a) sanfoundry(hello,world)
b) sanfoundry
c) hello,world
d) helloworld
View Answer
Explanation: 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) hello.@good morning!
b) error
c) hello.good morning!@
d) hello.@good morning!@
View Answer
Explanation: 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
View Answer
Explanation: 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
View Answer
Explanation: 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 <stdio.h> #define sanfoundry(x) #x int main() { int marks=100; printf("value of %s is = %d\n",sanfoundry(marks),marks); return 0; }
a) error
b) value of marks=100
c) value of=100
d) 100
View Answer
Explanation: 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
View Answer
Explanation: 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”
View Answer
Explanation: 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”
View Answer
Explanation: 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
View Answer
Explanation: 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.
Sanfoundry Global Education & Learning Series – C Programming Language.
To practice all areas of C language, here is complete set of 1000+ Multiple Choice Questions and Answers.
- Check C Books
- Check Computer Science Books
- Practice Computer Science MCQs
- Practice BCA MCQs
- Apply for Computer Science Internship