Declarations and Initializations in C Programming MCQ Questions and Answers

1. Which of the following is a declaration that also defines a variable in C?
A) extern int x;
B) int x;
C) extern int x; /* in another file */
D) int;
Answer: B

2. What is the value of a global variable with static storage that is not explicitly initialized?
A) Indeterminate
B) Garbage value
C) Zero (all-bits-zero)
D) Compiler error
Answer: C

3. Which storage class specifier gives a variable internal linkage when used at file scope?
A) extern
B) auto
C) static
D) register
Answer: C

4. Which of the following local variables is guaranteed to be initialized to zero by the runtime?
A) int a; inside a function
B) static int a; inside a function
C) int a = 0; inside a function
D) int *p; inside a function
Answer: B

5. Which is a valid declaration of an integer array of size 10 with all elements initialized to 0 using an initializer?
A) int a[10] = {0};
B) int a[10] = 0;
C) int a[10] = { };
D) int a[10] = NULL;
Answer: A

6. If you declare int a[] = {1,2,3};, what is the size of a?
A) 0
B) 2
C) 3
D) Undefined at runtime
Answer: C

7. Which of the following is true about extern declaration?
A) It allocates storage for the variable.
B) It specifies that the variable is defined in another translation unit.
C) It initializes the variable to zero.
D) It makes the variable local to the current function.
Answer: B

8. What happens if you write int x; int x; at global scope in the same translation unit (file)?
A) Compile-time error: multiple definitions
B) Linker error: multiple symbols
C) Allowed — they are tentative definitions and treated as single definition if one definition or initializer appears later.
D) Runtime warning
Answer: C

9. Which of the following initializers is allowed for a const object at block scope in C?
A) const int x;
B) const int x = 5;
C) const int x; x = 10;
D) const int x = /* nothing */;
Answer: B

10. Which initialization is correct for a pointer to an integer?
A) int *p = 5;
B) int *p = &5;
C) int x; int *p = &x;
D) int *p = NULL;
Answer: C

11. What is the value of an uninitialized automatic (non-static) local variable?
A) Zero
B) NULL
C) Indeterminate (garbage)
D) Compiler sets to -1
Answer: C

12. Which of the following is a valid designated initializer for an array in C99 and later?
A) int a[5] = { [2] = 10 };
B) int a[5] = { .2 = 10 };
C) int a[5] = { #2 = 10 };
D) int a[5] = { 2:10 };
Answer: A

13. Which of the following initializations of a string in C creates an array of size 6?
A) char s[] = “hello”;
B) char s[5] = “hello”;
C) char *s = “hello”;
D) char s[] = {‘h’,’e’,’l’,’l’,’o’};
Answer: A

14. What does the initializer int a[5] = {1, 2}; produce for elements a[2] to a[4]?
A) Indeterminate values
B) Zero initialized
C) Leftover memory garbage
D) Compile-time error
Answer: B

15. Which declaration both declares and defines a function named f?
A) extern void f();
B) void f();
C) void f() { /* body */ }
D) static void f;
Answer: C

16. Which of these tells the compiler “this variable may change at any time” and affects optimizations but not initialization?
A) volatile
B) register
C) static
D) const
Answer: A

17. Which is an invalid initializer in standard C for an automatic scalar (non-static) variable?
A) int i = 3;
B) int i = i + 1;
C) int i = (1,2);
D) int i = (int)3;
Answer: B

18. In C, which of the following is true about register?
A) It guarantees the variable is stored in CPU register.
B) It prohibits taking the address of the variable with &.
C) It allows initialization only with constant expressions.
D) It changes linkage to internal.
Answer: B

19. What is the result of static int x = 5; declared inside a function?
A) x is allocated on the stack each call
B) x retains its value between function calls
C) x is an alias for a global variable x
D) x cannot be initialized inside a function
Answer: B

20. How does the compiler treat an object with file scope declared const without extern in C?
A) It has external linkage by default.
B) It has internal linkage by default.
C) It cannot be used from another file unless declared volatile.
D) It becomes modifiable at runtime.
Answer: B

