简体   繁体   中英

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. But in case of char it is taken as unsigned. Why is that? Because in xcode IDE char is taken as signed by default.

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

-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. This choice varies between systems, and the C++ language specifies that either is allowed. 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. By contrast, int and signed int are two names for one and the same type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM