Latest web development tutorials

C ++ constants

A constant is a fixed value does not change during program execution. These fixed values, also calledliterals.

Constants may be any of the basic data types can be divided into integer numbers, floating-point numbers, characters, strings, and Boolean values.

Just like regular variables constant, but the value of the constant in the definition can not be modified.

Integer constant

Integer constants can be decimal, octal or hexadecimal constants. Prefix specified radix: 0x or 0X for hexadecimal, 0 for octal, without the prefix, the default decimal representation.

Integer constants can also take a suffix, the suffix is ​​the combination of U and L, U represents an unsigned integer (unsigned), L represents a long integer (long). Suffix can be uppercase or lowercase, U and L in any order.

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 constants

Floating-point constant consists of an integer part, a decimal point, and the fractional part of the index components. You can use a decimal or exponential form to represent floating-point constants.

When using the decimal representation, it must contain a decimal point, index, or both. When using the index form, it must contain the integer part, fractional part, or both. Index is an unsigned e or E introduced.

Here are a few examples of floating-point constants:

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

Boolean constants

There are two Boolean constants, which are standard C ++ keyword:

• true value represents true.
• value represents falsefalse.

We should not be regarded as the true value of 1, the value of false as 0.

Character constant

Character constants are enclosed in single quotes. If the constants L (only capitalized) at the beginning, then it is a wide character constant (for example L'x '), at which point it must be stored in a variable of typewchar_t.Otherwise, it is a narrow character constants (such as 'x'), where it can be stored in simple variablechar type.

Character constants can be an ordinary character (for example, 'x'), an escape sequence (for example, '\ t'), or a universal character (for example, '\ u02C0').

In C ++, there are some specific character, when there is a backslash in front of them, they have a special meaning, such as is used to represent a newline (\ n) or tab (\ t) and the like. The following table lists some of these escape sequences code:

\\\ 字符
\' ' 字符
\"" 字符
\?? 字符
\a警报铃声
\b退格键
\f换页符
\n换行符
\r回车
\t水平制表符
\v垂直制表符
\ooo一到三位的八进制数
\xhh . . .一个或多个数字的十六进制数

The following example shows some character escape sequences:

```#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

Constant or string literal is enclosed in double quotes "" in. Similar to a character string containing the character constant: ordinary characters, escape sequences and versatile characters.

You can use the space as separator, put a long string constants branches.

The following example shows some string constants. The following three forms string displayed is the same.

```"hello, dear"

"hello, \

dear"

"hello, " "d" "ear"
```

Defining Constants

In C ++, there are two simple ways of defining constants:

• Use#define preprocessor.
• Useconst keyword.

#define preprocessor

Here is the definition of the form #define preprocessor constants:

```#define identifier value
```

Consider the following specific 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 use theconst prefix declaration specifies the type of the constants as follows:

```const type variable = value;
```

Consider the following specific 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
```

Please note that the constant is defined as uppercase letters, it is a good programming practice.