简体   繁体   中英

printing size_t: format '%lu' expects argument of type 'long unsigned int'

I try to print size_t by casting to unsigned long (as suggested in the book "C programming a modern approach) like the following:

printf("size:%lu, bsize:%lu", (unsigned long)size, (unsigned long)bsize);
printf("size:%lu, bsize:%lu", ((unsigned long)size), ((unsigned long)bsize));

The first line would give me warning (gcc):

warning: format '%lu' expects argument of type 'long unsigned int', but argument 2 has type 'size_t' [-Wformat]

What's the difference between the first line and the second line? All I did was putting extra parenthesis, what exactly does that do?

I know I can use "%z" but this problem bugs me.

Assuming there are no ugly #define s around

printf("size:%lu, bsize:%lu", (unsigned long)size, (unsigned long)bsize);

and

printf("size:%lu, bsize:%lu", ((unsigned long)size), ((unsigned long)bsize));

are equivalent.

And therefore they shall result in the same code/warnings/errors.

If they don't, there is something broken.

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