c - 为什么以下代码片段中的 050 不等于 50?

标签 c

为什么以下代码片段中的050不等于50

#include <stdio.h>
int main(void) {
    int x=050,y=50;
    int ans;
    ans= x==y ? x+3 : y+7;
    printf("%d\n",ans);
}

最佳答案

因为 050 被视为八进制,而 50 被视为十进制。

因此,x = 050 基本上意味着 x = 40

6.4.4.1/3

A decimal constant begins with a nonzero digit and consists of a sequence of decimal digits. An octal constant consists of the prefix 0 optionally followed by a sequence of the digits 0 through 7 only.

关于c - 为什么以下代码片段中的 050 不等于 50?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58965260/

相关文章:

c - 如何将文本文档中的字符串放入字符串数组中?

c - xmlReadMemory 不保留原始格式的空节点

c - 优化的数组列表

c - 如何从终端获取文件名和 int 并将它们传递给 c 程序以用作参数?

c++ - 在 Android NDK 中使用多个模块

c - 即使缓冲区大小不同,数据是否在 UDP 中成功传输?

php - Openssl 和 Windows CryptoAPI 兼容性问题

c++ - 为什么我的(重新)实现 strlen 是错误的?

c - 静态与动态链接

c - 如何删除此 HEADER HTTP?