C++ Literals

In this tutorial, you will learn about C++ literals with the help of examples.

A literal in C++ is a value or number which is always treated with a specific value wherever it is found and cannot be changed. It is different from variables as Literals are generally rvalue and constant variables are lvalues. There are many types of literals which we use in our day-to-day programs. They are as follows :

  • Integer literal
  • Floating-point Literal
  • Boolean Literal
  • Character Literal
  • String Literal

We will discuss more them one by one in the next paragraphs.

Integer Literal 

An integer literal is a whole number that can be a long value, short value, or an integer value in C++ with a proper suffix to a number. With a proper prefix, you can declare decimal, octal and hexadecimal numbers too. A few examples of integer literals are


41         // decimal
0345       // octal
0x3d       // hexadecimal
46         // int
42u        // unsigned int
9084l      // long
56ul       // unsigned long
//Some valid and illegal literals
342         // Valid
456u        // Valid
0xfeel      // Legal as it is a hexadecimal number with a suffix of being long
078         // Illegal as 8 is not an octal digit
032UU       // Illegal as we cannot repeat a suffix.

Floating Point Literals

As the name suggests these are also constant values but they are floating points in nature which means they have either a decimal point or an exponential part attached with the number to make them a floating-point number. The following example demonstrates floating point numbers more clearly                                                          :


3.472911 // It is valid at it has a decimal point
348173E-5L // It is valid as there is a number and exponent part. 
           //The L suffix signifies that it is a long floating point.
670E // Illegal due to the incomplete exponent part
864f // Illegal as there is no decimal or exponent in it
.e34 // Illegal as it is missing an integer or fraction before the exponent part.

Boolean Literals

In C++ a boolean variable can have only two values which are either true or false and that is fixed and cannot be changed. So you can consider that the Boolean literals are true and false which can be assigned to any Boolean variable.

Character Literals

Character Literals are always enclosed in single quotes like 'a' but if the character value begins with the prefix 'L' like L'a', then it is a wide character and should be stored in a wchar_t type variable. Otherwise, by default, it is a narrow character that can be stored in a normal char variable. A character literal can be a plain wide or narrow character value as shown already, or an escape sequence like '/n' or even be a universal character like '/u02B0'. All universal character literals begin with the prefix of  '/u'. There are some escape sequences in C++ and a full list of them is given below.

List of Escape Sequences:

\a //Alert or bell
\b //Backspace
\f //Form feed
\n //Newline
\r //Carriage return
\t //Horizontal tab
\v //Vertical tab
\ooo //Octal number of one to three digits
\xhh //Hexadecimal number of two digits

Please get connected & share!