![]() |
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... |
Re: What is the "const" ? Why do we use it ?
Const is constant whose value is read only and cannot be edited.
|
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. |
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 1const 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;Code:
printf("%d",10);Code:
const int i=10;So instead of changing the value with a pointer, do this instead: Code:
const int i=10; |
Re: What is the "const" ? Why do we use it ?
Code:
const int i=10;Code:
|
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.
|
Re: What is the "const" ? Why do we use it ?
Quote:
error C2440: 'initializing' : cannot convert from 'const int *' to 'int *' A cast will fix that: Code:
void test19()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;Quote:
Code:
void test19a()10 20 Press any key to continue . . . |
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.
|
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)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 attemptsCode:
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 |
| All times are GMT +5.5. The time now is 00:59. |