Data Type MCQ 3 : Const Variable cannot be modified



Variables & Constant Multiple Choice Questions 1  : Const Variable


Predict the Output of the Following Program if Given Things are – 

#include<stdio.h>
void main()
{
const int num ;
num = 10;
printf("%d",num);
getch();
}

Options :

  1. 10
  2. Nothing Will be Printed
  3. Compile Error
  4. Run Time Error

Output:

How and Why ?

  1. You get Two Errors Here
  2. Const Indicates that the Variable is of Constant Type and Cannot be modified
  3. num = 10 attempts to modify num in Program Which is Illegal

Rules :

  1. Const Variable Should be Initialized as soon as it is declared
  2. Const Cannot be Modified

Errors :

  1. Constant variable ‘num’ must be initialized
  2. Cannot modify a const object