2

I have an unsigned int variable x which value is comprised between from 0 and 0xFFFFF and I would like to print it in a way more readable for the user.
If x is greater than 0xF000, print it in hexadecimal, else print it in decimal. So something like this would do it

if (x > 0xF000) 
   printf("%#x", x);
else 
   printf("%u", x)

But I would like to know if there is an handier and smarter way to handle this, like conditional formatting for output depending of their value.

4
  • 4
    What you have is probably the easiest to read. I'd stop here unless given a very good reason to complicate things. Commented Nov 20, 2019 at 16:19
  • 2
    If your program's output is ever read by a machine, or by an uninitiated human, the changing representation could be very confusing. If you really want to do this, I strongly recommend marking the output format somehow, perhaps by prefixing the hexadecimal numbers with 0x, which you can do either with "0x%x" or "%#x". Commented Nov 20, 2019 at 16:27
  • Besides the other problems mentioned, calling printf with a format which is not a single, constant string is arguably poor style. It is disrecommended by some style guides. I believe it is diagnosed by some compilers. It means that the compiler can't, in general, do any checking for mismatches between the format string and the arguments. Commented Nov 20, 2019 at 16:36
  • 1
    See also Why is printf with a single argument (without conversion specifiers) deprecated? (though it's a somewhat different question). Commented Nov 20, 2019 at 17:08

1 Answer 1

1

Your 'conditional format' could be specified using the ternary '?' operator, as suggested in the comments:

printf((x > 0xF000) ? "%#x" : "%u", x);

However, as also suggested in the comments, this doesn't really help in terms of readability.

You can also use a string variable as the format argument, for example (in this trivial case):

char fmt[4];
if (x > 0xF000) strcpy(fmt, "%x");
else            strcpy(fmt, "%u"); // You haven't specified what type "x" is, so let's assume "unsigned".
printf(fmt, x);
18
  • The second approach is not good in case you wanted to add a new-line or something else.
    – machine_1
    Commented Nov 20, 2019 at 16:31
  • @machine_1 - Well, assuming you make fmt a big enough buffer, you can put whatever you want (including newlines) into it. But I'm not saying it's the best way - just one way. Commented Nov 20, 2019 at 16:33
  • Wouldn't a buffer of size 3 be sufficient in the second example?
    – bool3max
    Commented Nov 20, 2019 at 16:34
  • @AnttiHaapala Of course, one could equally well use char* fmt and initialise to a string-literal. Commented Nov 20, 2019 at 16:35
  • 1
    @Mahouk You should post a separate question! And be very specific and explicit in your title. There are many folks here on SO far more 'clued-up' than I am on such methods. Commented Nov 21, 2019 at 12:22

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.