为什么在C 017 == 15? [英] Why is 017 == 15 in C?
问题描述
我做的阅读字节序和用C的作用一点点,但没有真正设法澄清这一点对我来说。我刚开始学习C和我看到这个例子:
I've done a little bit of reading on endianness and its role in C, but nothing has really managed to clarify this for me. I'm just starting out with C and I saw this example:
#include <stdio.h>
int main(void) {
int x = 017;
int y = 12;
int diff = x - y;
printf("diff is %d\n", diff);
return 0;
}
和它要求什么都会打印。我编译和运行例子,得到的差异是3,那么x是15.我的排序的看到这是为什么,不过倒很AP preciate如果有人真的澄清对我来说。
and it asks what will print. I compiled and ran the example and got that diff is 3, so x is 15. I sort of see why this is, but would really appreciate if somebody really clarified it for me.
[1]我看过类似的问题,但没有发现任何解释透的问题。如果有人可以链接我一个会也不错。
[1] I've looked for similar questions but haven't found any that explained the issue thoroughly. If someone could link me to one that would be good also.
推荐答案
prefixing与 0
若干会告诉编译器将其标记为八进制数(基数为8)
Prefixing a number with 0
will tell the compiler to mark it as a number in octal (base 8)
就像$ P $与 0X
pfixing它会告诉它使用十六进制(基数为16)
Just like prefixing it with 0x
will tell it to use hex (base 16)
例如:
int x = 05; // 5 in octal
int y = 5; // 5 in decimal
int z = 0x5; // 5 in hex
这篇关于为什么在C 017 == 15?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!