Why char is unsigned and int is signed by default?

Issue

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

Solution

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. 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.

Answered By – eerorika

Answer Checked By – David Marino (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.