简体   繁体   中英

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?

    #include <stdio.h>

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

025 is octal == 2 * 8 + 5 == 21 decimal

You have a minus sign in front of your integer format string placeholder, therefore it prints -21

Integer literals starting with 0 are in octal (base 8). So 025 is 2*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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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