Sail E0 Webinar
Question
Comment on the output of this C code?
int main()
{
const int i = 10;
int *ptr = &i;
*ptr = 20;
printf("%d\n", i);
return 0;
}
Options:
A .  Compile time error
B .  Compile time warning and printf displays 20
C .  Undefined behaviour
D .  10
Answer: Option B


Changing const variable through non-constant pointers invokes compiler warning
Output:
$ cc pgm2.c
pgm2.c: In function 'main':
pgm2.c:5: warning: initialization discards qualifiers from pointer target type
$ a.out
20



Was this answer helpful ?
Next Question

Submit Solution

Your email address will not be published. Required fields are marked *

Latest Videos

Latest Test Papers