Reply To: negative numbers, 2’s complement, signed/unsigned int/char ..

Home Forums C-programming.. negative numbers, 2’s complement, signed/unsigned int/char .. Reply To: negative numbers, 2’s complement, signed/unsigned int/char ..

#2358
Humble
Keymaster

Its always a doubt that whats default type for int and char . The default type for int is taken as signed.

How-ever char can be unsigned or signed .

/* Minimum and maximum values achar' can hold.  */
#  ifdef __CHAR_UNSIGNED__
#   define CHAR_MIN 0
#   define CHAR_MAX UCHAR_MAX
#  else
#   define CHAR_MIN SCHAR_MIN
#   define CHAR_MAX SCHAR_MAX
#  endif
`

If you are with gcc compiler, the default is signed.. You can modify that with -funsigned-char option of gcc..



   

-funsigned-char Let the type "char" be unsigned, like "unsigned char". Each kind of machine has a default for what "char" should be. It is either like "unsigned char" by default or like "signed char" by default. Ideally, a portable program should always use "signed char" or "unsigned char" when it depends on the signedness of an object. But many programs have been written to use plain "char" and expect it to be signed, or expect it to be unsigned, depending on the machines they were written for. This option, and its inverse, let you make such a program work with the opposite default. The type "char" is always a distinct type from each of "signed char" or "unsigned char", even though its behavior is always just like one of those two. -fsigned-char Let the type "char" be signed, like "signed char". Note that this is equivalent to -fno-unsigned-char, which is the negative form of -funsigned-char. Likewise, the option -fno-signed-char is equivalent to -funsigned-char.