21. Which of the following statements is true about initializing a struct in C?
A) All members not explicitly initialized are left with indeterminate values.
B) All members not explicitly initialized are initialized to zero.
C) Structs cannot be initialized at declaration.
D) Only the first member may be initialized.
Answer: B

22. Which one correctly initializes a two-dimensional array partially?
A) int m[2][3] = { {1}, {4,5} };
B) int m[2][3] = { 1, {4,5} };
C) int m[2][3] = { [0][0]=1, [1][0]=4, [1][1]=5 };
D) Both A and C are correct.
Answer: D

23. Consider extern int x = 5; at global scope. What does this do?
A) Declares x external but also defines and initializes it in this translation unit.
B) Is illegal — extern cannot have initializer.
C) Only declares that x is defined elsewhere.
D) Makes x a register variable.
Answer: A

24. What is the type of p in char *p = “text”;?
A) char[]
B) const char * in C++ but char * in C pointing to string literal (modifiable? undefined behaviour if modified)
C) char **
D) char
Answer: B

25. Which initialization is required for an object with static storage duration?
A) It must be initialized by programmer.
B) If not initialized, it is zero-initialized.
C) It is initialized to random memory.
D) It is initialized at runtime to an implementation-defined value.
Answer: B

26. Which initialization syntax is valid for a structure struct S { int a; float b; };?
A) struct S s = { .b = 2.5, .a = 1 };
B) struct S s = { 1, 2.5 };
C) struct S s; s = {1, 2.5};
D) Both A and B are valid.
Answer: D

27. When you write int arr[3] = {1,2,3,4}; what happens?
A) Compiler error — too many initializers.
B) Excess initializers are ignored silently.
C) Only first three used; rest cause undefined behaviour.
D) Array grows to hold all initializers.
Answer: A

28. Which of the following is true about initialization of enum constants?
A) Enum members must be initialized with floating-point values.
B) Enum members may have explicit integer initializers and default start is 0.
C) Enums cannot specify values; they are always sequential starting at 1.
D) Enums are stored as doubles.
Answer: B

29. What does int x = {5}; do in C?
A) Syntax error
B) Initializes x to 5 using brace initialization — allowed for scalars.
C) Initializes x to 0 then assigns 5 at runtime.
D) Makes x an array.
Answer: B

30. Which of the following initializations for a pointer is safe to indicate “no object”?
A) int *p = NULL;
B) int *p = 0;
C) int *p = (void*)0;
D) All of the above are commonly used and equivalent in C.
Answer: D

31. What is the effect of extern int a; inside a function body?
A) Declares a global variable a visible from that scope.
B) Defines a local automatic a.
C) Is illegal inside a function.
D) Makes a static.
Answer: A

32. Initialization of automatic variables may use:
A) Constant expressions only.
B) Any expression (including function calls).
C) Only literals.
D) Only other constant variables.
Answer: B

33. Which phrase is correct: “Tentative definition” in C refers to?
A) A declaration without type.
B) A file-scope declaration like int x; with no initializer that may be treated as definition.
C) A forward declaration for functions.
D) A placeholder for link-time dynamic allocation.
Answer: B

34. Which of the following is true for static global variables declared inside a file?
A) They have external linkage.
B) They have internal linkage — not visible to other translation units.
C) They are reinitialized every function call.
D) They cannot be initialized.
Answer: B

35. Which of these initializations will set all members of a struct to zero?
A) struct S s = {0};
B) struct S s = {1};
C) struct S s;
D) struct S s = {}; (in standard C)
Answer: A

36. In C, the order of initialization for aggregate types (array/struct) is:
A) Left-to-right in member/element order.
B) Right-to-left.
C) Unspecified.
D) Alphabetical by member name.
Answer: A

37. What is the result when a global variable is declared static int x = 1; and another file declares extern int x;?
A) Linker error due to conflicting linkage.
B) The extern refers to the same x.
C) The extern refers to a different x (not found) — static had internal linkage so extern will fail to link.
D) Compiler automatically changes internal linkage to external.
Answer: C

