Constant Variable Value

How will you change the value of a constant variable in C?

Questions by Vinod G S

Showing Answers 1 - 12 of 12 Answers

The only well-defined way to change the value of a const-qualified variable is to also declare it volatile and let some external system change the value. The whole point behind declaring a variable as "const" is that you explicitly do not want the value to be changed by the code.

Tricks like

const int x = 5;
int *y = (int *) &x;
*y = 10;

Invoke undefined behavior; any result is possible. 
If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined. If an attempt is made to refer to an object defined with a volatile-qualified type through use of an lvalue with non-volatile-qualified type, the behavior is undefined.

  Was this answer useful?  Yes

Praneeth

  • Aug 25th, 2011
 

No the constant can be changed by using the pointer. Initialize a pointer to point to the value of a and then change the value using the pointer. It works.....
Try this:

Code
  1. #include <stdio.h>

  2. "%d""%d""%d"

Praneeth

  • Aug 25th, 2011
 

It works as said by the first two members. Try this :

Code
  1. #include <stdio.h>

  2. "%d""%d""%d"

  Was this answer useful?  Yes

Arup

  • Aug 27th, 2011
 

@Praneeth,
how the line
int*k=(int *)&a;
executes....can u explain me...it'll be helpful for me??

  Was this answer useful?  Yes

@Praneeth: unfortunately, that code invokes undefined behavior, as per the following section from the C language standard:

6.7.3 Type Qualifiers
...
5 If an attempt is made to modify an object defined with a const-qualified type through use of an lvalue with non-const-qualified type, the behavior is undefined. If an attempt is made to refer to an object defined with a volatile-qualified type through use of an lvalue with non-volatile-qualified type, the behavior is undefined.115)

Furthermore, a const-qualified object may be placed in a read-only data segment, as per footnote 114:

114) The implementation may place a const object that is not volatile in a read-only region of storage. Moreover, the implementation need not allocate storage for such an object if its address is never used.

Your code may generate the result you expect on your system, but that's a matter of luck, not design. You can't guarantee that you'll get the same result on a different system.

  Was this answer useful?  Yes

Sudhir Venugopal

  • Aug 31st, 2011
 

Thought so! You can change the memory value but you can not add the amount to a const variable

Code
  1. #include <stdio.h>

  2. "%d

  3. "/*the constant has 1 as value; the value can not be changed, but the const can be traversed through various memory addresses to confuse the reader, programmer*/"%d

  4. "


The first const value is 1;

The second const value is a memory address; Thanks


  Was this answer useful?  Yes

sapan

  • Sep 8th, 2011
 

Code
  1. #include<stdio.h>

  2. #include<conio.h>

  3. "before modification a=%d

  4. ""after modification a=%d"

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions