C++ Constants and Literals

Constants in C++ refers to immutable values that C++ program cannot change during the course of execution. In C++, a constant can be of any basic data types like boolean, character constants, integer constant, strings etc.

For Example : true, 'A', 754, 123.5, "Hello World".

Boolean literals in C++

C++ supports two boolean literals "true" and "false". true and false are part of the C++ keywords.

Integer Constants in C++

Integer constants are whole numbers without any fractional part or exponential part. It can either be positive or negative, If no sign precedes then by default it is assumed to be positive. The range of integer constant depends on programming environment. For Example, In a 16-bit system range of integer literal is -32768 to 32767.
An integer constant can be a decimal, octal, or hexadecimal. A prefix specifies the base of number system.

Prefix Description Base Example
0x or 0X Hexadecimal Number 16 0x5C, 0x22
0 Octal Number 8 012C, 0243
Nothing Decimal Number 10 25, 100

You can also specify the type of integer constant by using a suffix character. It can be lowercase or uppercase and can be in any order.
  • L, l : Long
  • U, u : Unsigned
Examples of Integer Constants
  • Unsigned Integer Constant : 100U, 565U
  • Long Constant : 67L, -2398876L
  • Unsigned Long Constant : 55UL, 77652UL
  • Decimal Constants : 85, -54
  • Octal Constants : 0213, 045
  • Hexadecimal Constants : 0x4b, 0x2A

Character Constants in C++

Character constants are enclosed between a pair or single quote. C++, supports two type of character constants, wide character literal and narrow character literal. If a character literal starts with uppercase 'L' then it is a Wide character (for Example : L'a') and must be stored in a variable of type wchar_t otherwise it is a narrow character(for Example : 'a') and can be stored in variable of type char. The ASCII value of character constants are stored internally.

For Example : 'a', 'A', '1', '#' are character constants. C++ Backslash Character Constants

There are some characters which are impossible to enter into a string from keyboard like backspace, vertical tab, newline etc. C++ includes a set of backslash character which have special meaning in C++ programming language. The backslash character ('\') changes the interpretation of the characters by C++ compiler.

Escape Sequence Description
\” Double quote(")
\' Single quote(')
\\ Backslash Character(\)
\b Backspace
\f Form feed
\n New line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\a Alert or bell
\? Question mark
\0 Null character
C++ Program to print character constants
#include <iostream>
using namespace std;

int main() {    
    char c1 = 'E';
    wchar_t wc = L'E'; 
    cout << c1 << endl;
    cout << wc << endl;
    // Printing newline character
    cout << '\n';



C++ Floating-point Constants

A floating point constant has a integer part, a decimal point, a fractional part and may contain an exponential part. Floating point literal may be positive or negative but must have a decimal point. You can also represent floating point literals in scientific notation.

For Example: 1234.5432, -100.001, 2.37E-3

C++ String Constants

p class="myParagraph"> A string constant in C++ is a set of characters enclosed between a pair of double quotes. A string literal is actually a character array. A string literal may contain any number of characters including alphanumeric characters, escape characters, graphical characters etc.

For Example
  • "" : Null String.
  • "A" : String literal having one characters.
  • "XYZ12.iyu" : String literal with multiple characters.
  • "XYZ jjuh\n" : String with spaces and escape characters.

Declaration of Constants in C++

We can define constants in C++ in two ways.
  1. Using const keyword.
  2. Using #define preprocessor directives.
We can use const keyword as a prefix of data type to declare a Constant. Here is the syntax for using const keyword.
const data_type variable_name = Constant;
For Example
    const float INTEREST_PERSENTAGE = 9;
#include Preprocessor Directive

We can use #define preprocessor directive to define a constant as per following syntax.

#define Constant_Identifier Value
For Example:

C++ Program to show use of #include preprocessor
#include <iostream>
#define PI 3.141

using namespace std;
int main() {    
    float radius;
    cout << "Enter Radius of Circle\n";
    cin >> radius;
    cout << "Circumference of Circle : " << 2*PI*radius;
    return 0;

Enter Radius of Circle
Circumference of Circle : 31.41