C++ Constants

Constants are fixed values that do not change during program execution. These fixed values are also calledLiterals。

Constants can be of any basic data type and can be divided into integer numbers, floating-point numbers, characters, strings, and boolean values.

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

Integer constants

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

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

The following are some examples of integer constants:

212 // valid 215u // valid 0xFeeL // valid 078 // invalid: 8 is not an octal digit 032UU // invalid: cannot repeat suffix

The following are examples of integer constants of various types:

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 use decimal form or exponential form to represent floating-point constants.

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. A signed exponent is introduced by e or E.

The following 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:

  • trueThe value represents true.
  • falseThe value represents false.

We should not regard the value of true as 1 or the value of false as 0.

Character constants

Character constants are enclosed in single quotes. If a constant begins with L (only when uppercase), it indicates that it is a wide-character constant (e.g., L'x'), and it must be stored inwchar_ta variable of type. Otherwise, it is a narrow-character constant (e.g., 'x'), and it can be stored incharin a simple variable of that type.

A character constant can be an ordinary character (e.g., 'x'), an escape sequence (e.g., '\t'), or a universal character (e.g., '\u02C0').

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

Escape sequencesMeaning
\\\ character
\'' character
\"" character
\?? character
\aAlarm bell
\bBackspace
\fForm feed
\nNewline
\rCarriage return
\tHorizontal tab
\vVertical tab
\oooOctal numbers with one to three digits
\xhh . . .Hexadecimal numbers with one or more digits

The following example shows some escape sequence characters:

Example

#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 results:

Hello   World

String constants

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

You can use\as delimiters to break a very long string constant into multiple lines.

The following example shows some string constants:

Example

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

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

Define constants

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

  • Usage#definePreprocessor.
  • UsageconstKeyword.

#define preprocessor

The following is the form of defining a constant using the #define preprocessor:

#define identifier value

See the examples below for details:

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 results:

50

const keyword

You can useconstThe prefix declaration specifies a constant of a specific type, as shown below:

const type variable = value;

See the examples below for details:

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 results:

50

Please note that defining constants in uppercase letters is a good programming practice.


other extensions