Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type conversion - unsigned to signed int/char

I tried the to execute the below program:

#include <stdio.h>  int main() {     signed char a = -5;     unsigned char b = -5;     int c = -5;     unsigned int d = -5;      if (a == b)         printf("\r\n char is SAME!!!");     else         printf("\r\n char is DIFF!!!");      if (c == d)         printf("\r\n int is SAME!!!");     else         printf("\r\n int is DIFF!!!");      return 0; } 

For this program, I am getting the output:

char is DIFF!!! int is SAME!!!

Why are we getting different outputs for both?
Should the output be as below ?

char is SAME!!! int is SAME!!!

A codepad link.

like image 568
user2522685 Avatar asked Jun 26 '13 05:06

user2522685


People also ask

How do I convert unsigned to signed int?

To convert a signed integer to an unsigned integer, or to convert an unsigned integer to a signed integer you need only use a cast. For example: int a = 6; unsigned int b; int c; b = (unsigned int)a; c = (int)b; Actually in many cases you can dispense with the cast.

Is %d signed or unsigned?

'%u' treats the integer as unsigned, whereas '%d' treats the integer as signed.

Can you cast unsigned char to char?

If you want to convert char to unsigned char inside the function, you just assign a char value to an unsigned char variable or cast the char value to unsigned char .

Can an unsigned char be an int?

unsigned char is essentially a one byte of memory interpreted by the computer as an integer it is from 0 to 255. An integer type is usually 4 bytes with range -2147483648 to 2147483647. Conversion usually involves assignments from one value to another.


1 Answers

This is because of the various implicit type conversion rules in C. There are two of them that a C programmer must know: the usual arithmetic conversions and the integer promotions (the latter are part of the former).

In the char case you have the types (signed char) == (unsigned char). These are both small integer types. Other such small integer types are bool and short. The integer promotion rules state that whenever a small integer type is an operand of an operation, its type will get promoted to int, which is signed. This will happen no matter if the type was signed or unsigned.

In the case of the signed char, the sign will be preserved and it will be promoted to an int containing the value -5. In the case of the unsigned char, it contains a value which is 251 (0xFB ). It will be promoted to an int containing that same value. You end up with

if( (int)-5 == (int)251 ) 

In the integer case you have the types (signed int) == (unsigned int). They are not small integer types, so the integer promotions do not apply. Instead, they are balanced by the usual arithmetic conversions, which state that if two operands have the same "rank" (size) but different signedness, the signed operand is converted to the same type as the unsigned one. You end up with

if( (unsigned int)-5 == (unsigned int)-5) 
like image 142
Lundin Avatar answered Sep 20 '22 13:09

Lundin