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
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?
#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
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.
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
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
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.
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
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
Explanation: The correct syntax of the attribute packed is: __attribute__((packed));
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
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
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
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
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.
- Apply for C Internship
- Practice Computer Science MCQs
- Check Computer Science Books
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship