Declaring variables with const

As the name suggests const is a constant. A constant is a value that cannot be changed

constexpr is similar but apparently perform faster during runtime

Const makes it so you can not change the variable after you declare it.

makes a variable a constant

const stands for constant so the variables cannot be changed.

I believe when using “const” it was a way of declaring that whatever number is initialized to the given variable, it will keep that value at a constant even if changes occur throughout the code.

Const is to remain the variable the same and avoid it to change(?

const means constant, meaning the int will never change its value.

I bet that const stands for constant, and thus declaring a variable with const int will mean that the value of the variable cannot be changed by reassigning a value for the variable somewhere else.

I believe that

const

will keep the variable at the specified value, regardless of operations performed on the variable.

I think const means constant. When we use it in a variable declaration it prevents the variable from varying.

The const keyword is short for constant, as in, it’s constantly the same value. It cannot be changed anywhere else in the code, else it throws an error. It is especially valuable within the global space, at least in other languages. I’m not sure how the execution context works in c++, but when there’s a lot of code being written, it’s easy to assign the same variable to another value and not realize that you’ve done it. The keyword, const can prevent this. Which is really cool, and can save you a lot of time when hunting down problems. It actually prevents you from creating the problem in the first place.

As I was fiddling with the code it makes the variable stay at a constant value, I tired updating the value or even tried to change it but it seems it had ignored those changes and remain as the initial value I had declared.

My guess would be that when inserting the command “const” in front of a variable it will remain unchangeable by any other declaration to the same variable for the rest of the program code… I think

I’m assuming const stands for constant. Which constrains the variable to its declared value??

If const is used with a variable, secondary codes to change the value of that variable will not work.
When both codes are used, an error occurs:

triplex.cpp(20): error C3892: ‘a’: you cannot assign to a variable that is const

Also, I just noticed that the (20) in “triplex.cpp(20)” is referring to the line where said error is occurring. Handy.

I think the ‘const’ prefix means that the variable now becomes a constant (hence the term ‘const’), and now the variable cannot be reassigned in the middle of the code.

Const stands for constant and it will not allow you to initialize that specific variable to a new value. It will remain at its constant value that was initialized.

I assume “const” stands for constant, which would keep the assigned integer the same throughout the code.

I think that const changes the declared variable into a constant and therefore prevents the assigned value from being changed within it.

Privacy & Terms