Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

c - Is the integer constant's default type signed or unsigned?

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
", 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>>3);

output:

n>>3: f0000000
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...