简体   繁体   English

为什么printf(“ - %d”,025); 打印-21? (C)

[英]why does printf(“-%d”,025); print -21? (C)

This might be really simple, but i was wondering why if you have this code it prints out -21? 这可能非常简单,但我想知道为什么如果你有这个代码它打印出-21?

    #include <stdio.h>

    main() {
       printf("-%d",025);
    }

025 is octal == 2 * 8 + 5 == 21 decimal 025是八进制== 2 * 8 + 5 == 21十进制

You have a minus sign in front of your integer format string placeholder, therefore it prints -21 在整数格式字符串占位符前面有一个减号,因此它打印-21

Integer literals starting with 0 are in octal (base 8). 0开头的整数文字是八进制(基数为8)。 So 025 is 2*8 + 5 = 21 . 所以0252*8 + 5 = 21

If you use an editor with syntax highlighting it might show you this. 如果您使用带语法突出显示的编辑器,它可能会显示此信息。

Trivia: 0 itself is actually in octal for this reason, not decimal! 琐事:因为这个原因, 0本身实际上是八进制,而不是十进制!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM