+ 2
5 differences between constant and variables
3 Réponses
+ 3
constant cannot be changed but variable can change during the course of the program.
0
Const is a keyword and a variable is a type, it's an apple to oranges comparison and they're not mutually exclusive. For example:
void Foo(const int x) {...};
int main () {
int x = 666; // is a variable
Foo(x);
printf("%d", x); // no matter what Foo does, will print 666
x = 0; // will be reassigned
}
So instead I'll list 5 uses of const:
1. Foo(const int x) - cannot reassign x, read only
2. Foo(const int* x) - cannot reassign pointer
3. Foo() const - cannot reassign values not in the scope of the function
4. const int* Foo() - returns a pointer that cannot be reassigned (i.e. private member pointers)
5. const int x = 666 can be used to size array.
0
Constant can not change but variables can change