Reputation: 2621
Problem
I am making use of the GLib 2.0 library, and declared a gunit64 variable. I wish to print its value to screen, but its not working properly.
Code
Consider the following code snippet as an example. I declare a guint64 variable, and try to print its value.
guint64 myValue = 24324823479324;
printf("My Value: %d\n", myValue);
Warning
I get this warning:
warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘guint64’
Output
I get a strange negative number on screen:
My Value: -1871285220
Further Comments
I tried to search the API's documentation, and I found the following under guint64:
An unsigned integer guaranteed to be 64 bits on all platforms. Values of this type can range from 0 to G_MAXUINT64 (= 18,446,744,073,709,551,615).
To print or scan values of this type, use G_GINT64_MODIFIER and/or G_GUINT64_FORMAT.
Therefore, I assume I have to use either the modifier or format definitions. However, the documentation does not show how to use them. Can anybody help me please?
Upvotes: 8
Views: 9672
Reputation: 400039
You've found the proper macro for this. Here's how to use it:
printf("My value: %" G_GUINT64_FORMAT "\n", myValue);
Note that the macro is a quoted string literal, so the above is the proper syntax. Also note that the %
is not part of the macro.
Your number appears negative since you're using %d
, which expects an int
, and the bits of your number, when viewed as a smaller, signed int
, encode something negative.
Upvotes: 15