-1

I walked through a C++ book and I got a statement like this long x = 32L and given statement as l or L suffix on an integer means the integer is a type long constant; but when I check using compiler as long x = 32L ; cout<<x; x = 45 ; cout<<x ; then there is no error or warning so what constant mean here? Isn't long const x = 32; ?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

5 Answers5

2

Whether or not 32L is const doesn't matter ; when you write long x = 32L; then x is a variable whose value is copied from 32. x is certainly not const. When you change x you are not changing the literal 32; you are putting a new value into the variable x's storage.

The technical term in C++ for 32 or 32L is a literal. However, people sometimes call them "constants". This is a bit confusing because it is nothing to do with what the keyword const does.

The only difference between 32 and 32L is that one is int and the other is long.

M.M
  • 138,810
  • 21
  • 208
  • 365
  • "Whether or not `32L` is `const` doesn't matter" I guess it does, though. From my understanding, it's like saying "Whether or not Wednesday tastes blue doesn't matter": `const` only applies to lvalues (and perhaps effectful rvalues). –  Jul 04 '14 at 13:54
  • @Rhymoid well, every literal has a type; and every type is either const-qualified or not const-qualified (at the top level). I can't think of a case right now where it makes a difference though. Note that `(const int)32` is legal, and gives a const rvalue – M.M Jul 04 '14 at 13:58
  • 1
    C++11 supports literals of class type; in those cases it is actually important that the literal is not const, because you can call functions on the literal directly! – M.M Jul 04 '14 at 14:04
  • Well, I guess C++'s type system is more elaborate than I thought (the undecidability of reconstruction should've been a hint). –  Jul 04 '14 at 14:12
  • FWIW, you can do `int &&x = 5; x = 6;`, too. – chris Jul 04 '14 at 14:44
  • @chris: [Oh, my](http://coliru.stacked-crooked.com/a/bde4455534707a0c). This language is stupid. – Lightness Races in Orbit Jul 04 '14 at 14:49
  • I said it in chat, but [explanation](http://codepuppy.co.uk/cpptuts/Beginning/references.php). – chris Jul 04 '14 at 14:50
  • @chris: [Apparently it's a temporary copy, at least](http://stackoverflow.com/a/6864955/560648) – Lightness Races in Orbit Jul 04 '14 at 14:51
0

I think you're getting confused about what L is for. Consider a platform with 32 bit int and 64 bit long :

unsigned long i = 0xffffffff << 16;

This sets: i <- 0xffff0000 since 0xffffffff is implicitly an integer value, and the expression on the right is evaluated accordingly. On the other hand,

unsigned long i = 0xffffffffUL << 16;

yields: i <- ffffffff0000, as the expression is being evaluated as an unsigned long.

Brett Hale
  • 21,653
  • 2
  • 61
  • 90
0

In order a variable to be constant, it should be accompanied by the const keyword.

For example:

const int a = 5;
a = 6; // error 

Now what you have, is that you assign a constant value, i.e. 32 in a variable. The constant goes to the value, not to the variable.

You do for example:

a = 32L;

so the value you assign to the variable a is a constant, 32L.

If you did

for(int i = 0; i < 5; ++i)
   a = i;

then the value you assign to variable a is not a constant.

I strongly suggests you to take a look at my relative question about the L that follows 32.

What is the difference between long x = 32L and long const x = 32 ?

The difference is that in the first, you can assign variable x a new value, where in the second you can not.

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
0

You are getting confused with the terms.

According to this statement:

int x = 32; 

x is a variable of the int type, and 32 is a literal of int type.

You can never do 32 = 35 , but you can do x = 35 because x is not a constant.

According to this other statement:

const long y = 32L;

it means that y is a constant of type long and cannot be changed. Also, 32L is a literal of type long and it cannot be changed either.

You can never do y = 35L because y is a constant and its value has been already set.

Moreover, just like @MattMcNabb sad:

"The only difference between 32 and 32L is that one is int and the other is long."

RafaelTSCS
  • 1,234
  • 2
  • 15
  • 36
  • 1
    To add to the answer, if you used **auto x=32**, it would create an int and if you used **auto y=32L**, it would create a long. – cup Jul 04 '14 at 14:15
  • @cup `auto` is perhaps a bad example, because you'd never want to use it in a case like this. On the other hand, given `template void f( T param );`, `f( 32 )` will instantiate and call `f`, and `f( 32L )` will instantiate and call `f`. – James Kanze Jul 04 '14 at 14:42
0

tl;dr: Yes, you're right.


The book is calling it a "constant", but it is wrong; the correct term is "literal".

There is no way to modify the literal itself (which is probably why the book author used this term) but, once you use the literal to initialise long int x, x can be modified because it is not const.

You are right that long int const x would be the way to make the final "variable" actually be a constant.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055