What is the "const" ? Why do we use it ?

Discussion in 'C' started by lionaneesh, Mar 30, 2010.

  1. lionaneesh

    lionaneesh Active Member

    Joined:
    Mar 21, 2010
    Messages:
    848
    Likes Received:
    224
    Trophy Points:
    43
    Occupation:
    Student
    Location:
    India
    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...
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Const is constant whose value is read only and cannot be edited.
     
  3. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  5. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    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.
     
  6. Fatima Khan

    Fatima Khan New Member

    Joined:
    Feb 20, 2010
    Messages:
    27
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Pakistan
    const is stand for constant. The key word which indicates the constant value of a particular variable in whole implementation.
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.

    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 . . .
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    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.
     
  9. en_7123

    en_7123 New Member

    Joined:
    Feb 11, 2010
    Messages:
    105
    Likes Received:
    0
    Trophy Points:
    0
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice