C Questions and Answers – Pragma

This set of C Multiple Choice Questions & Answers (MCQs) focuses on “Pragma”.

Pre-requisite for this C MCQ set: Advanced C Programming Video Tutorial.

1. The preprocessor directive used to give additional information to the compiler, beyond which is conveyed in the language _____________
a) #include
b) #define
c) #pragma
d) #elif
View Answer

Answer: c
Explanation: The preprocessor directive #pragma is used to give additional information to the compiler, other than what is conveyed in the language itself.

2. What will be the output of the following C code, if it is run on a 32 bit platform?

advertisement
advertisement
#include<stdio.h>
#pragma(1)
struct test
{
    int i;
    char j;
};
main()
{
    printf("%d",sizeof(struct test));
}

a) Error
b) 1
c) 4
d) 8
View Answer

Answer: d
Explanation: #pragma pack(n), where n is the number of alignment in bytes. #pragma pack(1) is the directive for the compiler to pack the structure.
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

3. In the directive, #pragma pack(n), which of the following is not a valid value of n?
a) 1
b) 2
c) 3
d) 4
View Answer

Answer: c
Explanation: Valid arguments are 1,2,4 and 8. 3 is not a valid value for n.

4. Which of the following attributes is used to specify that the minimum required memory to be used to represent the types?
a) packed
b) aligned
c) unused
d) deprecated
View Answer

Answer: a
Explanation: The keyword __attribute__ allows you to specify special attributes of struct type. 6 attributes are currently defined for types: aligned, packed, transparent_union, unused, deprecated and may_alias. The attribute “packed” is used to specify that the minimum required memory to be used to represent the types.
advertisement

5. In the directive #pragma pack(n), if the value of ‘n’ is given to be 5, then what happens?
a) Error
b) Warning but no error
c) Executes the pragma statement
d) Ignores the pragma statement and executes the program
View Answer

Answer: d
Explanation: Valid values for n are 1,2,4 and 8. If the value of n is one that the compiler does not recognize, then it simply ignores the pragma statement without throwing any error or warning.

6. The correct syntax of the attribute packed is _________
a) __attribute__((packed));
b) _attribute(packed);
c) _attribute_((packed));
d) __attribute__(packed);
View Answer

Answer: a
Explanation: The correct syntax of the attribute packed is: __attribute__((packed));
advertisement

7. The pragma ___________________ is used to remove an identifier completely from a program.
a) GNU piston
b) GCC poison
c) GNU poison
d) GCC piston
View Answer

Answer: b
Explanation: There are several pragmas defines. One such pragma is GCC poison which is used to remove an identifier.

8. The function of __attribute__((packed)); can also be performed using _________
a) #pragma pack(1);
b) #pragma pack(2);
c) #pragma pack(4);
d) #pragma pack(8);
View Answer

Answer: a
Explanation: __attribute((packed)); and #pragma(1) are used to perform the same function, that is, to direct the compiler to pack the structure.

9. #pragma GCC poison should be followed by a list of identifiers that are _________
a) even in number
b) odd in number
c) valid
d) invalid
View Answer

Answer: d
Explanation: #pragma poison GCC is used to remove an identifier from a program. It should be followed by a list of identifiers which are not valid in the program, for example: scanf, printf etc.

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

#include<stdio.h>
#pragma GCC poison printf
main()
{
    printf("sanfoundry");
    return 0;
}

a) error is thrown
b) sanfoundry is printed
c) warning but no error
d) yrdnoufnas is printed
View Answer

Answer: a
Explanation: The code shown above results in an error: attempt to use poisoned printf
When the above program is compiled, it results in an error since #pragma was used to specify that the identifier printf should not be used in the program.

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.