kaikai said:
Eric Sosman wrote:
kaikai wrote:
[snip]
no need to call snprintf at run time, the compiler could do that. try this
#define BUF_SZ 16383
#define TO_STR(a) _TO_STR(a)
#define _TO_STR(a) #a
strcat(ConnectString, "Content-Length: " TO_STR(BUF_SZ) "\n");
This could be made conforming by changing the name of
_TO_STR to avoid using the reserved identifier. However, it's
probably better to construct the string at run-time with
sprintf() or some such than to depend on the lexical form of
the BUF_SZ definition. Consider what happens when somebody
rewrites BUF_SZ in a more mnemonic way, e.g.
Well, that's a problem after he does rewrite the macro. I mean he will
confuse himself by doing that, but not by the TO_STR macro.
My point is that stringizing the macro gives you the
literal text of the macro definition. If what you want is
the numeric value that the definition produces, stringizing
is not the most reliable way to get it: You're depending on
the macro being written in exactly the numeric form required,
but the macro may have actually been written in some other
form. (There are often excellent reasons for doing this;
you can probably find an example by examining the INT_MIN
definition in your implementation's said:
btw. Compilers will generate warnings on the duplicated definitions of
macro, right ?
Yes: If all these definitions appear in one translation
unit (and aren't #undef'ed, suppressed with #if, or otherwise
made invisible), a diagnostic is required. I was just trying
to show some plausible ways the BUF_SZ macro might be written
(and that would make trouble when stringized), not to suggest
that it would be written in all these ways at once.