Type conversion - unsigned to signed int/char
2014-02-17 12:42
357 查看
Q:
I tried the to execute the below program:
For this program, I am getting the output:
Why are we getting different outputs for both?
Should the output be as below ?
A1:
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
The integer promotion rules state that whenever a small integer type is an operand of an operation, its type will get promoted to
which is signed. This will happen no matter if the type was signed or unsigned.
In the case of the
the value -5. In the case of the
that same value. You end up with
In the integer case you have the types
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
A2:
Cool question!
The
works, because both ints contain exactly the same bits, so they are essentially the same. But what about the
Ah, C implicitly promotes
to
on various occasions. This is one of them. Your code says
but what the compiler actually turns that to is:
if((int)a==(int)b)
-5, but
251. Those are definitely not the same.
EDIT: As @Carbonic-Acid pointed out,
251 only if a
8 bits long. If
32 bits long,
-32764.
REDIT: There's a whole bunch of comments discussing the nature of the answer if a byte is not 8 bits long. The only difference in this case is that
not 251 but a different positive number, which isn't -5. This is not really relevant to the question which is still very cool.
From:http://stackoverflow.com/questions/17312545/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!!!
A1:
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
(unsigned char) == (signed char). These are both small integer types. Other such small integer types are
booland
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
intcontaining
the value -5. In the case of the
unsigned char, it contains a value which is 251 (0xFB ). It will be promoted to an
intcontaining
that same value. You end up with
if( (int)-5 == (int)251 )
In the integer case you have the types
(unsigned int) == (signed 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)
A2:
Cool question!
The
intcomparison
works, because both ints contain exactly the same bits, so they are essentially the same. But what about the
chars?
Ah, C implicitly promotes
chars
to
ints
on various occasions. This is one of them. Your code says
if(a==b),
but what the compiler actually turns that to is:
if((int)a==(int)b)
(int)ais
-5, but
(int)bis
251. Those are definitely not the same.
EDIT: As @Carbonic-Acid pointed out,
(int)bis
251 only if a
charis
8 bits long. If
intis
32 bits long,
(int)bis
-32764.
REDIT: There's a whole bunch of comments discussing the nature of the answer if a byte is not 8 bits long. The only difference in this case is that
(int)bis
not 251 but a different positive number, which isn't -5. This is not really relevant to the question which is still very cool.
From:http://stackoverflow.com/questions/17312545/type-conversion-unsigned-to-signed-int-char
相关文章推荐
- 打印signed 及unsigned限定的char short int 及long类型变量的取值范围
- 请编写一个c程序确定signed,unsigned的char,short,int和long变量取值范围
- char, signed char,unsigned char, short int unsigned short int, int数据类型取值问题
- 关键字short、int、long、char、float、double、signed、unsigned的理解
- 练习2-1 编写一个程序一确定分别由signed及unsigned限定的char,short,int及long类型变量的取值范围。采用打印标准头文件中的相应值以及直接计算两种方式实现。通过直接计算来确定浮点类型的取值范围是一项难度很大的任务。
- C/C++之char,short ,long,double,float,int, signed, unsigned
- unsigned/signed int/char类型表示的数值范围
- char,short,int,long,unsigned,signed
- 编写一个程序以确定分别由signed及unsigned限定的char、short、int及long类型变量的取值范围。采用打印标准头文件中的相应值以及直接计算两种方式实现
- 编写一个程序,分别以signed和unsigned限定的char、short、int与long类型的变量取值范围
- 打印分别由signed和unsigned限定的char , short, int ,与long类型变量的取值范围。
- 练习 2-1 编写一个程序以确定分别由 signed 及 unsigned 限定的 char、short、 int 与 long 类型变量的取值范围。
- unsigned/signed int/char类型表示的数值范围
- Android NDK r6b使用stl遇到undefined reference to `std::__node_alloc::_M_allocate(unsigned int&)” 的终极解决办法
- 跨平台C语言,double、long、unsigned、int、char类型数据所占字节数
- char,short ,int ,long,long long,unsigned long long数据范围
- char,short ,int ,long,long long,unsigned long long数据范围
- 在C语言中,double、long、unsigned、int、char类型数据所占字节数
- 整型转字符串(convert int to char)优化实践
- linux编程--基本数据类型unsigned char/int