C Questions and Answers – Stringizers

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

Answer: b
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?

advertisement
advertisement
#define sanfoundry(s,n) #s #n
main()
{
    printf(sanfoundry(hello,world));
}

a) sanfoundry(hello,world)
b) sanfoundry
c) hello,world
d) helloworld
View Answer

Answer: d
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.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

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

Answer: d
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.
advertisement

4. What will be the output of the following C code?

advertisement
#define display(a) #a
main()
{
    printf(display("56#7"));
}

a) Error
b) “56#7”
c) 56#7
d) 567
View Answer

Answer: b
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

Answer: c
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

Answer: b
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

Answer: d
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

Answer: b
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

Answer: a
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

Answer: c
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.

If you find a mistake in question / option / answer, kindly take a screenshot and email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.