Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading