+ 2

5 differences between constant and variables

25th Apr 2017, 7:00 AM
pravin mahat
pravin mahat - avatar
3 Answers
+ 3
constant cannot be changed but variable can change during the course of the program.
25th Apr 2017, 7:08 AM
Nithiwat
Nithiwat - avatar
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.
25th Apr 2017, 7:23 AM
Mason Krei
Mason Krei - avatar
0
Constant can not change but variables can change
12th Dec 2017, 4:01 PM
Ajay
Ajay - avatar