Translating a DEFINE to char*

M

marcwentink

Dear people,

The code below is compiling:

#define BUF_SZ 16383
.....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.
 
U

usenet

#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.

Try snprintf(), simpe and flexible.
 
A

Antonio Contreras

Dear people,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.

Macros are not substituted inside strings or inside comments. You can
achieve what you want using sprintf like this:

char my_string[BIG_ENOUGH];

sprintf(my_string, "Content-Length: %d\n", BUF_SZ);

HTH
 
I

iamgodk

ÓÚ Thu, 05 Jan 2006 00:38:32 -0800£¬[email protected]дµ½£º
Dear people,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n");
sprintf("ConnectString, "Content-Length: %d\n",BUF_SZ);
/*
%d -- int
%u -- unsigned int
%s -- char *
%c -- char
....
*/
 
N

Nick Keighley

iamgodk said:
于 Thu, 05 Jan 2006 00:38:32 -0800,[email protected]写到:
sprintf("ConnectString, "Content-Length: %d\n",BUF_SZ);

you've gained an extra quote

sprintf(ConnectString, "Content-Length: %d\n",BUF_SZ);

<snip>

--
Nick Keighley

De maan likt niet hoog
Maar het is niet zo
De maan is wel hoog
Of niet sams?
 
C

Chuck F.

The code below is compiling:

#define BUF_SZ 16383
.....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize
used in the string, but also somehow keep it simple, and keep it
flexible.

Try the following:

#define BUF_SZ "16383"
...
strcat(ConnectString, "Content-Length: " BUF_SZ "\n");

assuming you can modify the #define as shown.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
M

marcwentink

Chuck
modify the #define

Then I could rather make two defines:

#define BUF_SZ 16383
#define CONTENTLENGTH "Contect-Length 16383 \n"
.....
strcat(ConnectString, CONTENTLENGTH);

but I think the "sprintf(ConnectString, "Content-Length:
%d\n",BUF_SZ);" is the neatest.
 
K

kaikai

Dear people,

The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.

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");

kaikai
 
T

Targeur fou

Dear people,
Hello,


The code below is compiling:

#define BUF_SZ 16383
....
strcat(ConnectString, "Content-Length: BUF_SZ\n");

but it does not work since it give me:

Content-Length: BUF_SZ and not
Content-Length: 16838

Is there a way to handle this? What I want is to get buffersize used in
the string, but also somehow keep it simple, and keep it flexible.

Here is another solution to stringize (I think it's in the FAQ) your
macro:

#include <stdio.h>
#include <string.h>

#define BUF_SZ 16383

#define STR(n) #n
#define MKSTR(n) STR(n)

int main(void)
{
char ConnectString[50+1];
*ConnectString = '\0';
strncat(ConnectString,"Content-Length: " MKSTR (BUF_SZ) "\n", 50);
puts(ConnectString);
return 0;
}

Regis
 
E

Eric Sosman

kaikai said:
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.

#define BUF_SZ ((1 << 14) - 1)
#define BUF_SZ (HEADER_LEN + PREFIX_SZ + BODY_MAX)
#define BUF_SZ 0x3fff
...
 
V

vishnu

#define BUF_SZ #16383
strcat(ConnectionString,"Content Length:" BUF_SZ "\n");
1.Since ANSI Cocatenates automatically strings
e.g
if u write
printf("Vishnu " "Tiwari");
output:
Vishnu Tiwari
2.
#define var #name
replaces var by "name"
thus three after preprocessor execution it looks to compiler
strcat( ConnectionString,"Content Length:" "16383" "\n");
after joining of strings it becomes
strcat( ConnectionString,"Content Length:16383\n");
Tha same as u wished if u change value of BUF_SZ it will be reflected
in the program.
Hope it will help u .
else contact me at
(e-mail address removed)
 
K

kaikai

Eric said:
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.
#define BUF_SZ ((1 << 14) - 1)
#define BUF_SZ (HEADER_LEN + PREFIX_SZ + BODY_MAX)
#define BUF_SZ 0x3fff
...

btw. Compilers will generate warnings on the duplicated definitions of
macro, right ?


kaikai
 
E

Eric Sosman

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.
 
K

kaikai

Eric said:
kaikai said:
Eric Sosman wrote:
[snip]

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.

O, IC. I misunderstand you. At this point, run-time generated strings
would be better, you're right.

kaikai
 

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,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top