[英]Why char is unsigned and int is signed by default?
I'm using gcc as my c++ compiler and when I declare a variable of int datatype then it is taken as signed by default.我使用 gcc 作为我的 c++ 编译器,当我声明一个 int 数据类型的变量时,默认情况下它被视为已签名。 But in case of char it is taken as unsigned.
但在 char 的情况下,它被视为无符号。 Why is that?
这是为什么? Because in xcode IDE char is taken as signed by default.
因为在 xcode IDE 中,char 默认被视为已签名。
#include<iostream>
using namespace std;
int main() {
system("clear");
int x;
char y;
cout<<INT_MIN<<" "<<INT_MAX<<endl;
cout<<CHAR_MIN<<" "<<CHAR_MAX<<endl;
return 0;
}
OUTPUT: OUTPUT:
-2147483648 2147483647
0 255
char
is an unsigned type on your system, because the people who implemented your system chose that it should be unsigned. char
是您系统上的无符号类型,因为实现您的系统的人选择它应该是无符号的。 This choice varies between systems, and the C++ language specifies that either is allowed.这种选择因系统而异,C++ 语言指定允许任一种。 There is no "default" choice.
没有“默认”选项。 You cannot assume one choice if you wish to write programs that work across different systems.
如果您希望编写跨不同系统工作的程序,则不能假设一种选择。
Note that char
, signed char
and unsigned char
are all three distinct types regardless of whether char
is signed or not.请注意,
char
、 signed char
和unsigned char
都是三种不同的类型,无论char
是否有符号。 By contrast, int
and signed int
are two names for one and the same type.相比之下,
int
和signed int
是同一种类型的两个名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.