Join us Now |  Forgot Password? | Forgot UserName?


Learn step by step


Constants are expressions with fixed value.


A literal is specific constant value, such as 123, -456, 3.14, 'a', "Hello", they can be assigned directly to a variable; or used as part of an expression. They literally and explicitly identify their values because of that they are called literals.

a = 5;

The 5 in above code was a literal constant.
Literal constants are classified into: integer, floating-point, characters, strings, Boolean, pointers, and user-defined literals.

Integer Literals

A whole number, such as 123 and -456, is treated as an int, by default. Example,

int number = -123;
int sum = 4567;
int bigSum = 8234567890; // ERROR: this value is outside the range of int

An int literal may precede with a plus (+) or minus (-) sign, followed by digits. No commas or special symbols (e.g., $ or space) is allowed (e.g., 1,234 and $123 are invalid). No preceding 0 is allowed too (e.g., 007 is invalid).

Besides the default base 10 integers, you can use a prefix '0' (zero) to denote a value in octal, prefix '0x' for a value in hexadecimal, and prefix '0b' for binary value (in some compilers), e.g.,

int number1 = 1234; // Decimal
int number2 = 01234; // Decimal 2322, Octal 1234
int number3 = 0x1abc; // decimal 15274, hexadecimal 1ABC
int number4 = 0b10001001; // binary (may not work in some compilers)

>A long literal is identified by a suffix 'L' or 'l' (avoid lowercase). long long int is identified by a suffix 'LL'. We can also use suffix 'U' for unsigned int, 'UL' for unsigned long, and 'ULL' for unsigned long long int. Example,

long number = 12345678L; // Suffix 'L' for long
long sum = 123; // int 123 auto-casts to long 123L
long long bigNumber = 987654321LL; // Need suffix 'LL' for long long int

No suffix is needed for short literals. But we can only use integer values in the permitted range. For example,

short smallNumber = 1234567890; // ERROR: this value is outside the range of short.
short midSizeNumber = -12345;
Floating-point Literals

A number with a decimal point, such as 55.66 and -33.44, is treated as a double, by default. We can also express them in scientific notation, e.g., 1.2e3, -5.5E-6, here e or E denotes the exponent in the power of 10. We could precede the fractional part or exponent with a plus (+) or minus (-) sign. Exponent shall be an integer. There should be no space or other characters (e.g., space) in the number.

We MUST use a suffix of 'f' or 'F' for float literals, e.g., -1.2345F. Example,

float average = 55.66; // Error! RHS is a double. Need suffix 'f' for float.
float average = 55.66f;

Use suffix 'L' (or 'l') for long double.

Character Literals and Escape Sequences

A printable char literal is written by enclosing the character with a pair of single quotes, e.g., 'z', '$', and '9'. In C++, characters are represented using 8-bit ASCII code, and can be treated as 8-bit signed integers in arithmetic operations. Or we can say, char and 8-bit signed integer are interchangeable. We can also assign an integer in the range of [-128, 127] to a char variable; and [0, 255] to an unsigned char.

char letter = 'a'; // Same as 97
char anotherLetter = 98; // Same as the letter 'b'
cout << letter << endl; // 'a' printed
cout << anotherLetter << endl; // 'b' printed instead of the number
anotherLetter += 2; // 100 or 'd'
cout << anotherLetter << endl; // 'd' printed
cout << (int)anotherLetter << endl; // 100 printed

Non-printable and control characters can be represented by a so-called escape sequence, which begins with a back-slash (\). The commonly-used escape sequences are:

Escape Sequence Description Hex (Decimal)
\n New-line (or Line-feed) 0AH (10D)
\r Carriage-return 0DH (13D)
\t Tab 09H (9D)
\" Double-quote 22H (34D)
\' Single-quote 27H (39D)
\\ Back-slash (to resolve ambiguity) 5CH (92D)
String Literals

A String literal is composed of zero of more characters surrounded by a pair of double quotes, Example, "Hello, tuton!", "The sum ", "".,

String directionMsg = "Turn Right";
String greetingMsg = "Hello";
String statusMsg = ""; // empty string

String literals may contains escape sequences. Inside a String, we need to use \" for double-quote to distinguish it from the ending double-quote, Example, "\"quote\"". Single quote inside the String doesn't require an escape sequence. Below given example,

cout << "Use \\\" to place\n a \" within\ta\tstring" << endl;
Use \" to place
a " within a string
bool Literals

There are only two bool literals, i.e., true and false. Example,

bool done = true;
bool gameOver = false;
int i;
if (i == 9) { // it returns either true or false

In expression, Bool values and literals are converted to int 0 for false and 1 (or a non-zero value) for true.

/* Testing Primitive Types (TestLiteral.cpp) */
#include <iostream>
using namespace std;

int main() {
char gender = 'm'; // char is single-quoted
bool isMarried = true; // true(non-zero) or false(0)
unsigned short numChildren = 8; // [0, 255]
short yearOfBirth = 1945; // [-32767, 32768]
unsigned int salary = 88000; // [0, 4294967295]
double weight = 88.88; // With fractional part
float gpa = 3.88f; // Need suffix 'f' for float

// "cout <<" can be used to print value of any type
cout << "Gender is " << gender << endl;
cout << "Is married is " << isMarried << endl;
cout << "Number of children is " << numChildren << endl;
cout << "Year of birth is " << yearOfBirth << endl;
cout << "Salary is " << salary << endl;
cout << "Weight is " << weight << endl;
cout << "GPA is " << gpa << endl;
return 0;


Gender is m
Is married is 1 // true
Number of children is 8
Year of birth is 1945
Salary is 88000
Weight is 88.88
GPA is 3.88

Related Videos