Declaring variables with const

a cont int locks the variable from changing even if we declare a new one using the same name after the Variable is stored in the compiler.

const is short for Constant. I believe it makes it so the variable canā€™t be changed (private).

Iā€™m familiar with this from other languages. So, I am assuming itā€™s going to be the same in C++:

Declaring a variable with the const keyword means that the variable will always have the same value. Once declared and initialized, nothing can change the value of it.

const B = 2;

Using the above example, B must always be 2 and the compiler will not let us change it.

B = 5;


Attempting to assign B again will result in an error.

4 Likes

My guess would be that const would keep the number the same no mater how you try to change it

Const locks in the initiated value of the variable. Changes to that variable will be ignored.

I think that it changes the nature of the variable so that it can no longer be modified.

I think const means constant and it keeps your declared variable from being changed.

i think const mean that even if you add an other variable after you declare it
if you go like this
ex: const int a = 4

  a = 2

the compiler will ignore the new value you enter as 2.

I think it declares a fixed value, not a variable valueā€¦

const make the variable fixes ā€¦ means thought we say it a variable but the variable acts like a constant value if we try to assign a new value to constant variable then it will show error.

I would guess the ā€œconstā€ makes it so the compiler sees the given variable as absolute, and cannot be modified.

Const means constant, so it declares the integer as a fixed value rather than a variable. It also goes to global scope perhaps? Not sure.

Const keyword stores a constant value to the variable when we initialize it and does not allow it to be changed. If changed it throws an error.

I think ā€œconstantā€ means that after the variable has been initialized, it canā€™t be changed, based on the error I got when I tried to set a constant after it had been declared.

I think that prepending the variable declarations with ā€˜constā€™ makes it so that the value of the variable cannot be altered after declaration.

I think that by writing const in front of the variable means that after it got initialized, it cannot be changed anymore

I guess const would maintain the integrity of the original value for each variable regardless of any other inputs.

I think that using const will make it so that a variable wonā€™t be allowed to be changed after it has been declared anywhere else in the code.

I guess it declares that the variable is not subject to change, so if we try and change it afterwards itā€™d not be possible. I think that const works with our code right now, but if we want to change the variable value the compiler would go crazy.

I think adding const to a variable declaration will make it so the value of the variable does not change and stays the same.

Privacy & Terms