Convert an integer to a string? Plan B?

  • Thread starter Steven T. Hatton
  • Start date
D

Dietmar Kuehl

Pete said:
#define XSTR(x) #x
#define STR(x) XSTR(x)
STR(INT_MIN)

But note that on some implementations, INT_MIN is defined more like
(-2147483657-1).

.... or as '(~0)'.
 
P

Pete Becker

hmm..IIRC Its you that started us on this rocky road, further up this
thread. Now look where its ended up using nested macros!

Well, that's a pretty standard idiom. And it's far easier than
installing a preprocessor library just so you don't have to write a
two-line macro.
OK How about this... just to initalise that value:

How about this:

#if INT_MIN == 0xffff
const char *int_min = "0xffff";
#elif INT_MIN == 0xffffffff
const char *int_min = "0xffffffff";
#elif INT_MIN == 0xffffffffffffffff
const char *int_min = "0xffffffffffffffff";
#else
#error Don't know how to initialize int_min
#endif
 
A

andy

Pete said:
How about this:

#if INT_MIN == 0xffff
const char *int_min = "0xffff";
#elif INT_MIN == 0xffffffff
const char *int_min = "0xffffffff";
#elif INT_MIN == 0xffffffffffffffff
const char *int_min = "0xffffffffffffffff";
#else
#error Don't know how to initialize int_min
#endif

Sure, but its pragmatic rather than elegant.

regards
Andy Little
 
A

andy

Dietmar said:
... or as '(~0)'.

It occurs though that "technically" the user must be happy with that.
Whatever the output actually is in this case its "technically" correct.
There is no detailed specification for this output after all. Its just
"some representation" of the input , which the above fulfills ... :)

regards
Andy Little
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,197
Latest member
Sean29G025

Latest Threads

Top