C MCQs on “Pragma”.
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
Answer: c
Clarification: 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#pragma(1) struct test { int i; char j; }; main() { printf("%d",sizeof(struct test)); }
a) Error
b) 1
c) 4
d) 8
Answer: d
Clarification: #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
Answer: c
Clarification: 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
Answer: a
Clarification: 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
Answer: d
Clarification: 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);
Answer: a
Clarification: 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
Answer: b
Clarification: 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);
Answer: a
Clarification: __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
Answer: d
Clarification: #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#pragma GCC poison printf main() { printf(""); return 0; }
a) error is thrown
b) is printed
c) warning but no error
d) yrdnoufnas is printed
Answer: a
Clarification: 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.