Translating a DEFINE to char*

Discussion in 'C Programming' started by marcwentink@hotmail.com, Jan 5, 2006.

  1. Guest

    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.
    , Jan 5, 2006
    #1
    1. Advertising

  2. Guest

    <> wrote:
    > #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.


    --
    :wq
    ^X^Cy^K^X^C^C^C^C
    , Jan 5, 2006
    #2
    1. Advertising

  3. wrote:
    > 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
    Antonio Contreras, Jan 5, 2006
    #3
  4. iamgodk Guest

    ÓÚ Thu, 05 Jan 2006 00:38:32 -0800£¬Ð´µ½£º

    > 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
    ....
    */

    >
    > 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.
    iamgodk, Jan 5, 2006
    #4
  5. iamgodk wrote:
    > 于 Thu, 05 Jan 2006 00:38:32 -0800,写到:


    > > The code below is compiling:
    > >
    > > #define BUF_SZ 16383
    > > ....
    > > strcat(ConnectString, "Content-Length: BUF_SZ\n");

    > 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?
    Nick Keighley, Jan 5, 2006
    #5
  6. Chuck F. Guest

    wrote:
    >
    > 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/>
    Chuck F., Jan 5, 2006
    #6
  7. Guest

    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.
    , Jan 5, 2006
    #7
  8. kaikai Guest

    <>
    ??????:...
    > 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
    kaikai, Jan 5, 2006
    #8
  9. Targeur fou Guest

    wrote:
    > 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
    Targeur fou, Jan 5, 2006
    #9
  10. Eric Sosman Guest

    kaikai wrote:

    > <>
    > ??????:...
    >
    >>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");


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

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 5, 2006
    #10
  11. vishnu Guest

    #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
    vishnu, Jan 6, 2006
    #11
  12. kaikai Guest

    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.

    >
    > #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 ?

    > --
    > Eric Sosman
    > lid



    kaikai
    kaikai, Jan 8, 2006
    #12
  13. Eric Sosman Guest

    kaikai wrote:

    > 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 <limits.h>.)

    >> #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 ?


    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.

    --
    Eric Sosman
    lid
    Eric Sosman, Jan 9, 2006
    #13
  14. kaikai Guest

    Eric Sosman wrote:
    > kaikai wrote:
    >
    > > 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.

    > --
    > Eric Sosman
    > lid


    kaikai
    kaikai, Jan 11, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    539
  2. wwj
    Replies:
    24
    Views:
    2,480
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    458
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    570
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,011
    Ian Collins
    May 9, 2006
Loading...

Share This Page