use*_*282 3 c types implicit-conversion
请帮我区分C中的这些代码:
代码1:
#include<stdio.h>
#include <stdint.h>
uint8_t fb(int a)
{
return -3;
}
int main()
{
int a = fb(-3);
printf("%d",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
代码2:
#include<stdio.h>
unsigned int fb(int a)
{
return -3;
}
int main()
{
int a = fb(-3);
printf("%d",a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是第一个代码按预期返回253但第二个代码返回-3,这是意外的,因为返回类型是无符号的.请帮帮我这是怎么回事?
我用过mingw gcc编译器.
在第一个程序中,
int-3(FFFFFFFD)被转换uint8_t为253(FD).高阶字节被丢弃,因此它适合.uint8_t253(FD)int通过符号扩展转换为253(000000FD).在第二个节目中,
int-3(FFFFFFFD)强制转换为unsigned int4294967293(FFFFFFFD).不需要删除任何字节.unsigned int4294967293(FFFFFFFD)int为-3(FFFFFFFD).笔记: