Easy Tutorial
❮ Cpp Examples Class Member Access Operator Overloading ❯

C++ Constants

Constants are fixed values that do not change during program execution. These fixed values are also known as literals.

Constants can be any of the basic data types and can be classified as integer numbers, floating-point numbers, characters, strings, and boolean values.

Constants are similar to regular variables, except that the value of a constant cannot be modified after its definition.

Integer Constants

Integer constants can be decimal, octal, or hexadecimal constants. The prefix specifies the base: 0x or 0X for hexadecimal, 0 for octal, and no prefix for decimal.

Integer constants can also have a suffix, which is a combination of U and L, where U represents an unsigned integer (unsigned) and L represents a long integer (long). The suffix can be uppercase or lowercase, and the order of U and L is arbitrary.

Here are some examples of integer constants:

212         // Valid
215u        // Valid
0xFeeL      // Valid
078         // Invalid: 8 is not an octal digit
032UU       // Invalid: Duplicate suffix

The following are examples of various types of integer constants:

85         // Decimal
0213       // Octal
0x4b       // Hexadecimal
30         // Integer
30u        // Unsigned integer
30l        // Long integer
30ul       // Unsigned long integer

Floating-Point Constants

Floating-point constants consist of an integer part, a decimal point, a fractional part, and an exponent part. You can represent floating-point constants using decimal form or exponential form.

When using decimal form, you must include the integer part, the fractional part, or both. When using exponential form, you must include the decimal point, the exponent, or both. The signed exponent is introduced by e or E.

Here are some examples of floating-point constants:

3.14159       // Valid
314159E-5L    // Valid
510E          // Invalid: Incomplete exponent
210f          // Invalid: No decimal or exponent
.e55          // Invalid: Missing integer or fraction

Boolean Constants

There are two boolean constants, both of which are standard C++ keywords:

We should not consider the value of true as 1 and the value of false as 0.

Character Constants

Character constants are enclosed in single quotes. If a constant starts with L (only when uppercase), it indicates a wide character constant (e.g., L'x'), which must be stored in a variable of type wchar_t. Otherwise, it is a narrow character constant (e.g., 'x'), which can be stored in a simple variable of type char.

Character constants can be a regular character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').

In C++, certain characters have special meanings when preceded by a backslash, and are used to represent things like newline characters (\n) or tab characters (\t). The following table lists some of these escape sequence codes:

Escape Sequence Meaning
\ \ character
\' ' character
\" " character
\? ? character
\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 one or more digits

The following example shows some escape sequence characters:

#include <iostream>
using namespace std;

int main()
{
   cout << "Hello\tWorld\n\n";
   return 0;
}

When the above code is compiled and executed, it produces the following result:

Hello   World

String Constants

String literals or constants are enclosed in double quotes "". A string contains characters similar to character constants: regular characters, escape sequences, and universal characters.

You can use \ as a delimiter to split a long string constant into multiple lines.

The following example shows some string constants:

#include <iostream>
#include <string>
using namespace std;

int main() {
    string greeting = "hello, tutorialpro";
    cout << greeting;
    cout << "\n";     // Newline character
    string greeting2 = "hello, \
                        tutorialpro";
    cout << greeting2;
    return 0;
}

When the above code is compiled and executed, it produces the following result:

hello, tutorialpro
hello, tutorialpro

Hello, tutorialpro

Defining Constants

In C++, there are two simple ways to define constants:

#define Preprocessor

Below is the form of defining constants using the #define preprocessor:

#define identifier value

See the example below for a detailed illustration:

Example

#include <iostream>
using namespace std;

#define LENGTH 10   
#define WIDTH  5
#define NEWLINE '\n'

int main()
{
   int area;  

   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}

When the above code is compiled and executed, it produces the following result:

50

const Keyword

You can use the const prefix to declare constants with a specific type, as shown below:

const type variable = value;

See the example below for a detailed illustration:

Example

#include <iostream>
using namespace std;

int main()
{
   const int  LENGTH = 10;
   const int  WIDTH  = 5;
   const char NEWLINE = '\n';
   int area;  

   area = LENGTH * WIDTH;
   cout << area;
   cout << NEWLINE;
   return 0;
}

When the above code is compiled and executed, it produces the following result:

50

Note that defining constants in uppercase is a good programming practice.

❮ Cpp Examples Class Member Access Operator Overloading ❯