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...
|
Go4Expert Founder
|
![]() |
| 30Mar2010,20:16 | #2 |
|
Const is constant whose value is read only and cannot be edited.
|
|
Ambitious contributor
|
|
| 30Mar2010,22:52 | #3 |
|
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. |
|
Mentor
|
![]() |
| 31Mar2010,13:44 | #4 |
|
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; 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);
Code:
printf("%d",10);
printf("%d",10);
Code:
const int i=10;
printf("%d",i);
int *j=&i;
*j*=2;
printf("%d",i);
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);
|
|
Ambitious contributor
|
|
| 31Mar2010,20:51 | #5 |
|
Code:
const int i=10;
printf("%d",i);
int *j=&i;
*j*=2;
printf("%d",i);
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);
|
|
Go4Expert Member
|
|
| 1Apr2010,01:07 | #6 |
|
const is stand for constant. The key word which indicates the constant value of a particular variable in whole implementation.
|
|
Mentor
|
![]() |
| 1Apr2010,03:39 | #7 |
|
Quote:
Originally Posted by en_7123 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);
}
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)]
Quote:
Code:
void test19a()
{
const int i=10;
printf("%d\n",i);
int j=i;
j*=2;
printf("%d\n",j);
}
10 20 Press any key to continue . . . |
|
Mentor
|
![]() |
| 1Apr2010,03:41 | #8 |
|
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.
|
|
Ambitious contributor
|
|
| 1Apr2010,17:40 | #9 |
|
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) 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 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 |




