C Questions and Answers – Conditional Preprocessor Directives – 1

«
»

This set of C MCQs focuses on “Conditional Preprocessor Directives – 1”.

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

advertisement
#include<stdio.h>
#define max 100
main()
{
    #ifdef max
    printf("hello");
}

a) 100
b) hello
c) “hello”
d) error
View Answer

Answer: d
Explanation: The code shown above results in an error. This is because the preprocessor #endif is missing in the above code. If the identifier is defined, then the statements following #ifdef are executed till #endif is encountered.
advertisement
advertisement

2. _______________ is the preprocessor directive which is used to end the scope of #ifdef.
a) #elif
b) #ifndef
c) #endif
d) #if
View Answer

Answer: c
Explanation: The #ifdef preprocessor directive is used to check if a particular identifier is currently defined or not. If the particular identifier is defined, the statements following this preprocessor directive are executed till another preprocessor directive #endif is encountered. #endif is used to end the scope of #ifdef.

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

advertisement
#include<stdio.h>
void main()
{
    #ifndef max
    printf("hello");
    #endif
    printf("hi");
}

a) hello
b) hellohi
c) error
d) hi
View Answer

Answer: b
Explanation: The code shown above illustrates the preprocessor directive #ifndef. If the identifier checked is not defined, then the statements following #ifndef are executed. Here, since the identifier max is not defined, the statement after #ifndef is executed. Hence the output is: hellohi
advertisement

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

#include<stdio.h>
#define san 557
main()
{
    #ifndef san
    printf("yes");
    #endif
    printf("no");
}

a) error
b) yes
c) no
d) yesno
View Answer

Answer: c
Explanation: The output to the above code will be no. Since we have used the preprocessor directive, #ifndef and defined the identifier san, “yes” is not printed. Hence only “no” is printed as output.
advertisement

5. The preprocessor directive which checks whether a constant expression results in a zero or non-zero value __________
a) #if
b) #ifdef
c) #undef
d) #ifndef
View Answer

Answer: a
Explanation: #if checks whether a constant expression results in zero or not. If the expression is a non-zero value, then the statements between #if and #endif will be executed. If the constant expression results in a zero value, the statements between #if and #endif will not be executed.

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

#include<stdio.h>
#define max 100
void main()
{
    #if(max%10)
    printf("san");
    #endif
    printf("foundry");
}

a) error
b) san
c) foundry
d) sanfoundry
View Answer

Answer: d
Explanation: The code shown above is an illustration of the preprocessor directive #if. The value of the defined identifier max is 100. On checking the condition (100510==), which is true, the statements after #if are executed till #endif is encountered. Hence the output is sanfoundry.

7. The preprocessor directive which is used to remove the definition of an identifier which was previously defined with #define?
a) #ifdef
b) #undef
c) #ifndef
d) #def
View Answer

Answer: b
Explanation: #undef is used to remove the definition of any identifier which had been previously defined in the code with #define.

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

#include<stdio.h>
#define hello 10
void main()
{
    printf("%d",hello);
    #undef hello
    printf("%d",hello);
}

a) 10
b) hello
c) error
d) 1010
View Answer

Answer: c
Explanation: Error: hello undeclared. An error is thrown when the code shown above is executed because before the second call to the printf function, the macro hello was undefined using #undef.

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

#include <stdio.h>
#define a 2
main()
{
    int r;
    #define a 5
    r=a*2;
    printf("%d",r);
}

a) 10
b) 4
c) 2
d) 5
View Answer

Answer: a
Explanation: The macro ‘a’ is redefined in the code shown above. Hence the value of a, which is initially 2, is redefined to 5. The value stored in ‘r’ is the result of the expression (a*2), which is equal to 10. Hence the output of this code will be 10.

10. What will be the output of the following C code if the value of ‘p’ is 10 and that of ‘q’ is 15?

#include<stdio.h>
int main()
{
    int p,q;
    printf("Enter two numbers\n");
    scanf("%d",&p);
    scanf("%d",&q);
    #if(4<2)
    printf("%d",p);
    #elif(2>-1)
    printf("%d",q);
    #else
    printf("bye");
    #endif
}

a) 10
b) 15
c) bye
d) error
View Answer

Answer: b
Explanation: The output of the code shown above is 15. The values given for ‘p’ and ‘q’ are 10 and 15 respectively. Since the condition given for #elif is true, the value stored in ‘q’ will be printed, that is 15.

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