C programming - Problems compiling with the %d format specifier | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

C programming - Problems compiling with the %d format specifier

The code example in C which returns the sizeof() in bytes for data types fails to compile w/ %d format specifier. [ clang version 7.0.0; Target: aarch64--linux-android; Thread model: posix; using Termux ] Clang throws the following warning for each printf statement: -- format specifies type 'int' but the argument has type 'unsigned long' -- Using the %lu format specifier, as suggested in Clang's output, corrects the warnings thrown by the compiler. Has anyone experienced a similar issue? Helpful suggestions appreciated!

28th Sep 2018, 8:18 PM
stefan hall
stefan hall - avatar
2 Answers
+ 1
sizeof returns a number of type size_t, this can have a different underlying type depending on complier and os. Use the format specifier %zd.
29th Sep 2018, 1:47 AM
Jared Bird
Jared Bird - avatar
+ 1
Thanks for the assist. Aside from the format specifier %lu working, the %zd specifier returns the expected output as well. Works in Termux and I suspect it will work across platforms - had the same trouble using both CodeBlocks and Bash on Ubuntu on Windows.
4th Oct 2018, 3:12 AM
stefan hall
stefan hall - avatar