C Programming - Const - Discussion

Discussion :: Const - Find Output of Program (Q.No.4)

4. 

What will be the output of the program?

#include<stdio.h>

int main()
{
    const int x=5;
    const int *ptrx;
    ptrx = &x;
    *ptrx = 10;
    printf("%d\n", x);
    return 0;
}

[A]. 5
[B]. 10
[C]. Error
[D]. Garbage value

Answer: Option C

Explanation:

Step 1: const int x=5; The constant variable x is declared as an integer data type and initialized with value '5'.

Step 2: const int *ptrx; The constant variable ptrx is declared as an integer pointer.

Step 3: ptrx = &x; The address of the constant variable x is assigned to integer pointer variable ptrx.

Step 4: *ptrx = 10; Here we are indirectly trying to change the value of the constant vaiable x. This will result in an error.

To change the value of const variable x we have to use *(int *)&x = 10;


Prudhvi said: (Jul 14, 2011)  
Any one give me a clear explanation to step 4.

Hari said: (Aug 25, 2011)  
I also don't think step 4 willl work how can it will try to change the value of x answer is 5.

Martyf said: (Sep 13, 2011)  
It is very difficult paper.

Prakash said: (Oct 30, 2011)  
int main()
{
int x=5;
int *ptrx;
ptrx = &x;
*ptrx = 10;
printf("%d\n", x);
return 0;
}
To change the value of const variable x we have to use *(int *)&x = 10;

Bijay said: (Dec 1, 2011)  
In gcc compiler it will work o/p:10.

Voski said: (Jan 16, 2012)  
A small correction in the Explanation Step 2 is needed: ptrx is not a constant, but a pointer to an integer constant.

Logeswari said: (Jan 25, 2013)  
1)const x is declared as integer and assign the value 5.

2)const ptrx value declared as integer point.

3)ptrx=&x mean ptrx contain address of x which mean *ptrx contain x value 5.

4)then *ptrx=10 mean indirectly change the value of 5 know.

So its wrong.

If any wrong in above statement means please any one clear me.

Charan said: (Sep 11, 2013)  
How we can change const variable by using *(int *)&x = 10; (mentioned in explanation). However it is not allowing to change from 5 to 10. But no error also raising.

Hotcpu said: (Jan 3, 2014)  
This is an undefined behavior by casting away the const definition. However, it will not work in g++ 4.1+.

Shiva Kumar said: (Sep 15, 2014)  
Using pointers we can change any kind it of data.

Even if it is constant variable also. That is why there is no security in C.

Emerson said: (Apr 11, 2015)  
Hi Shiva,

I think,

If const variable will be located in Read Only section of the memory. Trying to modify it will result in runtime error. Signal will be sent to the process in Linux.

Rishi said: (May 27, 2016)  
What is const int x=5;int *ptrx;?

Please explain.

Abhayraj Sn said: (Aug 31, 2016)  
The variable "x" of integer data type is declared as constant with assigning value 5.

ptrx is a pointer to the integer data type. Means, it stores integer values only.

You may correct or suggest me.

Suresh said: (Jan 2, 2017)  
Why cannot print x values of 5?

Gaurav said: (Jan 11, 2018)  
*(int *)&x = 10;
Please explain this.

Haritha said: (May 3, 2021)  
*(int *)&x = 10;

Please explain this.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.