Const Variables in C++
This Snippet is coded by a user inOnline cpp Compiler. You can see and run this code too.
زبان: C++
This code demonstrates the use of the const keyword in C++. A constant variable named myNum is initialized with the value 15.
Constants in C++ are read-only, and any attempt to modify them results in a compile-time error.
When the line myNum = 10; is executed, the compiler throws:
error: assignment of read-only variable 'myNum'
This prevents accidental modifications, making your code safer and more predictable.