the literals in c++

Hello everyone, I'm new on this forum and I wanted to ask if you could help me with the literals in C + +, I'm reading the book C + + Primer (fifht edition) and are just starting out with this language, my problem is that I can not understand the integer literals,
if I perform the tests with a variable such as int a = 42;

1
2
3

int a = 42;


its value varies within my test code, so I do not understand what these literals defined as constants, because the value of a can change easily;
42 is the integer literal. The only way you can change this value is to modify the code and recompile your program.

a is a integer variable. You are giving it the initial value 42 but you can modify it as much as you want.

If you don't want it to be possible to modify a you can make it constant.
1
2
const int a = 42;
a = 24; // Error: You can't modify a const variable! 
Last edited on
@Peter87

So why on some forums some people say that the literals are integer constants when in fact it is the opposite?
last question: my book, I also explain the difference between the various types of integer literals as decimal, octal, hexadecimal, and then explains to me that the whole decimal literals as have smallest type of int, long, long long, this means say that I can not create and initialize an integer variable if it does not belong to these types ?
why on some forums some people say that the literals are integer constants when in fact it is the opposite
Literals are constant. you cannot change their values without editing source code.

a is not literal, it is variable initializated with literal. It copied value of literal in itself. When you change variable, you do not change literal.

I can not create and initialize an integer variable if it does not belong to these types ?
You can. It is type of literal, not the variable you assigning it to.
As you can do
1
2
int i = 5;
short x = i;
you can do
signed int a = 15ull; //assigning literal of type unsigned long long to variable of type signed int too
If you want to be formal, integer literals such as 42 are classified as rvalue expressions (because the address cannot be taken) of non-const type int, and they are also classified as constant expressions, meaning that their values are known at compile time (integer constant expressions can be used as array bounds, bit-field lengths, case expressions, enumeration initializers, or as integer template parameters)
Topic archived. No new replies allowed.