top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

C : Can we change value of a constant variable?

+3 votes
928 views
C : Can we change value of a constant variable?
posted Jun 23, 2014 by Ganesh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

4 Answers

0 votes

AFAIK Short answer is no, a constant variable can not be changed. However in some versions of C compiler this behavior is not intact i.e. using pointers u can modify the value but in GCC can not change the value of constant even using pointer.

See the following code

int main()
{
    const int a = 1;
    int *p;
    p = &a;
    *p = 5;
    printf("Value of a=%d",a);
}

This will always print 1 even though value of a has been changed by the pointer.

answer Jun 23, 2014 by Salil Agrawal
Thanks Salil. But value becomes 5. I verified in the system.
Ok I tested on http://codepad.org/ (I assumed that codepad is GCC which is not) so it proves compiler behavior is different for this case.
0 votes

The answer of this question depends on the type of const variable whether it is local or global const variable.

Const value :- U can't change the value using the variable.
local cons :- U can change the value using its pointer pointing to it.
global const :- U can't change its value using its pointer pointing to it too.

If we go through the in depth of importance of const they we can understand it better.

Consider two example of it where one const variable is local and another is global.

Local const importance: consider this program

 /* program 1 */

    int main()
    {
      const int a = 20; // Local const variable
      int const *p = &a; // As p is a pointer to const variable
      a = 30; //giving compilation error error as it is a const variable.

      *p = 30;//No error and value of a also changing ???
    }

1> General variables value we can change at different places in the same function or in different function by passing it as argument.

2> Suppose we want to take a variable whose value we don't want to change throughout the program Suppose take a local variable like :- int a = 30;

3> We can take care of it by not changing the value anywhere (By not writing a statement which changes the value of a variable from 30 )

4> So we need to check each and every place like anywhere I have written statement to increase the value of a (e.g a++, a = something ,++a like this) it means that we need to check/review it manually.

5> To not to check manually and giving this task to compiler so that he can check for it somewhere those kind of statement if u want to make a variable whose value will not change till end of program.

6> So for this we can make this value as const and define like this const int a = 30;

so that while compiling compiler will see any statement written which uses x and trying to increment it he will throw a compilation error.

in your example 1 it gives error

a = 30; //giving compilation error error as it is a const variable.

7> But using its address we can changes as the value is changed by considering its address at run time and a is not stored in read only memory also.(As a is local variable stored in stack)

int const *p = &a;

as a is stored in stack so that u can change value of it by accessing its address.

Global const Importance

consider this example

/* program 2 */

const int a = 20; //Global const variable
int main()
{
  int const *p = &a; // As p is a pointer to global const variable
  a = 30; //giving compilation error as it is a const variable.

  *p = 30;//No compilation error but gives segmentation fault ???
}

1> const variable can be global too as you have shown in 2nd example.

2> global const variable is stored in read only memory.

3> as its const variable so we can't write statement like a++, ++a, a= something; it will compilation error as the rule applies the same as local const variable.

4> But when we are accessing it using address and trying to change it (Run time) it gives segmentation fault as the a memory location is at read only data segment area.

5> So we can't write like this (As we can;t change the read only data segment)

Hope it is clear.

References:
http://embeddedgurus.com/barr-code/2012/01/combining-cs-volatile-and-const-keywords/
http://blog.techveda.org/using-const-keyword-in-c/
http://www.noxeos.com/2011/07/29/c-const-static-keywords/

answer Jun 24, 2014 by Sachidananda Sahu
0 votes
#include<stdio.h>
void fun (int *a)
{
    *a = 5;
}

int main()
{
    const int a = 1;
    fun(&a);
    printf("Value of a=%d",a);
}


OUTPUT: Value of a=5
answer Jun 24, 2014 by anonymous
0 votes

Yes, but not through by the same operand (variable).

For example,

const int A =10;
Here the variable 'A' will not allow you to change its value.
We cant use this operand in Left side of assignment operator.

answer Dec 4, 2014 by sivanraj
Similar Questions
+2 votes

I assume that constant must be initialized to a constant value at compile time, but following code the return value of ge_const() is assigned to x which will be collected at run time. So this must cause an error but I am getting the output as 50. Can someone clarify the detail?

main()
{
    const int x = get_const();
    printf("%d", x);
}
int get_const()
{
    return 50;
}
+5 votes

Even I have similar problem:

int (*pfun)(void);
int *pInt = 0;
void fun1()
{
    int i = 5; /* Local to fun1*/

    printf("Outer function");
    pInt = &i; /* As I know address of local variable is valid till function execution */

    int fun2()
    {
      printf("innerfunction");
      printf("%d", *pInt);
    }
    /* fun2 address assign to pfun so that It can be called even after completion of fun1 */
    pfun = fun2;
}

int main()
{
    fun1();
    pfun();
    return 0;
}

Can someone please explain ? I am getting *pInt value 5.

...