In C storing values that start with zero get mutated, why?

前端 未结 4 2124
闹比i
闹比i 2020-12-19 15:25

For example:

int main(){

    int x = 01234567;

    printf(\"\\n%d\\n\",x);

    return 0;

}

The following code produces: 342391

4条回答
  •  爱一瞬间的悲伤
    2020-12-19 16:01

    Integer constants written with a leading 0 are interpreted as octal (base-8), not decimal (base-10). This is analogous to 0x triggering hexadecimal (base-16) interpretation.

    Basically all you can do here is not put leading 0s on your integer constants.

提交回复
热议问题