🧩 Topic: Variables and Constants in C Language — GATE-Level (100 MCQs)
1.
Which of the following is a valid variable declaration in C?
A) int number_1 = 10;
B) int 1number = 10;
C) int number-1 = 10;
D) int number@1 = 10;
✅ Answer: A
Explanation:
Variable names cannot begin with digits or contain special symbols except _.
Hence number_1 is valid; 1number, number-1, and number@1 are invalid.
2.
Which of the following identifiers is invalid in C?
A) _var123
B) total_sum
C) int
D) sum_5
✅ Answer: C
Explanation:
Keywords like int, float, char cannot be used as identifiers.
3.
Which of the following statements is true about constants in C?
A) Integer constants can have decimal points
B) Character constants are enclosed in double quotes
C) Floating constants must include a decimal point
D) Constant names can start with digits
✅ Answer: C
Explanation:
A floating constant must include a decimal (e.g., 3.14, not 314).
4.
What is the output of the following code?
#include <stdio.h>
int main() {
const int a = 5;
int *p = (int*)&a;
*p = 10;
printf("%d", a);
}
A) 5
B) 10
C) Compiler error
D) Undefined behavior
✅ Answer: D
Explanation:
Modifying a const variable through a pointer results in undefined behavior, even if it compiles.
5.
What is the size (in bytes) of the long double type on a 64-bit GCC compiler?
A) 4
B) 8
C) 12
D) 16
✅ Answer: D
Explanation:
On 64-bit GCC, long double typically takes 16 bytes (compiler-dependent).
6.
Which statement correctly defines a constant pointer to an integer?
A) int const *p;
B) int * const p;
C) const int * const p;
D) int p const *;
✅ Answer: B
Explanation:int * const p; → the pointer p cannot change, but the integer it points to can.
Option C makes both pointer and data constant.
7.
Which of these correctly declares a symbolic constant?
A) symbol PI = 3.14;
B) #define PI 3.14
C) PI = 3.14;
D) const define PI 3.14;
✅ Answer: B
Explanation:#define is used to declare symbolic constants (macro constants) during preprocessing.
8.
What will be the output?
#include <stdio.h>
int main() {
int a = 5;
{
int a = 10;
printf("%d ", a);
}
printf("%d", a);
}
A) 5 5
B) 10 10
C) 10 5
D) 5 10
✅ Answer: C
Explanation:
Inner a shadows outer a.
First prints inner 10, then outer 5.
9.
Which of the following is not a valid C variable declaration?
A) int _temp;
B) float rate1;
C) char *name;
D) double %value;
✅ Answer: D
Explanation:
C variable names cannot contain special characters like %.
10.
Which keyword is used to define variables whose value cannot be changed?
A) const
B) static
C) volatile
D) register
✅ Answer: A
Explanation:const declares a read-only variable, making its value immutable.
11.
What is the output?
#include <stdio.h>
int main() {
const int x = 5;
printf("%d", x + 2);
return 0;
}
A) 5
B) 7
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:x is a constant, but can still participate in expressions. Output = 5 + 2 = 7.
12.
What happens if you declare two variables with the same name in the same block?
A) Compiles successfully
B) Linker error
C) Compilation error
D) Undefined behavior
✅ Answer: C
Explanation:
Duplicate variable names in the same scope cause a compile-time error.
13.
Which of the following constants is invalid in C?
A) 012
B) 0x2A
C) 08
D) 'A'
✅ Answer: C
Explanation:08 is invalid — leading 0 means octal constant, and 8 is not valid in octal.
14.
What is the default type of a numeric constant like 12.0 in C?
A) float
B) double
C) long double
D) int
✅ Answer: B
Explanation:
In C, floating-point literals like 12.0 are of type double by default.
15.
Which of these constants represents an integer literal of type long?
A) 123L
B) 123.0
C) 0123
D) '123'
✅ Answer: A
Explanation:L or l suffix denotes a long integer constant.
16.
What is the output?
#include <stdio.h>
int main() {
int a = 5;
const int b = a + 2;
printf("%d", b);
}
A) 5
B) 7
C) Compiler error
D) Garbage
✅ Answer: B
Explanation:b is a runtime const initialized with expression a+2. Value = 7.
17.
Which is true about volatile variables?
A) Stored in register memory
B) Optimized aggressively by compiler
C) May change unexpectedly (e.g., by hardware)
D) Must be constant
✅ Answer: C
Explanation:volatile tells compiler the value may change unexpectedly (e.g., sensor input, interrupt variable).
18.
Which of these constants is valid?
A) 'AB'
B) '\n'
C) "A"
D) '\x'
✅ Answer: B
Explanation:
Character constants must have a single character or valid escape sequence. 'AB' is invalid.
19.
What is the correct range of unsigned char in C (8-bit system)?
A) 0 to 127
B) -128 to 127
C) 0 to 255
D) -255 to 255
✅ Answer: C
Explanation:
Unsigned 8-bit integer stores values 0–255.
20.
Which of these declarations is illegal?
A) int a = 10;
B) const int a = 20;
C) register float a = 5.6;
D) volatile char b = 'A';
✅ Answer: C
Explanation:
You cannot declare register variables of type float or double — only simple data types are allowed.
21.
Which constant suffix indicates unsigned long long?
A) u
B) ll
C) ull
D) uL
✅ Answer: C
Explanation:
Suffix ULL or ull indicates an unsigned long long constant.
22.
What will be the output?
#include <stdio.h>
int main() {
const int a = 10;
int b = a * 2;
printf("%d", b);
}
A) 10
B) 20
C) Compiler error
D) Garbage value
✅ Answer: B
Explanation:
Constants can be used in expressions — result is 20.
23.
Which of the following correctly declares a global constant variable?
A) const int MAX;
B) int const MAX = 100;
C) int MAX = const 100;
D) const int MAX == 100;
✅ Answer: B
Explanation:int const MAX = 100; or const int MAX = 100; are valid constant declarations.
24.
Which of these cannot be modified after initialization?
A) const variable
B) static variable
C) extern variable
D) register variable
✅ Answer: A
Explanation:const variables are immutable after initialization.
25.
What is the output?
#include <stdio.h>
int x = 10;
int main() {
printf("%d", x);
{
int x = 20;
printf(" %d", x);
}
printf(" %d", x);
}
A) 10 10 20
B) 10 20 10
C) 20 10 10
D) 20 20 10
✅ Answer: B
Explanation:
Inner variable x shadows outer x → prints 10 20 10.
26.
What will be the output?
#include <stdio.h>
int main() {
static int a = 5;
a++;
printf("%d", a);
return 0;
}
A) 5
B) 6
C) Garbage
D) Compiler error
✅ Answer: B
Explanation:static variables preserve their value across function calls. Initial value = 5 → incremented → prints 6.
27.
Which storage class gives a variable internal linkage by default?
A) auto
B) register
C) static
D) extern
✅ Answer: C
Explanation:static variables have internal linkage, i.e., they are visible only within the file.
28.
Which of the following statements is true?
A) Constants can appear on the left-hand side of assignment
B) Variables must be initialized during declaration
C) register variables are stored in CPU registers (if possible)
D) auto variables have external linkage
✅ Answer: C
Explanation:register variables suggest storing in CPU registers for faster access (compiler-dependent).
29.
What is the output?
#include <stdio.h>
int main() {
int x = 10;
{
extern int x;
printf("%d", x);
}
}
A) 10
B) 0
C) Linker error
D) Garbage value
✅ Answer: C
Explanation:extern int x; inside block expects a global variable, not local → linker error (undefined reference).
30.
Which variable type must be initialized before use?
A) auto
B) static
C) global
D) const
✅ Answer: D
Explanation:const variables must be initialized at declaration since they can’t be modified later.
31.
Which of these is not a valid constant expression?
A) #define MAX 10
B) const int x = 5;
C) int y = x * 2;
D) int z = MAX + 2;
✅ Answer: C
Explanation:
Since x is a const but initialized at runtime, x * 2 isn’t a compile-time constant.
32.
What is the output?
#include <stdio.h>
void fun() {
static int count = 3;
printf("%d ", count);
count--;
}
int main() {
for(int i = 0; i < 3; i++) fun();
}
A) 3 3 3
B) 3 2 1
C) 1 2 3
D) 3 2 2
✅ Answer: B
Explanation:static variable retains its value → decremented each call → prints 3 2 1.
33.
Which of the following is an invalid constant declaration?
A) const int x = 5;
B) int const y = 10;
C) #define Z = 8;
D) #define W 9
✅ Answer: C
Explanation:
In macros, no = is used. Correct form → #define Z 8.
34.
Which keyword declares a variable that’s accessible across multiple files?
A) auto
B) register
C) extern
D) static
✅ Answer: C
Explanation:extern keyword gives external linkage, allowing use across files.
35.
What happens if you don’t initialize a static variable?
A) Garbage value
B) 0
C) Random negative value
D) Compiler error
✅ Answer: B
Explanation:
Static/global variables are automatically initialized to zero by default.
36.
Which of these constants is valid?
A) '12'
B) 'A'
C) "B"
D) ‘C’
✅ Answer: B
Explanation:
Character constants must be single character within single quotes. 'A' is valid.
37.
What is the output?
#include <stdio.h>
int main() {
int x = 5;
{
int x = x + 1;
printf("%d", x);
}
}
A) 6
B) 5
C) Garbage
D) Compiler error
✅ Answer: D
Explanation:x = x + 1 uses the uninitialized inner x, leading to compile-time error “use of undeclared variable”.
38.
Which type of variable has a lifetime limited to the function but retains value between calls?
A) auto
B) register
C) static
D) extern
✅ Answer: C
Explanation:static variables preserve their value between calls.
39.
Which keyword ensures a variable is always fetched from memory and not optimized?
A) const
B) volatile
C) static
D) register
✅ Answer: B
Explanation:volatile prevents compiler optimization — variable value is always read from memory.
40.
What is the output?
#include <stdio.h>
const int x = 10;
int main() {
int x = 20;
printf("%d", x);
}
A) 10
B) 20
C) Compiler error
D) Undefined
✅ Answer: B
Explanation:
Local variable shadows the global constant → prints 20.
41.
Which statement about scope is incorrect?
A) Global variables are accessible everywhere after declaration.
B) Static local variables have function-level scope.
C) Auto variables have block-level scope.
D) Extern variables have block-level scope.
✅ Answer: D
Explanation:extern variables have file/global scope, not block-level.
42.
What will be the output?
#include <stdio.h>
int main() {
const int x = 4;
printf("%d", ++x);
}
A) 4
B) 5
C) Compiler error
D) Undefined
✅ Answer: C
Explanation:
Incrementing a const variable is illegal — compiler error.
43.
Which constant is of type float?
A) 12.0
B) 12.0F
C) 12L
D) 12.0L
✅ Answer: B
Explanation:
The suffix F or f makes it a float constant.
44.
What is the default storage class for local variables?
A) auto
B) register
C) static
D) extern
✅ Answer: A
Explanation:
Local variables are auto by default.
45.
Which of these declarations is incorrect?
A) auto int x;
B) register int x;
C) extern int x;
D) static const int;
✅ Answer: D
Explanation:const variable must be initialized; missing value → error.
46.
What is the output?
#include <stdio.h>
int main() {
static int x;
printf("%d", x);
}
A) Garbage
B) 0
C) Compiler error
D) Undefined
✅ Answer: B
Explanation:
Uninitialized static variables are initialized to 0.
47.
Which keyword is used to define a variable that cannot change its memory address?
A) const
B) static
C) volatile
D) register
✅ Answer: D
Explanation:register variables may not have a memory address accessible via pointers.
48.
Which variable can be declared inside a function but used outside it with extern?
A) static
B) auto
C) register
D) None
✅ Answer: D
Explanation:
Local variables (auto/register/static) cannot be accessed externally.
49.
What is the output?
#include <stdio.h>
void fun() {
static int x = 0;
x++;
printf("%d ", x);
}
int main() {
for(int i=0;i<3;i++) fun();
}
A) 1 1 1
B) 0 1 2
C) 1 2 3
D) 3 2 1
✅ Answer: C
Explanation:
Static variable retains increment → prints 1 2 3.
50.
Which of the following is a correct declaration?
A) volatile const int a = 10;
B) const volatile int a = 10;
C) Both A and B
D) None
✅ Answer: C
Explanation:
Order doesn’t matter; both valid. Declares a constant volatile integer.
51.
Which of the following statements about constants in C is correct?
A) Constants occupy memory by default.
B) Macros declared with #define occupy memory.
C) Both A and B
D) None of the above
✅ Answer: D
Explanation:
Compile-time constants like #define don’t occupy memory; they’re replaced by the preprocessor.const variables may occupy memory if used as variables.
52.
What is the output?
#include <stdio.h>
int main() {
const int a = 5;
int *p = (int *)&a;
*p = 7;
printf("%d", a);
}
A) 5
B) 7
C) Compiler error
D) Undefined behavior
✅ Answer: D
Explanation:
Modifying a const variable through a pointer is undefined behavior — may print 5 or 7 depending on optimization.
53.
Which of these declarations is valid?
A) int const* p;
B) const int* p;
C) int * const p;
D) All of the above
✅ Answer: D
Explanation:
All are valid:
const int* p: constant dataint * const p: constant pointerint const* p: same asconst int*.
54.
Which constant suffix denotes a long double literal?
A) L
B) D
C) LD
D) DL
✅ Answer: A
Explanation:
The suffix L or l specifies long double (e.g., 12.34L).
55.
What is the output?
#include <stdio.h>
int main() {
const int n = 5;
int arr[n] = {1,2,3,4,5};
printf("%d", arr[4]);
}
A) 4
B) 5
C) Compiler error
D) Undefined
✅ Answer: C
Explanation:
C89 does not allow variable-length arrays with const variables; n is not compile-time constant.
(C99 and later may allow it).
56.
Which of the following is NOT a valid way to declare a constant?
A) #define MAX 10
B) const int MAX = 10;
C) #const MAX 10
D) None
✅ Answer: C
Explanation:#const is invalid. Only #define or const are valid methods.
57.
What is the output?
#include <stdio.h>
#define VALUE 10+10
int main() {
int x = VALUE * VALUE;
printf("%d", x);
}
A) 400
B) 100
C) 120
D) 30
✅ Answer: C
Explanation:
Macro expands to → int x = 10+10 * 10+10;
→ operator precedence → 10 + (10*10) + 10 = 120.
58.
How can you avoid macro expansion ambiguity as in Q57?
A) Avoid macros
B) Use const variables instead
C) Enclose macros in parentheses
D) Use preprocessor conditionals
✅ Answer: C
Explanation:#define VALUE (10+10) avoids ambiguity → expands correctly as (10+10)*(10+10).
59.
Which variable type retains its value between function calls?
A) static
B) extern
C) register
D) volatile
✅ Answer: A
Explanation:static variables preserve their values across multiple calls.
60.
What is the output?
#include <stdio.h>
void display() {
static int a = 0;
a += 5;
printf("%d ", a);
}
int main() {
display();
display();
display();
}
A) 5 5 5
B) 0 5 10
C) 5 10 15
D) 15 10 5
✅ Answer: C
Explanation:a retains its previous value; increments cumulatively → prints 5 10 15.
61.
Which of the following can be declared without initialization?
A) const variable
B) static variable
C) extern variable
D) register variable
✅ Answer: C
Explanation:extern only declares a variable defined elsewhere — doesn’t require initialization.
62.
What is the lifetime of an automatic (auto) variable?
A) Entire program
B) Until end of file
C) Until function returns
D) Forever once initialized
✅ Answer: C
Explanation:auto variables exist only while the function executes.
63.
Which storage class keyword suggests fast access?
A) static
B) register
C) extern
D) auto
✅ Answer: B
Explanation:register hints compiler to store variable in CPU registers.
64.
What happens if you take the address of a register variable?
A) Returns valid address
B) Always 0
C) Compiler error
D) Undefined
✅ Answer: C
Explanation:register variables may not have an address in memory — taking address causes error.
65.
What is the output?
#include <stdio.h>
int x = 10;
void fun() {
extern int x;
printf("%d ", x);
}
int main() {
fun();
}
A) 0
B) 10
C) Compiler error
D) Undefined
✅ Answer: B
Explanation:extern accesses the global variable x declared outside all functions → prints 10.
66.
Which of these declarations will fail?
A) const int *ptr;
B) int * const ptr;
C) const int * const ptr;
D) int const *ptr = &5;
✅ Answer: D
Explanation:
Cannot take address of a literal; &5 is invalid.
67.
What will be the output?
#include <stdio.h>
int main() {
const int i = 10;
printf("%d", sizeof(i++));
}
A) 4
B) 10
C) Compiler error
D) Undefined
✅ Answer: A
Explanation:sizeof is evaluated at compile time; i++ is not executed. Result is size of int = 4 bytes.
68.
Which of these is a valid constant declaration?
A) int const 10 = x;
B) const int val = 10;
C) constant int x = 5;
D) define int x 10;
✅ Answer: B
Explanation:const int val = 10; is correct. Others are syntactically wrong.
69.
What will be the output?
#include <stdio.h>
#define VALUE 5
int main() {
const int VALUE = 10;
printf("%d", VALUE);
}
A) 5
B) 10
C) Compiler error
D) Undefined
✅ Answer: B
Explanation:
Preprocessor replaces VALUE before compile, but local variable VALUE shadows it in code block → prints 10.
70.
Which variable cannot change during program execution?
A) volatile variable
B) const variable
C) extern variable
D) auto variable
✅ Answer: B
Explanation:const variables are immutable after initialization.
71.
What will happen?
#include <stdio.h>
int main() {
const float PI;
printf("%f", PI);
}
A) Prints 0.000000
B) Compiler error
C) Garbage value
D) 3.141593
✅ Answer: B
Explanation:const variables must be initialized at declaration — missing value → error.
72.
Which is true about macros?
A) Handled by preprocessor
B) Type-checked by compiler
C) Stored in symbol table
D) Evaluated at runtime
✅ Answer: A
Explanation:
Macros are expanded before compilation by the preprocessor.
73.
Which is true for static global variables?
A) Visible only inside the same file
B) Accessible from other files
C) Has block-level scope
D) Disappears after function exits
✅ Answer: A
Explanation:static global → file scope, internal linkage.
74.
What is the output?
#include <stdio.h>
int x = 5;
int main() {
int x = x;
printf("%d", x);
}
A) 5
B) 0
C) Garbage value
D) Compiler error
✅ Answer: C
Explanation:
Local variable x shadows global one → uninitialized local variable → garbage.
75.
Which type qualifier prevents compiler optimization?
A) const
B) volatile
C) static
D) extern
✅ Answer: B
Explanation:volatile indicates variable may change unexpectedly — prevents optimization.
76.
Which of the following constants is invalid?
A) 0xAF
B) 078
C) 0123
D) 'B'
✅ Answer: B
Explanation:078 invalid — leading 0 = octal constant; 8 not valid in octal.
77.
Which of these gives a compiler error?
A) int const *ptr;
B) const int * const ptr;
C) int * const ptr;
D) const *int ptr;
✅ Answer: D
Explanation:const *int is invalid syntax. const must precede or follow type.
78.
Which variable will store its value in text (code) segment?
A) const variable
B) static variable
C) auto variable
D) extern variable
✅ Answer: A
Explanation:const literals often reside in read-only text segment, depending on compiler optimization.
79.
What is the default initial value of a global variable?
A) Garbage
B) 0
C) 1
D) Compiler dependent
✅ Answer: B
Explanation:
All global/static variables are initialized to zero by default.
80.
Which keyword can be used with both variable and function?
A) static
B) const
C) register
D) volatile
✅ Answer: A
Explanation:static can modify both variables (preserve lifetime) and functions (limit linkage).
81.
What is the output?
#include <stdio.h>
int main() {
const char c = 'A';
printf("%d", c);
}
A) 65
B) 97
C) Compiler error
D) Undefined
✅ Answer: A
Explanation:
Character constants are stored as ASCII integer values — 'A' = 65.
82.
Which statement is false?
A) Global variables are stored in data segment.
B) Static variables are stored in stack.
C) Auto variables are stored in stack.
D) Const literals are in code segment.
✅ Answer: B
Explanation:static variables reside in data segment, not stack.
83.
Which of the following constants represents a hexadecimal value?
A) 0x1F
B) 019
C) 15
D) '1F'
✅ Answer: A
Explanation:
Prefix 0x indicates hexadecimal constant.
84.
What is the output?
#include <stdio.h>
#define VAL 5*5
int main() {
printf("%d", VAL + VAL);
}
A) 25
B) 50
C) 55
D) 30
✅ Answer: C
Explanation:
Expands to 5*5+5*5 = 25 + 25 = 50 ✅ (Oops! Correction)
✅ Answer: B (Corrected).
85.
Which keyword is used to define a constant variable that can change due to external factors?
A) static
B) const
C) volatile
D) register
✅ Answer: C
Explanation:volatile ensures compiler always reads actual memory value.
86.
Which of these storage classes cannot be used with const?
A) auto
B) static
C) extern
D) register
✅ Answer: D
Explanation:const register is invalid because address of register variable cannot be taken.
87.
Which of these variables are automatically initialized?
A) static
B) extern
C) global
D) All of these
✅ Answer: D
Explanation:
All (static/global/extern) are initialized to 0 automatically.
88.
Which of these constants denotes octal 10?
A) 010
B) 0x10
C) 10
D) A
✅ Answer: A
Explanation:
Leading 0 = octal → decimal 8.
89.
Which statement about const and #define is correct?
A) Both occupy memory
B) Both are runtime constants
C) const is type-safe, #define is not
D) #define is evaluated at runtime
✅ Answer: C
Explanation:const follows type rules, #define is text replacement — not type-checked.
90.
Which storage class allocates memory at compile-time?
A) auto
B) static
C) register
D) volatile
✅ Answer: B
Explanation:static memory is allocated once, at compile time.
91.
Which of these is not a valid declaration?
A) int const *ptr;
B) int const ptr;
C) const int *ptr;
D) int * const ptr;
✅ Answer: B
Explanation:int const ptr; missing * — invalid declaration.
92.
What will be the output?
#include <stdio.h>
#define X 10
int main() {
int arr[X];
printf("%d", sizeof(arr)/sizeof(arr[0]));
}
A) 0
B) 10
C) Compiler error
D) Undefined
✅ Answer: B
Explanation:
Macro X expands to 10 → array of size 10 → prints 10.
93.
Which of the following statements is true?
A) const ensures variable cannot be modified.
B) volatile ensures compiler optimization.
C) register ensures variable is global.
D) auto variables are initialized automatically.
✅ Answer: A
Explanation:const makes variable read-only. Others are incorrect.
94.
Which variable is visible throughout the program but defined only once?
A) static
B) extern
C) const
D) auto
✅ Answer: B
Explanation:extern allows sharing single global definition across files.
95.
Which constant type suffix is invalid?
A) u
B) L
C) ll
D) ld
✅ Answer: D
Explanation:ld is invalid; L (long double) or LL (long long) are valid.
96.
What is the default type of a character constant like 'A'?
A) char
B) int
C) short
D) unsigned char
✅ Answer: B
Explanation:
Character constants are of type int in C, not char.
97.
Which variable storage class cannot be used outside a function?
A) static
B) auto
C) extern
D) const
✅ Answer: B
Explanation:auto variables exist only within a function block.
98.
What happens when you modify a string literal?
char *str = "hello";
str[0] = 'H';
A) Changes string successfully
B) Compiler error
C) Segmentation fault (undefined)
D) Prints nothing
✅ Answer: C
Explanation:
String literals are stored in read-only memory — modifying → undefined behavior.
99.
Which constant represents a single character?
A) 'A'
B) "A"
C) 'AB'
D) "AB"
✅ Answer: A
Explanation:
Single quotes → single character constant. Double quotes → string literal.
100.
What is the result of the following?
#include <stdio.h>
#define SIZE 5
int main() {
const int size = 10;
printf("%d", SIZE + size);
}
A) 5
B) 10
C) 15
D) Compiler error
✅ Answer: C
Explanation:
Macro SIZE replaced by 5 → expression becomes 5 + 10 = 15.