C Questions and Answers – Stringizers

«
»

This set of C Multiple Choice Questions & Answers focuses on “Stringizers”.

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.
advertisement

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

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.
advertisement
advertisement

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

advertisement
#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]
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?

#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”.
advertisement

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.

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!

advertisement
advertisement

Leave a Comment

Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter