12

Possible Duplicate:
What does the word “literal” mean?

Often when reading literature about C++, I encounter the word "literal". It is a bit unclear to me what exactly this term means in C++.

Community
  • 1
  • 1
Amani
  • 16,245
  • 29
  • 103
  • 153
  • 2
    http://en.wikipedia.org/wiki/Literal_%28computer_programming%29 – Oliver Charlesworth Jan 01 '13 at 14:35
  • **See also:** http://stackoverflow.com/questions/485119/what-does-the-word-literal-mean – Lightness Races in Orbit Jan 01 '13 at 14:39
  • Note: Not to be confused with the [C++/CLI `literal` keyword](https://msdn.microsoft.com/en-us/library/5yzft952.aspx) : "A variable (data member) marked as literal in a /clr compilation is the native equivalent of a static const variable." – jrh Mar 25 '17 at 14:55

2 Answers2

41

A literal is some data that's presented directly in the code, rather than indirectly through a variable or function call.

Here are some examples, one per line:

42
128
3.1415
'a'
"hello world"

The data constituting a literal cannot be modified by a program, but it may be copied into a variable for further use:

int a = 42;  // creates variable `a` with the same value as the literal `42`

This concept is by no means unique to C++.

The term "literal" comes from the fact that you've written data literally into your program, i.e. exactly as written, not "hidden" behind a variable name.

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

Wikipedia gives you quickly this about literals.

In your C or C++ source code, Things like 1234, nullptr (in recent C++), "abcd" are literals.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547