Declaring variables with const

const indicates that the variable is a constant. Even if we try to change the value of the variable, it still outputs the value of the original value it was assigned.

A variable prefixed with “const” means the variable needs to be initialized at declaration and must remain that value through the life of the program. Any attempts to change its value should throw a compiler error.

The value of the variable is forced to be a constant meaning it will not be changed anywhere else in the code. For instance if you were declaring a float variable for pi. Pi will never change so you could use const in this instance.

Just looking at what we are working with as well as what const could stand for (constant). I would guess it doesn’t allow the value of the variable to be changed.

I think it means an unchangeable int that’s what const is in c# so by convention I would think it is the same.

I am sure it is short for constant and like a constant in math, it doesn’t change. So for a variable, it will make it constant?

a variable declared with const will not be able to change from it’s initialized value; it will throw an error message if attempted.

My guess is that const stands for constant. If we use const before int when declaring and initializing a variable, we won’t be able to change said variable at the later stages of our code.

For example:

If we do a
const int b = 3;

then later we try a
b = 5;

Either the compiler is going to give us a big nope in a form of an error or a = 5 will get ignored entirely.

I guess const it will change values from variable wherever it live

Const means constant.
If we add const befor a variable, we turn it to a constant that can’t be changed anymore.

It fixes the value of the variable to not be changeable even if I declare a new value after I initialize it. I guess.

I can’t tell what it does yet. It probably refers to “constant” or “constantly”.

constant keyword does 2 things:

  1. the value of variable must be initialized
  2. cant assing new value to this variable

Const would mean that you cannot change the variable right
so, a, b and c would remain as they were defined, plus sum and product.

Const seems like the begging to the word constant. It would suggest its not a variable anymore but a constant value that can’t changed after being initialized.

1 Like

I think const means constant and will make a variable retain it’s value even if we try to change it later.

My guess is that it it no longer makes it a “variable” anymore, that it just gives it one value that can never be changed.

Const fixes the variable as a constant number that cannot affect the code by declaring a different value later in the program.

I believe that when we use const we are telling the compiler that this variable must remain a constant throughout the whole execution of the code. :globe_with_meridians:

I think const stands for constant making the variable the constant set variable on the line unless changed with another const

Privacy & Terms