In C, what is the purpose of the 'static' keyword when used with a global variable?
ATo make it accessible only within the current file
BTo allocate it on the stack
CTo make it a constant
DTo prevent memory deallocation
Correct Answer:
A. To make it accessible only within the current file
EXPLANATION
When 'static' is used with a global variable, it restricts its scope to the current file (internal linkage), making it not accessible from other files.
What will be the output of the following code?
#include
int main() {
int x = 10, y = 20;
int *p = &x, *q = &y;
*p = *q;
printf("%d %d", x, y);
return 0;
}
A10 20
B20 20
C10 10
D20 10
Correct Answer:
B. 20 20
EXPLANATION
*p = *q assigns the value of y (20) to x through the pointer p. So x becomes 20, y remains 20.
What will be printed by the following code?
#include
int main() {
float f = 0.1 + 0.2;
if(f == 0.3)
printf("Equal");
else
printf("Not Equal");
return 0;
}
AEqual
BNot Equal
CCompiler Error
DRuntime Error
Correct Answer:
B. Not Equal
EXPLANATION
Due to floating-point precision limitations, 0.1 + 0.2 does not exactly equal 0.3 in binary representation.