简体   繁体   English

在C中打印定义的整数

[英]Printing defined integers in C

I have defined an integer in C as 我在C中定义了一个整数

#define FOO_VERSION 04042012

when I go to pass this to a function like so: 当我将其传递给类似这样的函数时:

unsigned int version = FOO_VERSION;
bar(version);

the actual value passed is 1065994. I verified this using GBD, printf-ing the variable version and by printf-ing the define: 'FOO_VERSION' 传递的实际值为1065994。我使用GBD进行了验证,将变量版本打印出来,并通过将定义打印出来:'FOO_VERSION'

My question is what is causing this , I can't see any reason unless there is some sort of type conversion going on that I do not know about? 我的问题是造成这种情况的原因,除非发生某种我不知道的类型转换,否则我看不出任何原因吗?

04042012是一个八进制(基数8)整数常量,因为它的04042012为0。将其删除以定义一个十进制(基数10)整数:

#define FOO_VERSION 4042012

Literal integers starting with a leading 0 is an octal in C. 0开头的文字整数是C中的八进制。

Thus 04042012 (base 8) is 1065994 in (base 10). 因此04042012 (基数8)是1065994 in(基数10)。

Literals starting with 0 are in base 8 0开头的文字以0为底

#define FOO_VERSION_8 04042012 // base 8
#define FOO_VERSION 4042012 // base 10

you can print extra zeros with printf() 您可以使用printf()打印额外的零

printf("Version %08d\n", FOO_VERSION); // prints 04042012
//               ^

Integers starting with a leading 0 are octal notation in C. 以0开头的整数是C中的八进制符号。

04042012 is base 8. without any leading sign it is base 10. 04042012是以8为底的,没有任何前导符号是10是底的。

You might also find interesting that leading 0x is hexa decimal (Base 16). 您可能还会发现有趣的是,前导0x是十六进制(基数16)。

so 0x10 would be 16 decimal. 所以0x10将是16位小数

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

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