Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   What is the "const" ? Why do we use it ? (http://www.go4expert.com/forums/const-t21584/)

lionaneesh 30Mar2010 19:43

What is the "const" ? Why do we use it ?
 
I want to know what is const type in C and what is its uses etc.

i dont know anything about this term used in C so please give detailed description...

shabbir 30Mar2010 20:16

Re: What is the "const" ? Why do we use it ?
 
Const is constant whose value is read only and cannot be edited.

en_7123 30Mar2010 22:52

Re: What is the "const" ? Why do we use it ?
 
Const is used with variables whose value remains constant throughout the program
.You should use constant for variables whose value would not change.
Even though I think it's a mere cosmetic as even if you use constant you can still change the value of that variable by using pointer.

xpi0t0s 31Mar2010 13:44

Re: What is the "const" ? Why do we use it ?
 
const is for variables that do not change. So they are like defined symbols but with type checking.
Code:

#define a 1
const int b=2;

b is recognised as an integer by the compiler. But a will just be lexically replaced with 1 wherever it occurs, and this can lead to runtime errors if it's used in the wrong context.

const is also used by functions to indicate that the parameter won't be changed by the function. The compiler will prevent you from making changes to a parameter that you have declared as const.

*DO NOT* "work around" const by using a pointer. Copy the value into another variable instead. The reason for this is that an optimising compiler will take const into account. So:
Code:

const int i=10;
printf("%d",i);
printf("%d",i);

the compiler may optimise i away altogether and rewrite this as
Code:

printf("%d",10);
printf("%d",10);

so if you've been too clever and done something like:
Code:

const int i=10;
printf("%d",i);
int *j=&i;
*j*=2;
printf("%d",i);

then the output might still be 10 10. Except the optimisations could be different in different builds. So this might be 10 20 in a debug build where there is no optimisation (because optimisations can make debugging tricky), and when you're satisfied the code is correct and do a release build, the behaviour will change because of the optimisations and the code you thought would output 10 20 now outputs 10 10. Bugs that only reproduce with release builds and not debug builds are incredibly difficult to find and any kind of "hacking" like this opens the door to a whole world of pain that is so easily avoidable.

So instead of changing the value with a pointer, do this instead:
Code:

const int i=10;
printf("%d",i);
int j=i;
j*=2;
printf("%d",j);

and you will get the correct output regardless of optimisation.

en_7123 31Mar2010 20:51

Re: What is the "const" ? Why do we use it ?
 
Code:

const int i=10;
printf("%d",i);
int *j=&i;
*j*=2;
printf("%d",i);

Well I think the compiler might throw a warning in the above code but still would give output 10 20.

Code:


So instead of changing the value with a pointer, do this instead
const int i=10;
printf("%d",i);
int j=i;
j*=2;
printf("%d",j);

How is the above code relevant to changing the value of const .In case you need to change the value of const(even though if that is the case no use of defining the variable as a const) you could only do it with pointer.The above code merely prints.

Fatima Khan 1Apr2010 01:07

Re: What is the "const" ? Why do we use it ?
 
const is stand for constant. The key word which indicates the constant value of a particular variable in whole implementation.

xpi0t0s 1Apr2010 03:39

Re: What is the "const" ? Why do we use it ?
 
Quote:

Originally Posted by en_7123 (Post 66492)
Well I think the compiler might throw a warning in the above code but still would give output 10 20.

There's nothing like actually trying it out. Visual Studio 2008 throws the error:
error C2440: 'initializing' : cannot convert from 'const int *' to 'int *'

A cast will fix that:
Code:

void test19()
{
        const int i=10;
        printf("%d\n",i);
        int *j=(int*)&i;
        *j*=2;
        printf("%d\n",i);
}

and running this, the output is:

10
10
Press any key to continue . . .

So even in debug mode the int i is optimised out, and we can see this in the generated assembly:
Code:

        const int i=10;
012516BE  mov        dword ptr [i],0Ah
        printf("%d\n",i);
012516C5  mov        esi,esp
012516C7  push        0Ah
012516C9  push        offset string "%d %c" (1259800h)
012516CE  call        dword ptr [__imp__printf (125D470h)]
012516D4  add        esp,8
012516D7  cmp        esi,esp
012516D9  call        @ILT+610(__RTC_CheckEsp) (1251267h)
        int *j=(int*)&i;
012516DE  lea        eax,[i]
012516E1  mov        dword ptr [j],eax
        *j*=2;
012516E4  mov        eax,dword ptr [j]
012516E7  mov        ecx,dword ptr [eax]
012516E9  shl        ecx,1
012516EB  mov        edx,dword ptr [j]
012516EE  mov        dword ptr [edx],ecx
        printf("%d\n",i);
012516F0  mov        esi,esp
012516F2  push        0Ah
012516F4  push        offset string "%d %c" (1259800h)
012516F9  call        dword ptr [__imp__printf (125D470h)]

and you can see at "012516C7 push 0Ah" and "012516F2 push 0Ah" that the literal value 10 is pushed, not the contents of memory location &i. So at the second printf, 10 is still pushed, not what j is pointing at.

Quote:

Code:

const int i=10;
printf("%d",i);
int j=i;
j*=2;
printf("%d",j);

How is the above code relevant to changing the value of const .In case you need to change the value of const(even though if that is the case no use of defining the variable as a const) you could only do it with pointer.The above code merely prints.
Precisely. That's why I said "do this INSTEAD". i.e. don't change the const variable with a pointer, cos you won't get the desired result. This way you DO get the desired result, i.e. the output 10 20, and just to be certain (in VS2008 at least):
Code:

void test19a()
{
        const int i=10;
        printf("%d\n",i);
        int j=i;
        j*=2;
        printf("%d\n",j);
}

Output:
10
20
Press any key to continue . . .

xpi0t0s 1Apr2010 03:41

Re: What is the "const" ? Why do we use it ?
 
So the net effect is that if you "need" to change the value of a const, don't bother, because the compiler will thwart your attempts. If you need to change the value of a const, then you need for it not to be const, so take the const off and everything will work (you have source access, after all). But if you can't change the const variable to a non-const and you "need" to change its value, then you have to copy its value to a new variable that isn't const and use that instead.

en_7123 1Apr2010 17:40

Re: What is the "const" ? Why do we use it ?
 
Code:

Precisely. That's why I said "do this INSTEAD". i.e. don't change the const variable with a pointer, cos you won't get the desired result. This way you DO get the desired result, i.e. the output 10 20, and just to be certain (in VS2008 at least)
If you need to print 10 20 or for that matter any value this is quite obvious that don't try to change the value of const variable coz than there is no use of defining a variable as const.
But my point is that if you wish to change the value of const even though it should remain constant by definition still there is a a way around it by using pointers you can modify it maybe not on VS2008.I tried it on gcc and it allows ayou to get around const.So that is my point but yeah if you want to do something as trival as printing values than please don't try changing the value of const.
Code:

So the net effect is that if you "need" to change the value of a const, don't bother, because the compiler will thwart your attempts
Well this would depend on compiler some like gcc would simply throw a warning and modify the value.
Code:

If you need to change the value of a const, then you need for it not to be const, so take the const off and everything will work
Exactly coz by definition const is something whose value remains constant but there is way around it so that's all I'm trying to say.


All times are GMT +5.5. The time now is 07:34.