38. Which of these initializers is allowed for an array of characters?
A) char s[4] = “hello”;
B) char s[6] = “hello”;
C) char s[] = ‘h’,’i’;
D) char *s = {‘h’,’i’};
Answer: B

39. Which of the following is true about const variables and initialization?
A) const objects must be initialized at the point of definition.
B) const objects can be assigned later like non-const.
C) const implies storage class static.
D) const forbids initialization.
Answer: A

40. What does int n = sizeof(int); do?
A) Initializes n with the size in bytes of int — compile-time constant.
B) Causes runtime error.
C) sizeof cannot be used in initializer.
D) Sets n to 0.
Answer: A

41. Which of the following statements about initialization is false?
A) Static objects are zero-initialized before any other initialization.
B) If you provide initializer list, it’s applied in declaration order.
C) Automatic variables are always zero-initialized by compiler.
D) Pointers not initialized may contain indeterminate values.
Answer: C

42. In C, what does int x = (int)3.9; produce?
A) Compile-time error
B) x = 4
C) x = 3 (truncation)
D) Undefined behaviour
Answer: C

43. If int a[5] = { [4] = 10 }; then what is a[0]?
A) Indeterminate
B) 10
C) 0
D) 5
Answer: C

44. Which of the following is valid initialization of an array of pointers?
A) int *p[3] = { NULL, NULL, NULL };
B) int **p = { &a, &b };
C) int *p = { &a };
D) int p[3] = { &a };
Answer: A

45. For a file-scope array declared extern int arr[];, which must be true?
A) Its size must be specified here.
B) It must be defined (with size or initializer) in another translation unit.
C) It cannot be used elsewhere.
D) It becomes a tentative definition.
Answer: B

46. What is a difference between int a = 1; at file scope and static int a = 1; at file scope?
A) No difference.
B) static gives internal linkage; without static it has external linkage.
C) static places variable on stack.
D) static forbids initialization.
Answer: B

47. Which of these initializers for a pointer to function is valid?
A) int (*f)() = &printf; (assuming matching signature)
B) int (*f)() = 5;
C) int (*f)() = “hello”;
D) int (*f)() = NULL;
Answer: A or D

48. When initializing a volatile variable, which is true?
A) Initialization rules are identical to non-volatile variables.
B) Volatile variables cannot be initialized.
C) Volatile forces compile-time constant initializers.
D) Volatile implies the object is read-only.
Answer: A

49. What happens if you write int a = sizeof(a); inside a function where a is declared as int a = sizeof(a);?
A) a is initialized with sizeof(int) because sizeof operand evaluated at compile time.
B) Undefined behaviour because a used in its own initializer.
C) a gets value of sizeof of uninitialized memory.
D) Compile error.
Answer: A

50. Which rule holds for initializing an enum in C?
A) Enum values must be unique integers.
B) Enum values can repeat; the compiler will error.
C) Enum values may be explicitly set; remaining values increment from previous.
D) Both A and C.
Answer: D

51. Which of the following is a valid combined declaration and initialization of a double array with implicit size?
A) double d[] = {1.0, 2.0};
B) double d[0] = {1.0};
C) double d[]; d = {1.0,2.0};
D) double d; d[] = {1.0,2.0};
Answer: A

52. Consider int *p = malloc(sizeof(int)); (after including stdlib.h). Is this initialization valid in C?
A) Yes, it initializes p with allocated memory pointer.
B) No — malloc returns void, needs cast.
C) No — C forbids use of malloc in initializer.
D) Only in C++ it’s valid.
Answer: A

53. What does char s[4] = “abc”; set s[3] to?
A) ‘c’
B) ‘\0’ (null terminator)
C) Indeterminate
D) ‘a’
Answer: B

54. Which of these is correct about initialization of union members in C?
A) Only the first member in initializer is set; others are not.
B) All union members are set to the same value.
C) You must initialize all members.
D) Unions cannot be initialized.
Answer: A

