是整型常量的默认类型符号或无符号? [英] Is the integer constant's default type signed or unsigned?

查看:768
本文介绍了是整型常量的默认类型符号或无符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是整型常量的默认类型符号或无符号?如为0x80000000,我怎么能决定使用它作为一个符号整数常量或无符号整型常量不带任何后缀?

如果它是一个有符号整数常量,如何解释下面的案例?

 的printf(0x80000000的>> 3:%X \\ n,0x80000000的>> 3);

输出:

  0x80000000的>> 3:千万

下面的案例可以说明我的平台使用算法按位转移,不是逻辑按位转变:

  INT N = 0x80000000的;的printf(N>→3:%×\\ N,N>→3);

输出:

  N'GT;> 3:F0000000


解决方案

C有十进制,八进制和十六进制常量不同的规则。

有关小数,它是第一个类型的值可以适应: INT 长长

有关十六进制,它是第一个类型的值可以适应: INT unsigned int类型无符号长长长无符号长长

例如用 32位 INT unsigned int类型<系统上/ code>:为0x80000000 unsigned int类型

需要注意的是十进制常量,C90有不同的规则(但规则没有为十六进制常数变化)。

Is the integer constant's default type signed or unsigned? such as 0x80000000, how can I to decide to use it as a signed integer constant or unsigned integer constant without any suffix?

If it is a signed integer constant, how to explain following case?

printf("0x80000000>>3 : %x\n", 0x80000000>>3);

output:

0x80000000>>3 : 10000000

The below case can indicate my platform uses arithmetic bitwise shift, not logic bitwise shift:

int n = 0x80000000;

printf("n>>3: %x\n", n>>3);

output:

n>>3: f0000000

解决方案

C has different rules for decimal, octal and hexadecimal constants.

For decimal, it is the first type the value can fit in: int, long, long long

For hexadecimal, it is the first type the value can fit in: int, unsigned int, long, unsigned long, long long, unsigned long long

For example on a system with 32-bit int and unsigned int: 0x80000000 is unsigned int.

Note that for decimal constants, C90 had different rules (but rules didn't change for hexadecimal constants).

这篇关于是整型常量的默认类型符号或无符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