What does the "L" mean at the end of an integer literal?
Asked Answered
S

4

33

I have this constant:

#define MAX_DATE  2958465L

What does the L mean in this sense?

Slushy answered 2/6, 2010 at 13:18 Comment(2)
It's not a constant but a macro (that expands to a literal).Directorial
so what does the number expand to then?Slushy
G
47

It is a long integer literal.

Integer literals have a type of int by default; the L suffix gives it a type of long (Note that if the value cannot be represented by an int, then the literal will have a type of long even without the suffix).

Galimatias answered 2/6, 2010 at 13:19 Comment(4)
Sure? AFAIK literals without the 'L' suffix are of integer type in C++, and it will fail to compile if the literal will not fit in the int type.Engagement
@David: "If it is decimal and has no suffix, it has the first of these types in which its value can be represented: int, long int (C++03 §2.13.1/2).Galimatias
So it seems like compiler can choose for us automatically. When do we want to put in the L suffix ourselves?Volscian
@kizzx2: 42 is an int. If you want it to be a long, you need to add the L (giving 42L). There are many reasons that you might want a long explicitly: you might do it to select a particular function overload or to ensure a template is instantiated with long instead of int. You might use it to ensure some integer expression is evaluated with long precision instead of int precision. INT_MAX + 1 will overflow. If long has greater range than int, INT_MAX + 1L will not overflow.Galimatias
F
9

In this scenario the Ldoes nothing.

The L after a number gives the constant the long type, but because in this scenario the constant is immediately assigned to an int variable nothing is changed.

Figment answered 22/5, 2012 at 7:20 Comment(2)
Could you give an example where it does do something?Thallium
@Thallium This is useful: #13135456Covariance
B
4

L tells the compiler that the number is of type Long. Long is a signed type greater than or equal to an int in size. On most modern compilers, this means that the number will take 4 bytes of memory. This happens to be the same as an int on most compilers so it won't have an effect in this case.

Benedictbenedicta answered 22/5, 2012 at 7:23 Comment(1)
long is signed, on mac os, using gcc, long is 8 byte long.Hawkins
A
1

see this link it says :

Literal constants (often referred to as literals or constants) are invariants whose values are implied by their representations.

base:decimal

example:1L

description: Any decimal number (digits 0-9) not beginning with a 0 (zero) and followed by L or l

Alpheus answered 22/5, 2012 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.