Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a "literal" in C++? [duplicate]

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

like image 678
Amani Avatar asked Jan 01 '13 14:01

Amani


2 Answers

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.

like image 76
Lightness Races in Orbit Avatar answered Sep 20 '22 12:09

Lightness Races in Orbit


Wikipedia gives you quickly this about literals.

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

like image 40
Basile Starynkevitch Avatar answered Sep 21 '22 12:09

Basile Starynkevitch