Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The constant of C+


May 11, 2021 C++


Table of contents


The constant of C+

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

Constants can be 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 values of constants cannot be modified after they are defined.

Integer constant

An integer constant can be a heteth, octal, or heteen constant. The prefix specifies a base: 0x or 0X for hex, 0 for octal, and a hex by default without prefix.

An integer constant can also have a suffix, which is a combination of U and L, which represents an unsigned integer (unsigned), and L, which represents a long integer. The suffix can be capital, or it can be small, and the order of U and L is arbitrary.

Here are a few examples of integer constants:

212         // 合法的
215u        // 合法的
0xFeeL      // 合法的
078         // 非法的:8 不是八进制的数字
032UU       // 非法的:不能重复后缀

The following are examples of various types of integer constants:

85         // 十进制
0213       // 八进制 
0x4b       // 十六进制 
30         // 整数 
30u        // 无符号整数 
30l        // 长整数 
30ul       // 无符号长整数

Floating-point constant

Floating-point constants consist of integer parts, adget points, a small part, and an exponential part. You can use the form of a small number or an exponent to represent floating-point constants.

When expressed in the form of a number of points, you must include a number of points, an exponent, or both. W hen expressed exponentially, you must include an integer part, a small part, or both. The signed exponent is introduced with e or E.

Here are a few examples of floating-point constants:

3.14159       // 合法的 
314159E-5L    // 合法的 
510E          // 非法的:不完整的指数
210f          // 非法的:没有小数或指数
.e55          // 非法的:缺少整数或分数

Boolean constant

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

  • True values represent the truth.
  • The false value represents false.

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

The character constant

Character constants are enclosed in single quotes. I f the constant starts with L (when capitaled only), it is a wide character constant (such as L'x'), which must now be stored in a variable of type wchar_t. Otherwise, it is a narrow character constant, such as 'x', which can be stored in a simple variable of the char type.

The character constant can be a normal character (such as 'x'), an escape sequence (such as ''t'), or a generic character (e.g. ''u02C0').

There are specific characters in C+ that have a special meaning when they have backslashes in front of them, and are used to represent things like line breaks or tabs. The following table lists some of these escape sequence codes:

Escape sequence meaning
\\ \ Characters
\' 'Characters
\" "Character
\? ? Character
\a Alarm ringtone
\b Retractable
\f Change the page
\n Wrap
\r Bus stop
\t Horizontal tab
\v Vertical tab
\ooo One to three binary numbers
\xhh . . . One or more numbers of hexadecimal numbers

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

Hello   World

String constant

String literal values or constants are enclosed in double quotes "". A string contains characters that resemble character constants: ordinary characters, escape sequences, and generic characters.

You can use spaces as separators to branch a long string constant.

The following example shows some string constants. The strings shown in the following three forms are the same.

"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"

Define the constant

There are two simple ways to define a constant in C+:

  • Use #define preprocessor.
  • Use the const keyword.

#define preprocessor

Here's #define the preprocessor-defined constants using the #define of the using :

#define identifier value

See the following 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 use the const prefix to declare a constant of the specified type, as follows:

const type variable = value;

See the following 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

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