简体   繁体   中英

Printing defined integers in C

I have defined an integer in C as

#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'

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.

Thus 04042012 (base 8) is 1065994 in (base 10).

Literals starting with 0 are in base 8

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

you can print extra zeros with printf()

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

Integers starting with a leading 0 are octal notation in C.

04042012 is base 8. without any leading sign it is base 10.

You might also find interesting that leading 0x is hexa decimal (Base 16).

so 0x10 would be 16 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