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;
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 signedint 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)