I know we can use printf("%04X", value); to print unsigned hexadecimal values.

Is there a similar flag or a function in C that you can use to print signed hex values?

Something similar like this

Binary   Hex (signed) Hex (unsigned)

00000010 +0x2        0x2
00000001 +0x1        0x1
00000000 +0x0        0x0
11111111 -0x1        0xFF
11111110 -0x2        0xFE
11111101 -0x3        0xFD

Unfortunately C's printf function has no way to do this directly. You could of course instead try:

printf("%s%x\n", x<0 ? "-" : "", x<0 ? -(unsigned)x : x);

This should also work for handling INT_MIN.

  • 2
    wuahhh, now thats a ugly hack... Isn't there anything other, not even in C99? if not: Damn you, C.
    – Gewure
    Mar 7 '17 at 10:52

No, but you can do something like

printf("%c%04X", (x<0) ? '-' : ' ', (x<0) ?-x : x);

But, as other point out, it is doubtful whether there is a valid reason to do so. According to your post, you do understand what you're asking for, so it's all your fault ;-)

  • If you don't like bugs and obfuscation, you could simply do if(x<0) printf("-") before printing the number. Because you have two possible bugs in your code.
    – Lundin
    Mar 7 '12 at 14:33
  • 3
    @Lundin: It's not obfuscation. Consider the case where the format string is much longer and involves 4 hex values to be printed as signed. Would you like an if/else tree of 16 printf statements, or a single printf statement that handles the whole thing? Mar 7 '12 at 14:42
  • 4
    @Lundin: And you second and third comment are just horribly wrong. '-' has type int in C to begin with, not char. And it's impossible to pass type char to a variadic function. %c expects an argument of type int. As for the third comment, of course the unary - operator makes sense on signed values. And signed does not mean the same thing as negative. It means (roughly) possessing a characteristic of being positive or negative. At this point I think you should stop commenting and find yourself a good book on the C language... Mar 7 '12 at 14:44
  • 5
    (1) this is C, not C++. They are completely different languages. (2) Even in C++, variadic functions are subject to default promotions. It's always impossible to pass char to printf. As for the issue with negative, you claimed for (x<0)?-x:x, "If x is signed, the code doesn't make sense to use unary - operator, because x would already be in the desired format.". I'm not sure what you meant by "format", but x does not have the desired value, which is what matters here.. Moreover, (x<0)?-x:x always has the same signedness (unsigned vs signed) as simply x. Mar 7 '12 at 15:02
  • 1
    @MichaelKrelin-hacker If an extra function call is critical to your performance, you shouldn't be using stdio.h in the first place.
    – Lundin
    Mar 7 '12 at 16:03

Here is a simpler version:

#include <stdio.h>
#include <stdib.h>
printf("%c%#x\n", "+-"[x < 0], (unsigned)abs(x));
  • This is an interesting solution. I have never seen indexing into a constant string inline like that before "+-"[ x < 0]. Of course it does depend on the value of true being 1. Perhaps "+-"[x < 0 ? 1 :0]. I have added this to my bag of tricks now. Maybe one day I will find a place to use it. Thanks! May 6 at 5:26
  • @AshleyDuncan: You can count on it! comparison operators are defined to evaluate to 0 or 1 in C. "+-"[x < 0 ? 1 : 0] would be redundant. Note that this expression could also be written in a more obfuscated style: (x < 0)["+-"].
    – chqrlie
    May 6 at 7:45

What do you mean by a "signed" hexadecimal value? Do you want something like "-e0"? If so, what would that mean? The sign is already represented in the hexadecimal display, since it shows all the bits and that's all the information that's in the number.

  • 1
    Well, it can be interpreted as value in hexadecimal with sign in front of it. Whether one may want it for valid reason is another issue :) Mar 7 '12 at 14:12

Your Answer

