Declaring variables with const

Ok here we go. Const, if I had to guess is short for constant. So in code that would make this line unchanging or “constant”

Without reading the replies here, I think const refers to constant, and I think it solidifies the initialization we’ve given to the variable, so if you initialize variable const int a = 3; , any subsequent initialization of int a will not do anything.

I think it means that the particular variables value will remain constant throughout the code and we won’t be able to change it

I think const stands for constant and means that once declared it can’t be altered afterwards like a variable.

Im not a 100% sure but I would imagine that const means constant. Therefore prefixing a variable with const would tell the compiler that even if we try and change our variable after its been declared the compiler will not change the variable.
EDIT: So I went back and removed const from and and changed the variable to make sure it actually changed the outcome. Then I went and reinstated the const before a and left the variable change in and it didnt change the outcome nor did it throw and error code so const does not allow you to change the variable even if you try.

the const keyword basically is used to declare a “constant”. A variable declared as a constant needs to be assigned a value during initialization and cannot be reassigned a new value. Whereas a regular variable can be initialized without assigning any initial value and can also be reassigned at any time.

Example

// contants
const int a = 10; // correct
a = 20; // reassigning constant throws error
const int b; // not assigning a value to a constant during initialization also throws error

// variables
int c = 5; // correct
int d; // correct
d = 10; // correct

const is an abbreviation for constant, so the variables cannot be changed during runtime anymore.

const means to keep the variable constant. meaning giving it a constant value which cannot be changed anymore afterwards.

I would guess that the variable cannot be changed during runtime?

It should make it constant, preventing the initialized value from being changed.

I understand that const stands for constant. It is a variable that is declared with a constant value that does not change. It is used for reusability.

I have no idea again, so I am going to say that const means constant and its used for variables that either stay constant or variables that do not need to be changed.

Const is a keyword that tells the compiler that this particular value will be constant throughout the execution of the program

1 Like

Const means we need to declare a value in variable and we can’t change it in the future.

It probably sets the variable as a constant, meaning that it cannot be changed elsewhere in the code.

tbh i think a constant can be changed in the terminal idk

yep got it wrong

I think it sort of hard codes in a value where it cannot be modified.

I think the compiler won’t let you change the value of the variable.

Let’s see!

const variable means constant variable… so when we add this to a variable it fixes it’s value.

Privacy & Terms