0

Const

const int a; What's it? I have questions like this, but I don't study it yet.

4th Feb 2022, 12:22 PM
ŠŠ½Š“рŠµŠ¹
ŠŠ½Š“рŠµŠ¹ - avatar
8 Answers
+ 1
Const stands for constant. An example would be the irrational number Pi. Itā€™s always the same and is never subject to change. Defining variables as const acts in the same fashion. A variable marked as const cant have itā€™s value changed after its been defined. There are ways to break the const rule in C++, but that is outside the scope of this conversation. Hope that helps.
6th Feb 2022, 12:33 AM
DinoBambino
+ 1
This means that variable of type int named a cannot change.
4th Feb 2022, 12:28 PM
Bartek
+ 1
Const is used because after you add const int or something the value of that int cannot be changed in redeclaration Ex: Const int pie = 3.14;
6th Feb 2022, 6:04 AM
akarsh
akarsh - avatar
0
Int a be can't double a?
4th Feb 2022, 12:29 PM
ŠŠ½Š“рŠµŠ¹
ŠŠ½Š“рŠµŠ¹ - avatar
0
It can be any type but it can't be changed later
4th Feb 2022, 12:33 PM
Bartek
0
It means, const int a =10; a=a+4; cout << a; Output: 10?
4th Feb 2022, 12:35 PM
ŠŠ½Š“рŠµŠ¹
ŠŠ½Š“рŠµŠ¹ - avatar
0
It rises error becuse you try to change it.
4th Feb 2022, 12:38 PM
Bartek
0
int is the type (Pro tip: it's a good idea to be explicit about long or short) const marks it as a constant (i.e. immutable). Basically, it will give you an error if you try to change it somewhere. It's useful for constants you use throughout your code, for instance const maxSize = 100; in a program where you don't allow arrays (or other data structures such as lists) of size > 100
6th Feb 2022, 11:48 AM
Hiba al-Sayf
Hiba al-Sayf - avatar