以下代码片段(C 语言)打印什么?

发布于 2024-10-06 07:20:13 字数 84 浏览 0 评论 0原文

以下代码片段(C 语言)打印什么?

int a = 033;
printf("%d", a + 1);

What does the following code fragment (in C) print?

int a = 033;
printf("%d", a + 1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

赢得她心 2024-10-13 07:20:13

033 是一个八进制整数文字,其值为8*3+3 = 27。您的代码打印 28

0 开头的整数文字是八进制。如果它以 0x 开头,则它是十六进制的。

顺便说一句,作为示例,

int x = 08; //error

这是一个编译时错误,因为 8 不是八进制数字。

033 is an octal integer literal and its value is 8*3+3 = 27. Your code prints 28.

An integer literal that starts with a 0 is octal. If it starts in 0x it's hexadecimal.

By the way, for an example's sake

int x = 08; //error

is a compile-time error since 8 is not an octal digit.

仙气飘飘 2024-10-13 07:20:13

我会冒着疯狂猜测的风险说28:)

I would risk a wild guess and say 28 :)

有木有妳兜一样 2024-10-13 07:20:13

28.

033 在 C 中是一个八进制数,因为它有一个前导“0”,这意味着它的十进制值为 27。

所以,27 + 1 = 28

28.

033 is an octal number in C because it has a leading "0" and that means its value is 27 in decimal.

So, 27 + 1 = 28

倥絔 2024-10-13 07:20:13

这里有一个提示:

  • 开头为零的 3 位数字是八进制。
  • 以“0x”开头的 2 位值是十六进制。

尝试看这个例子:

 #include<stdio.h>
 main()
 {
 int a = 033;
 printf("\nin decimal: %d", a+1);
 printf("\nin hex: %x", a+1);
 printf("\nin octal: %o", a+1);
 }

结果是:

in decimal: 28
in hex: 1c
in octal: 34

here's a cue:

  • a 3-digit with zero in the beginning is an octal.
  • a 2-digit value with "0x" in the beginning is a hex.

Try looking at this example:

 #include<stdio.h>
 main()
 {
 int a = 033;
 printf("\nin decimal: %d", a+1);
 printf("\nin hex: %x", a+1);
 printf("\nin octal: %o", a+1);
 }

this results in:

in decimal: 28
in hex: 1c
in octal: 34
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文