55. Which is true about initialization of static duration variables at program start?
A) They are zero-initialized before any dynamic initialization.
B) They are initialized in unspecified order across translation units.
C) If initialized with a constant, that happens at compile-time.
D) All of the above (A and B and C combined) — except B should be clarified.
Answer: D (A and C are true; across translation units, order of initialization of non-local static with dynamic init is undefined — so B is context-specific; best answer: D)

56. Which of these initializations causes implicit conversion?
A) int i = 3.14;
B) double d = 3;
C) char c = 65;
D) All of the above.
Answer: D

57. In C, can you declare a variable without giving its type?
A) Yes, using auto.
B) No — a type is always required.
C) Yes, the compiler infers type.
D) Only in C11.
Answer: B

58. Which initializer is valid for a variable-length array (VLA) in C99?
A) int n = 5; int a[n] = {0}; — allowed (initializer sets first element; remaining zero-initialized?)
B) VLAs cannot have initializers in standard C.
C) int a[n] = {1,2}; is always compile-time constant requirement failing.
D) VLAs must be static.
Answer: B
(Note: VLAs cannot be initialized with braces at definition in standard C; they may be defined only — initialization of VLAs with initializer is not allowed.)

59. If you have int arr[] = { [3] = 7, [0] = 1 }; which element is arr[1]?
A) 7
B) 1
C) 0
D) Indeterminate
Answer: C

60. Which of the following is a correct way to initialize a pointer to a function returning void and taking int?
A) void (*f)(int) = myfunc;
B) void f(int) = myfunc;
C) int (*f)(void) = myfunc;
D) void (*f)(int) = &myfunc;
Answer: A (D would also be valid; prefer A as simplest — both A and D acceptable; but choose A)

61. What does int a = {1,2}; do?
A) Compile error (too many initializers for scalar)
B) Initializes a to 1 and ignores rest.
C) Initializes a to 2.
D) Initializes an array.
Answer: A

62. Which of these is true about initialization of float f = 1.0f/3.0f;?
A) f is initialized at runtime with the computed floating value.
B) It must be computed at compile time.
C) It’s illegal to use arithmetic in initializer.
D) It becomes a constant expression only if const specified.
Answer: A

63. Which of the following is legal in C99 and later for initializing arrays?
A) int a[5] = {[0]=1, [2]=3};
B) int a[] = { [3] = 4 };
C) int a[5] = { [6] = 7 };
D) Both A and B are allowed; C is out-of-range and invalid.
Answer: D

64. What is true about initialization of char *s = “abc”; in C?
A) The string literal may be placed in read-only memory — modifying it is undefined behavior.
B) The pointer points to a modifiable array of characters.
C) The pointer becomes an array itself.
D) The compiler enforces constness.
Answer: A

65. Which rule applies when initializing partially a static object with an initializer list?
A) Remaining members are indeterminate.
B) Remaining members are zero-initialized.
C) Compiler error.
D) Remaining members are set to -1.
Answer: B

66. What happens if you redeclare a global variable with extern and provide an initializer in another file?
A) Linker will pick the initialized definition; other extern declarations are references.
B) Compiler error in both files.
C) All files get independent copies.
D) Initialization ignored.
Answer: A

67. Which of the following allows you to initialize a struct member by name?
A) struct S s = { .m = 10 };
B) struct S s = { m:10 }; (GCC extension)
C) struct S s = { [m] = 10 };
D) Only A is standard C.
Answer: D

68. Which of these initializers produces a compile-time constant for a static object?
A) static const int x = 5;
B) static int x = rand();
C) static int x = time(NULL);
D) static int x = printf(“hi”);
Answer: A

69. Which of the following is true about initializer lists and macros?
A) Macros expanding to initializer lists are allowed in declarations.
B) Macros cannot be used inside initializer.
C) Initializers must be hard-coded only.
D) Macros are evaluated at runtime to create initializer.
Answer: A

70. What is the default initial value of an object with static storage class but incomplete type?
A) Zero once complete type defined before program start.
B) Indeterminate because type incomplete.
C) Compile error.
D) Not allowed in C.
Answer: C

