45

I have a function that gets an unsigned long variable as parameter and I want to print it in Hex.

What is the correct way to do it?

Currently, I use printf with "%lx"

void printAddress(unsigned long address) {
    printf("%lx\n", address);
}

Should I look for a printf pattern for unsigned long hex? (and not just "long hex" as mentioned above)

Or does printf convert numbers to hex only using the bits? - so I should not care about the sign anyway?

Edit/Clarification

This question was rooted in a confusion: hex is just another way to express bits, which means that signed/unsigned number is just an interpretation. The fact that the type is unsigned long therefore doesn't change the hex digits. Unsigned just tells you how to interpret those same bits in your computer program.

11
  • 2
    What problem are you facing with %lx ? This should be fine for an unsigned long. Of course if your address parameter really is an address then you should probably be passing it as void * and printing it with %p.
    – Paul R
    Commented Apr 8, 2015 at 8:26
  • This address is not a real pointer in a C program - it is an address of a simulated machine, not always in the same size as a pointer on my machine. Commented Apr 8, 2015 at 8:29
  • 3
    What printf lacks is a way to print signed hexadecimal numbers...
    – rodrigo
    Commented Apr 8, 2015 at 8:30
  • 1
    Actually I now see that in cplusplus.com it is written very boldly that "x" is unsigned hex. You were right, my question is not very useful... Commented Apr 8, 2015 at 8:52
  • 2
    @SomethingSomething Passing a negative long and printing via "%lx: is undefined behavior. The code need not print based on a sign agnostic bit pattern, nor not print at all, it is UB. With "%lx", pass an unsigned long.
    – chux
    Commented Mar 3, 2021 at 12:57

2 Answers 2

45

You're doing it right.

From the manual page:

o, u, x, X

The unsigned int argument is converted to unsigned octal (o), unsigned decimal (u), or unsigned hexadecimal (x and X) notation.

So the value for x should always be unsigned. To make it long in size, use:

l

(ell) A following integer conversion corresponds to a long int or unsigned long int argument [...]

So %lx is unsigned long. An address (pointer value), however, should be printed with %p and cast to void *.

2
  • So actually the conclusion is that "%lx" is anyway unsigned.. Thanks! Commented Apr 8, 2015 at 8:32
  • "So %lx is unsigned long" sounds like the format specifier is unsigned long. Better stated as "%lx" should by used with unsigned long.
    – chux
    Commented Mar 2, 2021 at 22:04
11

I think the following format specifier should work give it a try

printf("%#lx\n",address);

2
  • 9
    Close, but no cigar! Either use printf("0x%lx\n",address); or printf("%#lx\n",address); but not both. The first will print the value 0UL as 0x0 while the second will just output 0. This suggestion will teach the OP something but is unrelated to the question.
    – chqrlie
    Commented Apr 8, 2015 at 8:48
  • Just a note, @Himanshu's answer works if you substitute the number of digits to print for "#". For example, num = 10UL; printf("%04lx\n", num); will print "000A". Commented Jul 25 at 12:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.