C++ Type modifiers

C++ allows inchar, int, and doublePlace modifiers before the data type.

Modifiers are keywords used to change the behavior of variable types, better meeting the needs of various situations.

The data type modifiers are listed below:

  • signed: Indicates that the variable can store negative numbers. For integer variables, signed can be omitted because integer variables are signed by default.

  • unsigned: Indicates that the variable cannot store negative numbers. For integer variables, unsigned can double the range of the variable.

  • short: Indicates that the variable has a smaller range than int. short int can be abbreviated as short.

  • long: Indicates that the variable has a larger range than int. long int can be abbreviated as long.

  • long long: Indicates that the variable has a larger range than long. A data type modifier added in C++11.

  • float: Represents a single-precision floating-point number.

  • double: Represents a double-precision floating-point number.

  • bool: Represents the boolean type, which has only two values: true and false.

  • char: Indicates a character type.

  • wchar_t: Represents the wide character type, which can store Unicode characters.

Modifierssigned, unsigned, long, and shortCan be applied to integer types,signedandunsignedCan be applied to character types,longCan be applied to double-precision types.

These modifiers can also be used in combination. Modifierssignedandunsignedcan also be used aslongorshortThe prefix of the modifier. For example:unsigned long int。

C++ allows the use of shorthand notation to declareunsigned short integerorunsigned long integer. You can omit int and just write the wordunsigned、shortorlong,intis implied. For example, the following two statements both declare unsigned integer variables.

signed int num1 = -10; // 定义有符号整型变量 num1,初始值为 -10
unsigned int num2 = 20; // 定义无符号整型变量 num2,初始值为 20

short int num1 = 10; // 定义短整型变量 num1,初始值为 10
long int num2 = 100000; // 定义长整型变量 num2,初始值为 100000

long long int num1 = 10000000000; // 定义长长整型变量 num1,初始值为 10000000000

float num1 = 3.14f; // 定义单精度浮点数变量 num1,初始值为 3.14
double num2 = 2.71828; // 定义双精度浮点数变量 num2,初始值为 2.71828

bool flag = true; // 定义布尔类型变量 flag,初始值为 true

char ch1 = 'a'; // 定义字符类型变量 ch1,初始值为 'a'
wchar_t ch2 = L'你'; // 定义宽字符类型变量 ch2,初始值为 '你'

To understand the difference between signed integer and unsigned integer modifiers as interpreted by C++, let's run the following short program:

Example

#include <iostream> using namespace std; /** This program demonstrates the difference between signed and unsigned integers.*/ int main() { short int i; // signed short integer short unsigned int j; // unsigned short integer j = 50000; i = j; cout << i << " " << j; return 0; }

When the above program runs, it will output the following results:

-15536 50000

In the above results, the bit pattern of the unsigned short integer 50,000 is interpreted as the signed short integer -15,536.

Type qualifiers in C++

Type qualifiers provide additional information about variables; they are keywords used to change the default behavior of variables or functions when defining them.

QualifiersMeaning
constconstDefines a constant, indicating that the value of the variable cannot be modified.
volatileModifiersvolatileTells that the value of the variable may be changed by factors outside the program, such as hardware or other threads.
restrictbyrestrictA pointer so modified is the only way to access the object it points to. Only C99 added the new type qualifier restrict.
mutable mutable is used to modify member variables of a class. Member variables modified by mutable can be modified even if the object they belong to is const.
static Used to define static variables, indicating that the scope of the variable is limited to the current file or the current function and cannot be accessed by other files or functions.
register Used to define register variables, indicating that the variable is frequently used and can be stored in CPU registers to improve the running efficiency of the program.

Marked as deprecated in C++11, officially removed in C++17.

const example

const int NUM = 10; // 定义常量 NUM,其值不可修改
const int* ptr = &NUM; // 定义指向常量的指针,指针所指的值不可修改
int const* ptr2 = &NUM; // 和上面一行等价

volatile example

volatile int num = 20; // 定义变量 num,其值可能会在未知的时间被改变

mutable example

class Example {
public:
    int get_value() const {
        return value_; // const 关键字表示该成员函数不会修改对象中的数据成员
    }
    void set_value(int value) const {
        value_ = value; // mutable 关键字允许在 const 成员函数中修改成员变量
    }
private:
    mutable int value_;
};

static example

void example_function() {
    static int count = 0; // static 关键字使变量 count 存储在程序生命周期内都存在
    count++;
}

register example

void example_function(register int num) {
    // register 关键字建议编译器将变量 num 存储在寄存器中
    // 以提高程序执行速度
    // 但是实际上是否会存储在寄存器中由编译器决定
}

Changes to register after C++11:

  • Marked as deprecated in C++11.
  • Officially removed in C++17
  • Reserved as a keyword (possibly for compatibility or future use)
other extensions