33

I have this constant:

#define MAX_DATE  2958465L

What does the L mean in this sense?

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415

4 Answers4

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).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • 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. – David Rodríguez - dribeas Jun 02 '10 at 13:29
  • 10
    @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). – James McNellis Jun 02 '10 at 13:44
  • So it seems like compiler can choose for us automatically. When do we want to put in the `L` suffix ourselves? – kizzx2 Feb 22 '11 at 03:12
  • 7
    @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. – James McNellis Feb 22 '11 at 03:30
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.

orlp
  • 112,504
  • 36
  • 218
  • 315
  • 4
    Could you give an example where it does do something? – Celeritas May 22 '12 at 07:28
  • @Celeritas This is useful: https://stackoverflow.com/questions/13134956/what-is-the-reason-for-explicitly-declaring-l-or-ul-for-long-values – Hari May 11 '23 at 12:32
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.

Steve Rowe
  • 19,411
  • 9
  • 51
  • 82
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

Pau Coma Ramirez
  • 4,261
  • 1
  • 20
  • 19
Vijay
  • 65,327
  • 90
  • 227
  • 319