71. Which of the following is true about initializing arrays with fewer initializers than their declared size?
A) Remaining elements are filled with zero.
B) Remaining elements are left uninitialized.
C) Compiler error.
D) They are filled with 1.
Answer: A

72. Is it valid to write extern const int n = 10; at file scope?
A) Yes — it declares and defines n with external linkage.
B) No — extern and const conflict.
C) Yes — but in C const at file scope has internal linkage unless explicitly extern. So with extern it becomes external.
D) It’s a syntax error.
Answer: C

73. Which of the following initializations of pointer-to-const is valid?
A) const int *p = NULL;
B) int *const p = NULL;
C) const int *const p = &x;
D) Both A and C are valid (B is a const pointer; must be initialized with address).
Answer: D

74. What is the effect of int a[ ] = { [1] = 2, [0] = 1 };?
A) The array gets elements [0]=1, [1]=2.
B) The array becomes reversed.
C) Compile error due to out-of-order.
D) Indeterminate content.
Answer: A

75. Which of these initializations is allowed for an object with automatic storage that uses compound literal?
A) int *p = (int []){1,2,3}; (pointer to compound literal with automatic lifetime in that scope)
B) int *p = (int [3]){1,2,3};
C) Both A and B are similar and allowed (compound literal syntax).
D) Compound literals not allowed in initializer.
Answer: C

76. What is initialized by int i = 0; inside function?
A) Static storage
B) Automatic storage initialized to 0
C) Register storage only
D) Constant storage
Answer: B

77. Which of the following is correct about extern and function declarations?
A) Function declarations are extern by default.
B) You must write extern before function declarations.
C) extern forbids function definitions.
D) extern changes function to inline.
Answer: A

78. Which rule is correct about aggregation initialization with fewer elements than members in a struct?
A) Remaining members are garbage.
B) Remaining members are zero-initialized.
C) The compiler synthesizes values.
D) The struct is considered incomplete.
Answer: B

79. Which of the following initializations is allowed for a pointer to void?
A) void *p = NULL;
B) void *p = &x; where x is int x; — valid due to implicit conversion? (need explicit cast in C++)
C) const void *p = &x; also valid.
D) All of the above (in C, A and B and C are valid; B requires no cast in C because void* can hold any object pointer).
Answer: D

80. What does static int arr[] = {1,2}; at file scope ensure?
A) arr has internal linkage and is zero-terminated.
B) arr has internal linkage and storage duration static; elements beyond initializer are zero if bigger size declared.
C) arr must be visible in other translation units.
D) arr is stored on the stack.
Answer: B

81. Which of the following initializations for pointers is implementation-defined behaviour if pointer arithmetic later used with sentinel?
A) int *p = NULL;
B) int *p = malloc(0); — may return NULL or unique pointer that must not be dereferenced; behaviour implementation-defined regarding pointer arithmetic.
C) int *p = (void*)0x1;
D) int *p = &global;
Answer: B

82. What happens if you provide an initializer for an extern variable in a header and include it in multiple C files?
A) Multiple definition linker errors will occur.
B) The compiler merges them.
C) No issue.
D) The last included header wins.
Answer: A

83. Which of the following initializers produces a pointer to string literal stored in read-only memory (implementation detail)?
A) char *s = “text”;
B) const char *s = “text”;
C) char s[] = “text”;
D) Both A and B produce pointer to literal; C makes a copy in modifiable array.
Answer: D

84. Which is true for initialization of bit-field members in a struct?
A) They can be initialized by list in order like other members.
B) Bit-fields cannot be initialized.
C) Bit-field initialization requires special syntax.
D) Bit-fields always default to -1.
Answer: A

85. Which of the following definitions reserves storage but does not initialize to non-zero?
A) int x = 5;
B) extern int x; with no initializer in that translation unit (if not defined elsewhere, may be tentative)
C) int x; at file scope — tentative definition leading to zero initialization if not overridden.
D) static int x; which is zero-initialized.
Answer: C

