Constants - literals

Hi, I am learning about constants and I am slightly confused about what I am reading. The sites and book I am using says that a literal, is a constant. So I assume something like int a = 1; is a literal. But I dont understand how that is a constant. I can change that later in code, a = 2; and now 'a' has a different value. But from my understanding, I should not be able to change the value of a constant.

I am obviously confused with something here, but not sure what part I am misunderstanding. If anyone could give me a better way of understanding this, or possibly see what I am not understanding, it would be greatly appreciated
int a = 1; //1 is a literal. a is not const.
const int a = 1; //1 is a literal and a is const. the compiler can change all a's into 1's if it wants, it does not need to go fetch a to get its value like it would a variable. change a now and it is a compiler error.

and newer code uses constexpr instead of const, its a little more flexible.
older code may use #defines, which is C-ish; they don't have a strong type and can be troublesome.

you seem to have overlooked the keywords that make a constant a constant instead of a variable, is all?
Last edited on
int a = 1; is the declaration of a variable.
The variable being declared is a, it is initialised with the integer literal 1
The literal value is just representing the value you want stored in the variable. The variable itself is not constant just because the value you copied from was. Consider this:

1
2
const int a = 1;    // now a actually is constant
int b = a;          // but b isn't; it just copied a's value 

String literals are different since they are ultimately represented as a constant character pointer that points to the characters in the string literal (which are often stored in "read-only" memory so that writing to them would segfault).

1
2
const char * a = "hello";
char * b = a;             // this is not good; compiler should complain 


Ok I think I might be understanding it now. The literal is just a value, ie - 7, and its a constant because 7 is just 7 i cant change 7 to mean something else.

And then if i use const int a = 1; then i can not change a to be anything else than 1.

Is that right?

I have read a small amount about constexpr, but wanted to understand literal etc before getting into that. Thats next in the book for me :)
> if i use const int a = 1; then i can not change a to be anything else than 1.

Yes. The type of a would then be const int;
the variable identifies a const object; an object that cannot be modified.


The type of a string literal is an array (of const char type) with an additional null character as the last element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <type_traits>

template < typename T >
void what_is_this( const T& x )
{
    if constexpr ( std::is_array<T>::value )
       std::cout << "array of size " << sizeof(x)/sizeof(x[0])<< '\n' ;

    else if constexpr ( std::is_pointer<T>::value ) std::cout << "pointer\n" ;

    else std::cout << "something else\n" ;
}

int main()
{
    what_is_this( "hello world!" ) ; // array of size 13
}

http://coliru.stacked-crooked.com/a/34579b52f2cad1ba
https://rextester.com/OJGKKV90613
Last edited on
Registered users can post here. Sign in or register to post.