C++ 修饰符类型
修饰符在 C++ 中用于更改或赋予数据库类型额外的含义。它作为前缀添加到原始数据类型中以更改其含义。修饰符用于改变基本类型的含义,使其更好地匹配不同情况的要求。
以下是 C++ 数据类型修饰符:
- 签
- 未签名
- 长
- 短的
这些修饰符可以与以下内置数据类型或用户定义的数据类型一起使用。
内置数据类型:这些数据类型已经在编译器中预定义。
S No. | Data Type | Size(in bytes) |
---|---|---|
1. | char | 1 |
2. | int | 4 |
3. | float | 4 |
4. | double | 8 |
5. | bool | 1 |
用户定义的数据类型:
这些数据类型由用户定义。它们比预定义的数据类型更复杂。
1.有符号修饰符:有符号变量可以存储正整数、负整数和零。
例子:
signed int a = 45;
signed int b = -67;
signed int c = 0;
Here,
‘a’ is a positive valued integer.
‘b’ is a negative valued integer.
‘c’ is a zero-valued integer.
下面是实现上述方法的 C++ 程序——
C++
// C++ program to demonstrate
// the signed modifier
#include
using namespace std;
// Driver Code
int main()
{
cout << "Size of signed int : " <<
sizeof(signed int) <<
" bytes" << endl;
cout << "Size of signed char : " <<
sizeof(signed char) <<
" bytes" << endl;
return 0;
}
C++
// C++ program to demonstrate
// the unsigned modifier
#include
using namespace std;
// Driver Code
int main()
{
cout << "Size of unsigned int : " <<
sizeof(unsigned int) <<
" bytes" << endl;
cout << "Size of unsigned char : " <<
sizeof(unsigned char) <<
" bytes" << endl;
return 0;
}
C++
// C++ program to demonstrate
// the short modifier
#include
using namespace std;
// Driver Code
int main()
{
cout << "Size of short int : " <<
sizeof(short int) <<
" bytes" << endl;
return 0;
}
C++
// C++ program to demonstrate
// the long modifier
#include
using namespace std;
// Driver Code
int main()
{
cout << "Size of long int : " <<
sizeof(long int) <<
" bytes" << endl;
cout << "Size of long double : " <<
sizeof(long double) <<
" bytes" << endl;
return 0;
}
C++
// C++ program to demonstrate
// that modifiers can be combined
// together
#include
using namespace std;
// Driver Code
int main()
{
cout << "Size of signed long int : " <<
sizeof(signed long int) <<
" bytes" << endl;
cout << "Size of unsigned short int : " <<
sizeof(unsigned short int) <<
" bytes" << endl;
return 0;
}
C++
// C++ program to demonstrate
// the long long modifier
#include
using namespace std;
// Driver Code
int main()
{
cout << "Size of long long int : " <<
sizeof(long long int) <<
" bytes" << endl;
return 0;
}
Size of signed int : 4 bytes
Size of signed char : 1 bytes
Note: The int datatype is signed by default. So, int can be directly be used instead of signed int.
2.无符号修饰符:无符号变量只能存储非负整数值。
例子:
unsigned int a = 9;
unsigned int b = 0;
Here,
‘a’ is a positive valued integer.
‘c’ is a zero-valued integer.
下面是实现上述方法的 C++ 程序——
C++
// C++ program to demonstrate
// the unsigned modifier
#include
using namespace std;
// Driver Code
int main()
{
cout << "Size of unsigned int : " <<
sizeof(unsigned int) <<
" bytes" << endl;
cout << "Size of unsigned char : " <<
sizeof(unsigned char) <<
" bytes" << endl;
return 0;
}
Size of unsigned int : 4 bytes
Size of unsigned char : 1 bytes
有符号和无符号修饰符之间的区别:
- 有符号变量的有符号值存储在分配的内存中。但是,无符号变量不存储有符号值。
- 该标志需要额外的 1 位。因此,如果使用无符号值,则使用一位额外空间来保存变量的值。
- 无符号类型的取值范围从 0 开始。例如,对于unsigned int ,取值范围为0 到 4,294,967,295 。但是,对于signed int ,值范围是-2,147,483,648 到 2,147,483,647 。
Note: signed and unsigned modifiers can only be used with int and char datatypes.
3. Short 修饰符: Short 修饰一个数据类型可以容纳的最小值。它用于位于-32,767 到 +32,767范围内的小整数。
例子:
short int x = 4590;
下面是实现上述方法的 C++ 程序:
C++
// C++ program to demonstrate
// the short modifier
#include
using namespace std;
// Driver Code
int main()
{
cout << "Size of short int : " <<
sizeof(short int) <<
" bytes" << endl;
return 0;
}
Size of short int : 2 bytes
Note: The short int can be written as short also. They are equivalent.
4. Long Modifier: Long 修改一个数据类型可以容纳的最大值。它用于从-2147483647 到 2147483647范围内的大整数。
例子:
long int y = 26936;
下面是实现上述方法的 C++ 程序——
C++
// C++ program to demonstrate
// the long modifier
#include
using namespace std;
// Driver Code
int main()
{
cout << "Size of long int : " <<
sizeof(long int) <<
" bytes" << endl;
cout << "Size of long double : " <<
sizeof(long double) <<
" bytes" << endl;
return 0;
}
Size of long int : 8 bytes
Size of long double : 16 bytes
Note: The long int can be written as long also. They are equivalent.
要点:
1. 可以组合数据类型的修饰符。例如,有符号/无符号可以与长/短修饰符一起使用。下面是演示上述概念的 C++ 程序 -
C++
// C++ program to demonstrate
// that modifiers can be combined
// together
#include
using namespace std;
// Driver Code
int main()
{
cout << "Size of signed long int : " <<
sizeof(signed long int) <<
" bytes" << endl;
cout << "Size of unsigned short int : " <<
sizeof(unsigned short int) <<
" bytes" << endl;
return 0;
}
Size of signed long int : 8 bytes
Size of unsigned short int : 2 bytes
2 . long 修饰符可以用作 long long 的两倍。它用于比 long 更大的数字。但是,long long 类型修饰符只能与 int 一起使用。下面是演示上述概念的 C++ 程序 -
C++
// C++ program to demonstrate
// the long long modifier
#include
using namespace std;
// Driver Code
int main()
{
cout << "Size of long long int : " <<
sizeof(long long int) <<
" bytes" << endl;
return 0;
}
Size of long long int : 8 bytes
下表显示了带有修饰符的各种数据类型及其大小(以字节为单位):
1. 对于 char:这种数据类型只允许有符号和无符号修饰符。当只使用 char 而不是有符号 char 或无符号 char 时,这种类型称为普通 char。在执行数值计算时,最好使用有符号字符或无符号字符而不是普通字符。字符值只能存储为纯字符。 Modifier Size(in Bytes)S No. 1. char 1 2. signed char 1 3. unsigned char 1
2. 对于整数: Modifier Size(in Bytes)S No. 1. int 4 2. signed int 4 3. unsigned int 4 4. short 2 5. signed short 2 6. unsigned short 2 7. long 8 8. signed long 8 9. unsigned long 8
3.对于float和double: double类型可以和long修饰符一起使用。 Modifier Size(in Bytes)S No. 1. float 4 2. double 8 3. long double 16
C++ 类型限定符:类型限定符用于提供有关值的更多信息,同时还保证正确使用数据。
- const:const类型的对象在执行期间不能更改。
- 限制:修饰符volatile告诉编译器变量的值可以以程序未明确定义的方式更改。
- volatile:通过限制限定的指针最初是唯一可以访问它指向的对象的方法。只有 C99 添加了一个新的类型限定符,称为限制。