Detail the constants in C++

  • 2020-10-23 20:14:58
  • OfStack

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

Constants can be any basic data type, and can be classified as integer, floating point, character, string, and Boolean.

Constants are just like regular variables, except that their values cannot be changed after they are defined.

Integer constants

An integer constant can be a constant in base 10, base 8, or base 106. The prefix specifies the cardinality: 0x or 0X means base 106, 0 means base 8, and the default without a prefix means base 10.

Integer constants can also have a suffix, which is a combination of U and L, with U for unsigned integer (unsigned) and L for long integer (long). Suffixes can be uppercase or lowercase, U and L in any order.

Here are some examples of integer constants:

[

212 // Legal
215u // Legal
0xFeeL // Legal
078 // Illegal: 8 is not a base 8 number
032UU // Illegal: cannot repeat suffix

]

Here are examples of various types of integer constants:

[

85 over 10
0213 // 8 base
0x4b // 106
30 / / integer
30u // Unsigned integer
30l // Long integers
30ul // Long unsigned integer

]

Floating-point constant

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

When represented in decimal form, it must contain an integer part, a decimal part, or both. When represented in exponential form, you must include a decimal point, an exponent, or both. Signed exponents are introduced in the form e or E.

Here are a few examples of floating point constants:

[

3.14159 // Legal
314159ES66en-5ES67en // Legal
510E // Illegal: Incomplete index
210f // Illegal: No decimal or exponent
.e55 // Illegal: lack of integers or fractions

]

Boolean constants

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

The value true stands for true. The false value represents false.

We should not treat 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 begins with L (only if uppercase), it is a wide character constant (for example, L'x'), at which point it must be stored in a variable of type wchar_t. Otherwise, it is a narrow character constant (for example, 'x'), where it can be stored in a simple variable of type char.

The character constant can be 1 ordinary character (such as 'x'), 1 escape sequence (such as '\t'), or 1 generic character (such as '\u02C0').

In C++, there are a number of characters that, when preceded by a backslash, have a special meaning and are used to represent such characters as line breaks (\n) or tabs (\t). The following table lists 1 such escape sequence codes:

转义序列 含义
\\ \ 字符
\' ' 字符
\" " 字符
\? ? 字符
\a 警报铃声
\b 退格键
\f 换页符
\n 换行符
\r 回车
\t 水平制表符
\v 垂直制表符
\ooo 1到3位的8进制数
\xhh . . . 1个或多个数字的106进制数

The following example shows 1 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 results:

[

Hello World

]

String constant

String literals or constants are enclosed in double quotes "". 1 string contains characters that are similar to character constants: normal characters, escape sequences, and generic characters.

You can use a space delimiter to branch a very long string constant.

The following example shows 1 string constants. The following three forms display the same string.

[

"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

]

Define constants

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

Use the #define preprocessor. Use the const keyword.

#define preprocessor

Here is the form in which constants are defined using the #define preprocessor:


#define identifier value

Take a look at the following examples:


#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 declare constants of the specified type using the const prefix, as follows:


const type variable = value;

Take a look at the following examples:


#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

]

Note that it is a good programming practice to define constants as capital letters.

These are the details of C++ constants, more information about c++ constants please follow other related articles on this site!


Related articles: