using defined characters as strings

Discussion in 'C Programming' started by Martijn, Mar 23, 2005.

  1. Martijn

    Martijn Guest

    Hi,

    I think I can best ask my question by example. I may be overlooking
    something, but this is like what I have:

    #define ID1 'i'
    #define ID2 'd'
    #define ID3 'n'

    This is what I want in my output file:

    i=100:d=200:n=300

    This is what I do:

    sprintf("%c=%d:%c=%d:%c=%d\n",
    ID1, 100,
    ID2, 200,
    ID3, 300);

    That means that the characters are inserted run-time. Is there a way to
    make them part of the string-constant so that I can put them into my string
    compile-time?

    Sorry if the question is formulated somewhat strange :) Thanks for any
    help!

    --
    Martijn
    http://www.sereneconcepts.nl
    Martijn, Mar 23, 2005
    #1
    1. Advertising

  2. Martijn

    Michael Mair Guest

    Martijn wrote:
    > Hi,
    >
    > I think I can best ask my question by example. I may be overlooking
    > something, but this is like what I have:
    >
    > #define ID1 'i'
    > #define ID2 'd'
    > #define ID3 'n'
    >
    > This is what I want in my output file:
    >
    > i=100:d=200:n=300
    >
    > This is what I do:
    >
    > sprintf("%c=%d:%c=%d:%c=%d\n",


    You either forgot the buffer or use the wrong *printf() function.

    > ID1, 100,
    > ID2, 200,
    > ID3, 300);
    >
    > That means that the characters are inserted run-time. Is there a way to
    > make them part of the string-constant so that I can put them into my string
    > compile-time?
    >
    > Sorry if the question is formulated somewhat strange :) Thanks for any
    > help!


    Does
    #define STRINGIZE(S) #S
    #define XSTR(S) STRINGIZE(S)

    #define ID1 i

    ....
    printf(XSTR(ID1)"=%d",100)

    help you or do you need the character constants elsewhere?


    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Mar 23, 2005
    #2
    1. Advertising

  3. Martijn

    CBFalconer Guest

    Martijn wrote:
    >
    > Hi,
    >
    > I think I can best ask my question by example. I may be overlooking
    > something, but this is like what I have:
    >
    > #define ID1 'i'
    > #define ID2 'd'
    > #define ID3 'n'
    >
    > This is what I want in my output file:
    >
    > i=100:d=200:n=300
    >
    > This is what I do:
    >
    > sprintf("%c=%d:%c=%d:%c=%d\n",
    > ID1, 100,
    > ID2, 200,
    > ID3, 300);
    >
    > That means that the characters are inserted run-time. Is there a way to
    > make them part of the string-constant so that I can put them into my string
    > compile-time?


    sprintf("i=%d:d=%d:n=%d\n", 100, 200, 300);

    --
    "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
    CBFalconer, Mar 24, 2005
    #3
  4. CBFalconer <> scribbled the following:
    > Martijn wrote:
    >> Hi,
    >>
    >> I think I can best ask my question by example. I may be overlooking
    >> something, but this is like what I have:
    >>
    >> #define ID1 'i'
    >> #define ID2 'd'
    >> #define ID3 'n'
    >>
    >> This is what I want in my output file:
    >>
    >> i=100:d=200:n=300
    >>
    >> This is what I do:
    >>
    >> sprintf("%c=%d:%c=%d:%c=%d\n",
    >> ID1, 100,
    >> ID2, 200,
    >> ID3, 300);
    >>
    >> That means that the characters are inserted run-time. Is there a way to
    >> make them part of the string-constant so that I can put them into my string
    >> compile-time?


    > sprintf("i=%d:d=%d:n=%d\n", 100, 200, 300);


    A very nice solution, but you're still using sprintf() in the wrong way.
    Do you mean printf()?

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-------------------------------------------------------- rules! --------/
    "There's no business like slow business."
    - Tailgunner
    Joona I Palaste, Mar 24, 2005
    #4
  5. Martijn

    CBFalconer Guest

    Joona I Palaste wrote:
    > CBFalconer <> scribbled the following:
    >> Martijn wrote:
    >>>
    >>> I think I can best ask my question by example. I may be overlooking
    >>> something, but this is like what I have:
    >>>
    >>> #define ID1 'i'
    >>> #define ID2 'd'
    >>> #define ID3 'n'
    >>>
    >>> This is what I want in my output file:
    >>>
    >>> i=100:d=200:n=300
    >>>
    >>> This is what I do:
    >>>
    >>> sprintf("%c=%d:%c=%d:%c=%d\n",
    >>> ID1, 100,
    >>> ID2, 200,
    >>> ID3, 300);
    >>>
    >>> That means that the characters are inserted run-time. Is there
    >>> a way to make them part of the string-constant so that I can
    >>> put them into my string compile-time?

    >
    >> sprintf("i=%d:d=%d:n=%d\n", 100, 200, 300);

    >
    > A very nice solution, but you're still using sprintf() in the
    > wrong way. Do you mean printf()?


    Yup. I guess the OP did also.

    --
    "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
    CBFalconer, Mar 24, 2005
    #5
  6. On Thu, 24 Mar 2005 00:10:06 +0100, "Martijn"
    <> wrote:

    >Hi,
    >
    >I think I can best ask my question by example. I may be overlooking
    >something, but this is like what I have:
    >
    > #define ID1 'i'
    > #define ID2 'd'
    > #define ID3 'n'
    >
    >This is what I want in my output file:
    >
    > i=100:d=200:n=300
    >
    >This is what I do:
    >
    > sprintf("%c=%d:%c=%d:%c=%d\n",
    > ID1, 100,
    > ID2, 200,
    > ID3, 300);
    >
    >That means that the characters are inserted run-time. Is there a way to
    >make them part of the string-constant so that I can put them into my string
    >compile-time?
    >


    If you change the defines to specify double quotes (") instead of
    single quotes ('), then your format string can be coded as
    ID1 "=%d:" ID2 "=%d:" ID3 "=%d\n"
    since the compiler will automatically merge adjacent quoted strings
    together.


    <<Remove the del for email>>
    Barry Schwarz, Mar 25, 2005
    #6
    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. Oodini
    Replies:
    1
    Views:
    1,762
    Keith Thompson
    Sep 27, 2005
  2. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    755
    Malcolm
    Jun 24, 2006
  3. Angel Tsankov
    Replies:
    1
    Views:
    864
    Victor Bazarov
    Apr 5, 2006
  4. Replies:
    1
    Views:
    495
    Sion Arrowsmith
    Jul 10, 2008
  5. Keith Thompson

    Re: __STDC_IEC_559__ (defined or !defined ?)

    Keith Thompson, Aug 17, 2010, in forum: C Programming
    Replies:
    0
    Views:
    441
    Keith Thompson
    Aug 17, 2010
Loading...

Share This Page