C Unsigned int providing a negative value?

Printing %d will read the integer as a signed decimal number, regardless of its defined type.

To print unsigned numbers, use %u.

This happens because of C’s way to handle variable arguments. The compiler just pulls values from the stack (typed as void* and pointing to the call stack) and printf has to figure out what the data contains from the format string you give it to.

This is why you need to supply the format string – C has no way of RTTI or a ‘base class’ (Object in Java, for example) to get a generic or predefined toString from.

Leave a Comment