86. If you have int a[] = {1, 2, [5] = 10}; what is the length of the array?
A) 3
B) 6 (largest index + 1)
C) 5
D) Undefined
Answer: B

87. What is the result of int *p = & (int){5}; (address of a compound literal)?
A) Pointer to temporary compound literal with lifetime of enclosing block — valid while block exists.
B) Compile-time error.
C) Undefined at initialization.
D) Points to a static object.
Answer: A

88. Which of the following declarations declares but does not define an object?
A) extern int x; (if definition elsewhere)
B) int x; at file scope (tentative definition)
C) static int x = 0;
D) int x = 10;
Answer: A

89. Which of the following initializes an array of structures?
A) struct S arr[] = { {1,2}, {3,4} };
B) struct S arr[2] = {{1,2}, {3,4}};
C) Both A and B are valid.
D) Neither is valid.
Answer: C

90. Which of these is correct: default initialization of local automatic aggregates?
A) Automatic aggregates are zero-initialized by default.
B) Automatic aggregates are uninitialized (indeterminate) unless explicitly initialized.
C) Compiler initializes them to pattern 0xFF.
D) They are static by default.
Answer: B

91. What is required when defining an array with initializer in parameterless function scope for static storage?
A) static int arr[] = {1,2,3}; — allowed; arr has static storage and initialized at program start.
B) int arr[] = {1,2,3}; — becomes automatic and initialized each call.
C) Arrays cannot be defined in function scope.
D) Must be const.
Answer: A

92. Which initialization is valid in ISO C for a pointer-to-member (C has no pointer-to-member like C++)?
A) N/A (C does not have pointer-to-member as in C++)
B) int S::*p = &S::m;
C) int (*pmem)() = NULL;
D) void *pm = &struct_member;
Answer: A

93. Which of the following is true about initialization expressions for objects with static storage duration?
A) They must be constant expressions in C89 for objects that are not runtime-initializable (but C allows compile-time constant or address of objects).
B) They cannot use function calls for initialization if compile-time required.
C) Non-constant initializer will still compile if permitted by implementation (with dynamic init).
D) All of the above (A and B and C contextually true; best summarized answer: D)
Answer: D

94. Which declaration creates a pointer to an array of 5 ints?
A) int (*p)[5];
B) int *p[5];
C) int p[5];
D) int p* [5];
Answer: A

95. Which of the following is true for designated initializers order?
A) They can appear in any order; unspecified members are zero-initialized.
B) They must be in increasing subobject order always.
C) Designated initializers are a GCC-only feature.
D) They require numeric indices only.
Answer: A

96. If int x; is declared inside a header included by multiple files, and no other file defines x, what is likely outcome using modern compilers with standard C?
A) Multiple definition errors at link time.
B) Tentative definitions collapse into a single definition in the final link (if linker supports it).
C) Linker will ignore them.
D) It will cause runtime segmentation fault.
Answer: B

97. Which of the following initializations of a volatile pointer is allowed?
A) volatile int *p = NULL;
B) int * volatile p = NULL;
C) volatile int * volatile p = NULL;
D) All of the above — qualifiers apply to pointer or target and initialization is allowed.
Answer: D

98. What’s the correct way to initialize an array of type int using memset (C library) to zero?
A) memset(a, 0, sizeof a);
B) memset(a, ‘\0’, sizeof a);
C) memset(a, 0, sizeof(a));
D) All the above variants with correct pointer and size are acceptable for zeroing bytes.
Answer: D

99. Which of the following is true about extern function declarations and initialization?
A) Functions cannot be initialized — only objects can.
B) You can initialize function pointers but not functions.
C) extern is unnecessary for function declarations since they are extern by default.
D) All of the above.
Answer: D

100. Which of the following is true regarding initialization of objects with dynamic storage (via malloc) vs static storage?
A) Objects from malloc are uninitialized (indeterminate) unless explicitly initialized; static storage objects are zero-initialized.
B) Both are zero-initialized automatically.
C) malloc’d memory is always filled with zeros by standard.
D) Static storage must be initialized by programmer.
Answer: A