Declaring variables with const

We cannot change the value of the variable after initializing the variable by using the const keyword.

1 Like

const possibly does not allow change of the value stored in it.

So what I assumed it would do would basically clamp the value of whatever variable at whatever the value you assigned to it was and it wouldn’t allow you to assign it a new value later in the code.

I gave it a quick test after and tried to assign a new value to “a” that had the const int and it threw an error and wouldn’t allow me to reassign.

I guess const makes the variable constant, so it prevents it from being changed later on in the code, if you try to change it, I guess the compiler would give you an error.

const is short for constant that keeps the initialized variable value the same and unchangeable

1 Like

I think const prevents any changes to the assigned variable.

1 Like

i think const is short for constant and it means that the variables value can no longer be changed after initialization, is this right?

Const from my knowledge would make the ''int" variable a constant variable that cannot be changed?
Wild guess

I think const means constant, but I don’t know why the value can’t change.

It should stand for “constant”, so this value will remain unchangeable during the execution of the code, and the compiler could throw an error if you try to assign a new value.

I have worked with C# and const is also supported there. I think its usefulness will come later, as it is used for specific data for games such as gravity and, in this case, for setting the max difficulty.

I think const locks a variable for being changed as long as its defined first.

I think to use const that the variable wont be variable it will be a static number or that when we get like number x it will stop or send a new message from the terminal

It won’t change into a new value. If “const int a = 4” is already given as a value. Then saying a = 10 later in the code, won’t work. An error would properly occur because the value is constant.

I think the const declaration says that the variable will stay constant and will not change, aka any value we assign the variable will be in the const will not change even if we try to change the variable later on.

I think that const is short for constant meaning the value can’t change

To me it mean a constant, meaning that it will stay constant as that declared variable. calling an int = declares the variable with in that {} or until re-identified…at least I think. Putting const in front of it ensures that the vaiable stays constant to what it is being identified as.

I think it’ll keep the variable the same (or constant). And if later codes try to change the value of the const variable, the program will give priority to the const than the later code, keeping the variable unchanged.

i think that it will keep the value the same (a constant).

Would the value a const is initialized with be “fixed”, not able to be changed?

Const stands for constant. Declaring this will make A, B, and C a constant variable. It’s process will last forever until we declare a termination statement.

Privacy & Terms