# C++ Variables, Literals and Constants

In this tutorial, we will learn about variables, literals, and constants in C++ with the help of examples.

*Content from [https://www.programiz.com/cpp-programming/variables-literals.](https://www.programiz.com/cpp-programming/variables-literals)*

## C++ Variables

In programming, a variable is a container (storage area) to hold data.

To indicate the storage area, each variable should be given a unique name (identifier). For example,

```
int age = 14;

```

Here, <var>age</var> is a variable of the `int` data type, and we have assigned an integer value 14 to it.

**Note:** The `int` data type suggests that the variable can only hold integers. Similarly, we can use the `double` data type if we have to store decimals and exponentials.

We will learn about all the data types in detail in the next tutorial.

The value of a variable can be changed, hence the name **variable**.

```
int age = 14;   // age is 14
age = 17;       // age is 17

```

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk-" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>### Rules for naming a variable

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk-a-variable-name-can-" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">- A variable name can only have alphabets, numbers, and the underscore `_`.
- A variable name cannot begin with a number.
- It is a preferred practice to begin variable names with a lowercase character. For example, <var>name</var> is preferable to <var>Name</var>.
- A variable name cannot be a [keyword](https://www.programiz.com/cpp-programming/keywords-identifiers). For example, `int` is a keyword that is used to denote integers.
- A variable name can start with an underscore. However, it's not considered a good practice.

</div></div></div>**Note:** We should try to give meaningful names to variables. For example, <var>first\_name</var> is a better variable name than <var>fn</var>.

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk--0" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>## C++ Literals

Literals are data used for representing fixed values. They can be used directly in the code. For example: `1`, `2.5`, `'c'` etc.

Here, `1`, `2.5` and `'c'` are literals. Why? You cannot assign different values to these terms.

Here's a list of different literals in C++ programming.

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk--1" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>### Integers

An integer is a numeric literal(associated with numbers) without any fractional or exponential part. There are three types of integer literals in C programming:

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk-decimal-%28base-10%29-oc" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">- decimal (base 10)
- octal (base 8)
- hexadecimal (base 16)

</div></div></div>For example:

```
Decimal: 0, -9, 22 etc
Octal: 021, 077, 033 etc
Hexadecimal: 0x7f, 0x2a, 0x521 etc
```

In C++ programming, octal starts with a `0`, and hexadecimal starts with a `0x`.

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk--2" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>### Floating-point Literals

A floating-point literal is a numeric literal that has either a fractional form or an exponent form. For example:

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk--4" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content"><div class="block-inject block-inject-1" id="bkmrk--8"><div class="pgAdWrapper"><div id="bkmrk--9"><div id="bkmrk--10"></div></div></div></div><div class="clearfix">  
</div></div></div></div>`-2.0`

`0.0000234`

`-0.22E-5`

**Note:** `E-5 = 10<sup>-5</sup>`

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk--5" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>### Characters

A character literal is created by enclosing a single character inside single quotation marks. For example: `'a'`, `'m'`, `'F'`, `'2'`, `'}'` etc.

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk--6" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>### Escape Sequences

Sometimes, it is necessary to use characters that cannot be typed or has special meaning in C++ programming. For example, newline (enter), tab, question mark, etc.

In order to use these characters, escape sequences are used.

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk-escape-sequences-cha" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content"><div class="table-responsive"><table border="0"><tbody><tr><th>Escape Sequences</th><th>Characters</th></tr><tr><td>`\b`</td><td>Backspace</td></tr><tr><td>`\f`</td><td>Form feed</td></tr><tr><td>`\n`</td><td>Newline</td></tr><tr><td>`\r`</td><td>Return</td></tr><tr><td>`\t`</td><td>Horizontal tab</td></tr><tr><td>`\v`</td><td>Vertical tab</td></tr><tr><td>`\\`</td><td>Backslash</td></tr><tr><td>`\'`</td><td>Single quotation mark</td></tr><tr><td>`\"`</td><td>Double quotation mark</td></tr><tr><td>`\?`</td><td>Question mark</td></tr><tr><td>`\0`</td><td>Null Character</td></tr></tbody></table>

</div>---

</div></div></div>### String Literals

A string literal is a sequence of characters enclosed in double-quote marks. For example:

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk-%22good%22-string-consta" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content"><div class="table-responsive"><table border="0"><tbody><tr><td>`"good"`</td><td>string constant</td></tr><tr><td>`""`</td><td>null string constant</td></tr><tr><td>`" "`</td><td>string constant of six white space</td></tr><tr><td>`"x"`</td><td>string constant having a single character</td></tr><tr><td>`"Earth is round\n"`</td><td>prints string with a newline</td></tr></tbody></table>

</div></div></div></div>We will learn about strings in detail in the C++ string tutorial.

<div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" id="bkmrk--7" typeof="sioc:Item foaf:Document"><div about="/cpp-programming/variables-literals" class="node node-cpp-tutorial clearfix" typeof="sioc:Item foaf:Document"><div class="content">---

</div></div></div>## C++ Constants

In C++, we can create variables whose value cannot be changed. For that, we use the `const` keyword. Here's an example:

```
const int LIGHT_SPEED = 299792458;
LIGHT_SPEED = 2500 // Error! LIGHT_SPEED is a constant.

```

Here, we have used the keyword `const` to declare a constant named `LIGHT_SPEED`. If we try to change the value of `LIGHT_SPEED`, we will get an error.

A constant can also be created using the `#define` preprocessor directive. We will learn about it in detail in the C++ Macros tutorial.