50 C Language MCQs with Answers
Question 1 |
#include "stdio.h"
int main()
{
int x, y = 5, z = 5;
x = y == z;
printf("%d", x);
getchar();
return 0;
}0 | |
1 | |
5 | |
Compiler Error |
Discuss it
The crux of the question lies in the statement x = y==z. The operator == is executed before = because precedence of comparison operators (<=, >= and ==) is higher than assignment operator =.
The result of a comparison operator is either 0 or 1 based on the comparison result. Since y is equal to z, value of the expression y == z becomes 1 and the value is assigned to x via the assignment operator.
Question 2 |
C
#include <stdio.h>
// Assume base address of "GeeksQuiz" to be 1000
int main()
{
printf(5 + "GeeksQuiz");
return 0;
}
GeeksQuiz | |
Quiz | |
1005 | |
Compile-time error |
Discuss it
printf is a library function defined under stdio.h header file. The compiler adds 5 to the base address of the string through the expression 5 + "GeeksQuiz" . Then the string "Quiz" gets passed to the standard library function as an argument.
Question 3 |
C
#include <stdio.h>
int main()
{
int i = 3;
printf("%d", (++i)++);
return 0;
}
What is the output of the above program?
3 | |
4 | |
5 | |
Compile-time error |
Discuss it
In C, prefix and postfix operators need l-value to perform operation and return r-value. The expression (++i)++ when executed increments the value of variable i(i is a l-value) and returns r-value. The compiler generates the error(l-value required) when it tries to post-incremeny the value of a r-value.
Question 4 |
C
#include <stdio.h>
#if X == 3
#define Y 3
#else
#define Y 5
#endif
int main()
{
printf("%d", Y);
return 0;
}
What is the output of the above program?
3 | |
5 | |
3 or 5 depending on value of X | |
Compile time error |
Discuss it
In the first look, the output seems to be compile-time error because macro X has not been defined. In C, if a macro is not defined, the pre-processor assigns 0 to it by default. Hence, the control goes to the conditional else part and 5 is printed. See the next question for better understanding.
Question 5 |
Consider the following C declaration
C
struct {
short s[5];
union {
float y;
long z;
}u;
} t;
Assume that objects of the type short, float and long occupy 2 bytes, 4 bytes and 8 bytes, respectively. The memory requirement for variable t, ignoring alignment considerations, is (GATE CS 2000)
22 bytes | |
14 bytes | |
18 bytes | |
10 bytes |
Discuss it
Short array s[5] will take 10 bytes as size of short is 2 bytes.
When we declare a union, memory allocated for the union is equal to memory needed for the largest member of it, and all members share this same memory space. Since u is a union, memory allocated to u will be max of float y(4 bytes) and long z(8 bytes). So, total size will be 18 bytes (10 + 8).
Question 6 |
In C, parameters are always
Passed by value | |
Passed by reference | |
Non-pointer variables are passed by value and pointers are passed by reference | |
Passed by value result |
Discuss it
In C, function parameters are always passed by value. Pass-by-reference is simulated in C by explicitly passing pointer values.
Question 7 |
Which of the following is true about return type of functions in C?
Functions can return any type | |
Functions can return any type except array and functions | |
Functions can return any type except array, functions and union | |
Functions can return any type except array, functions, function pointer and union |
Discuss it
In C, functions can return any type except arrays and functions. We can get around this limitation by returning pointer to array or pointer to function.
Question 8 |
Which of the following is not a storage class specifier in C?
auto | |
register | |
static | |
extern | |
volatile |
Discuss it
volatile is not a storage class specifier. volatile and const are type qualifiers.
Question 9 |
Which of the following is not a logical operator?
&& | |
! | |
|| | |
| |
Discuss it
&&- Logical AND
!- Logical NOT
||- Logical OR
|- Bitwise OR(used in bitwise manipulations)
Question 10 |
Which file is generated after pre-processing of a C program?
.p | |
.i | |
.o | |
.m |
Discuss it
After the pre-processing of a C program, a .i file is generated which is passed to the compiler for compilation.


