concatenate a constant to constant string using macros

Discussion in 'C Programming' started by sinbad, Jun 18, 2008.

  1. sinbad

    sinbad Guest

    hi,
    how to concatenate a "hash defined" constant value to another "hash
    defined" constant string. For example

    #define ABC 100

    #define MYSTR "The value of ABC is"

    Now i need a string that will concatenate the value of ABC to MYSTR .
    I need this at compile time.

    The resultant constant string should be "The value of ABC is 100"

    How can i do this.

    thanks
    sinbad
     
    sinbad, Jun 18, 2008
    #1
    1. Advertising

  2. sinbad

    sinbad Guest

    On Jun 18, 8:01 am, "Chris Thomasson" <> wrote:
    > "sinbad" <> wrote in message
    >
    > news:...
    >
    > > hi,
    > > how to concatenate a "hash defined" constant value to another "hash
    > > defined" constant string. For example

    >
    > > #define ABC 100

    >
    > > #define MYSTR "The value of ABC is"

    >
    > > Now i need a string that will concatenate the value of ABC to MYSTR .
    > > I need this at compile time.

    >
    > > The resultant constant string should be "The value of ABC is 100"

    >
    > > How can i do this.

    >
    > You can do something like:
    > _____________________________________________________________________
    > #include <stdio.h>
    >
    > #define QUOTE_X(t)#t
    > #define QUOTE(t)QUOTE_X(t)
    >
    > #define ABC 100
    > #define MYSTR "The value of ABC is"
    >
    > int main() {
    >   char const concat[] = MYSTR " " QUOTE(ABC);
    >   printf("%s\n", concat);
    >
    > /*------------------------------------------------------------*/
    >   puts("\n\n_______________________\
    > ___________________________________\npress <ENTER> to exit...");
    >   getchar();
    >   return 0;
    >
    > }
    >
    > _____________________________________________________________________


    chris,
    But i need to do this at compile time, meaing i can't use the code
    u've written in main () function.
    Because i will get the resultant string only during execution time.

    thanks
    sinbad
     
    sinbad, Jun 18, 2008
    #2
    1. Advertising

  3. sinbad

    Daniel Pitts Guest

    sinbad wrote:
    > On Jun 18, 8:01 am, "Chris Thomasson" <> wrote:
    >> "sinbad" <> wrote in message
    >>
    >> news:...
    >>
    >>> hi,
    >>> how to concatenate a "hash defined" constant value to another "hash
    >>> defined" constant string. For example
    >>> #define ABC 100
    >>> #define MYSTR "The value of ABC is"
    >>> Now i need a string that will concatenate the value of ABC to MYSTR .
    >>> I need this at compile time.
    >>> The resultant constant string should be "The value of ABC is 100"
    >>> How can i do this.

    >> You can do something like:
    >> _____________________________________________________________________
    >> #include <stdio.h>
    >>
    >> #define QUOTE_X(t)#t
    >> #define QUOTE(t)QUOTE_X(t)
    >>
    >> #define ABC 100
    >> #define MYSTR "The value of ABC is"
    >>
    >> int main() {
    >> char const concat[] = MYSTR " " QUOTE(ABC);
    >> printf("%s\n", concat);
    >>
    >> /*------------------------------------------------------------*/
    >> puts("\n\n_______________________\
    >> ___________________________________\npress <ENTER> to exit...");
    >> getchar();
    >> return 0;
    >>
    >> }
    >>
    >> _____________________________________________________________________

    >
    > chris,
    > But i need to do this at compile time, meaing i can't use the code
    > u've written in main () function.
    > Because i will get the resultant string only during execution time.
    >
    > thanks
    > sinbad

    It is done at compile time. he's just printing the result of the
    concatination.
    The line that has 'MYSTR " " QUOTE(ABC)' in it is actually concatenating
    the string at compile time.
    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jun 18, 2008
    #3
  4. sinbad

    rahul Guest

    On Jun 18, 9:40 am, sinbad <> wrote:
    > On Jun 18, 8:01 am, "Chris Thomasson" <> wrote:
    >
    >
    >
    > > "sinbad" <> wrote in message

    >
    > >news:...

    >
    > > > hi,
    > > > how to concatenate a "hash defined" constant value to another "hash
    > > > defined" constant string. For example

    >
    > > > #define ABC 100

    >
    > > > #define MYSTR "The value of ABC is"

    >
    > > > Now i need a string that will concatenate the value of ABC to MYSTR .
    > > > I need this at compile time.

    >
    > > > The resultant constant string should be "The value of ABC is 100"

    >
    > > > How can i do this.

    >
    > > You can do something like:
    > > _____________________________________________________________________
    > > #include <stdio.h>

    >
    > > #define QUOTE_X(t)#t
    > > #define QUOTE(t)QUOTE_X(t)

    >
    > > #define ABC 100
    > > #define MYSTR "The value of ABC is"

    >
    > > int main() {
    > > char const concat[] = MYSTR " " QUOTE(ABC);
    > > printf("%s\n", concat);

    >
    > > /*------------------------------------------------------------*/
    > > puts("\n\n_______________________\
    > > ___________________________________\npress <ENTER> to exit...");
    > > getchar();
    > > return 0;

    >
    > > }

    >
    > > _____________________________________________________________________

    >
    > chris,
    > But i need to do this at compile time, meaing i can't use the code
    > u've written in main () function.
    > Because i will get the resultant string only during execution time.
    >
    > thanks
    > sinbad


    Static allocation will happen at compile time. What makes you think
    the string is allocated at execution time?
     
    rahul, Jun 18, 2008
    #4
  5. sinbad

    sinbad Guest

    On Jun 18, 12:48 pm, rahul <> wrote:
    > On Jun 18, 9:40 am, sinbad <> wrote:
    >
    >
    >
    > > On Jun 18, 8:01 am, "Chris Thomasson" <> wrote:

    >
    > > > "sinbad" <> wrote in message

    >
    > > >news:...

    >
    > > > > hi,
    > > > > how to concatenate a "hash defined" constant value to another "hash
    > > > > defined" constant string. For example

    >
    > > > > #define ABC 100

    >
    > > > > #define MYSTR "The value of ABC is"

    >
    > > > > Now i need a string that will concatenate the value of ABC to MYSTR .
    > > > > I need this at compile time.

    >
    > > > > The resultant constant string should be "The value of ABC is 100"

    >
    > > > > How can i do this.

    >
    > > > You can do something like:
    > > > _____________________________________________________________________
    > > > #include <stdio.h>

    >
    > > > #define QUOTE_X(t)#t
    > > > #define QUOTE(t)QUOTE_X(t)

    >
    > > > #define ABC 100
    > > > #define MYSTR "The value of ABC is"

    >
    > > > int main() {
    > > > char const concat[] = MYSTR " " QUOTE(ABC);
    > > > printf("%s\n", concat);

    >
    > > > /*------------------------------------------------------------*/
    > > > puts("\n\n_______________________\
    > > > ___________________________________\npress <ENTER> to exit...");
    > > > getchar();
    > > > return 0;

    >
    > > > }

    >
    > > > _____________________________________________________________________

    >
    > > chris,
    > > But i need to do this at compile time, meaing i can't use the code
    > > u've written in main () function.
    > > Because i will get the resultant string only during execution time.

    >
    > > thanks
    > > sinbad

    >
    > Static allocation will happen at compile time. What makes you think
    > the string is allocated at execution time?


    Execution time, i mean here is , in the following statement.

    char const concat[] = MYSTR " " QUOTE(ABC);

    the variable concat[] has no meaning at compile time, there is no
    memory allocated for it.
    I will try to explain it more simple. There are two macro constants.
    My requirement is to concatenate the two constants and produce a
    new constant. (specifically i am referring to constant strings). Like

    #define A "It is A"
    #define B "It is B"

    whatever may be the value of A and B, I need a resultant constant
    string ,which is obtained by concatenating both A and B.
    In this case i need "It is AIt is B".

    thanks
     
    sinbad, Jun 18, 2008
    #5
  6. sinbad

    Daniel Pitts Guest

    sinbad wrote:
    > On Jun 18, 12:48 pm, rahul <> wrote:
    >> On Jun 18, 9:40 am, sinbad <> wrote:
    >>
    >>
    >>
    >>> On Jun 18, 8:01 am, "Chris Thomasson" <> wrote:
    >>>> "sinbad" <> wrote in message
    >>>> news:...
    >>>>> hi,
    >>>>> how to concatenate a "hash defined" constant value to another "hash
    >>>>> defined" constant string. For example
    >>>>> #define ABC 100
    >>>>> #define MYSTR "The value of ABC is"
    >>>>> Now i need a string that will concatenate the value of ABC to MYSTR .
    >>>>> I need this at compile time.
    >>>>> The resultant constant string should be "The value of ABC is 100"
    >>>>> How can i do this.
    >>>> You can do something like:
    >>>> _____________________________________________________________________
    >>>> #include <stdio.h>
    >>>> #define QUOTE_X(t)#t
    >>>> #define QUOTE(t)QUOTE_X(t)
    >>>> #define ABC 100
    >>>> #define MYSTR "The value of ABC is"
    >>>> int main() {
    >>>> char const concat[] = MYSTR " " QUOTE(ABC);
    >>>> printf("%s\n", concat);
    >>>> /*------------------------------------------------------------*/
    >>>> puts("\n\n_______________________\
    >>>> ___________________________________\npress <ENTER> to exit...");
    >>>> getchar();
    >>>> return 0;
    >>>> }
    >>>> _____________________________________________________________________
    >>> chris,
    >>> But i need to do this at compile time, meaing i can't use the code
    >>> u've written in main () function.
    >>> Because i will get the resultant string only during execution time.
    >>> thanks
    >>> sinbad

    >> Static allocation will happen at compile time. What makes you think
    >> the string is allocated at execution time?

    >
    > Execution time, i mean here is , in the following statement.
    >
    > char const concat[] = MYSTR " " QUOTE(ABC);
    >
    > the variable concat[] has no meaning at compile time, there is no
    > memory allocated for it.
    > I will try to explain it more simple. There are two macro constants.
    > My requirement is to concatenate the two constants and produce a
    > new constant. (specifically i am referring to constant strings). Like
    >
    > #define A "It is A"
    > #define B "It is B"
    >
    > whatever may be the value of A and B, I need a resultant constant
    > string ,which is obtained by concatenating both A and B.
    > In this case i need "It is AIt is B".
    >
    > thanks
    >
    >
    >


    #define C A B

    C will result in "It is AIt is B" when finally evaluated.

    So will simply A B

    So will "It is A" "It is B". The concatenation is done at compile time
    only, not run time. How do you intend on using this result?


    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jun 19, 2008
    #6
  7. sinbad

    sinbad Guest

    On Jun 19, 4:07 am, Daniel Pitts
    <> wrote:
    > sinbad wrote:
    > > On Jun 18, 12:48 pm, rahul <> wrote:
    > >> On Jun 18, 9:40 am, sinbad <> wrote:

    >
    > >>> On Jun 18, 8:01 am, "Chris Thomasson" <> wrote:
    > >>>> "sinbad" <> wrote in message
    > >>>>news:...
    > >>>>> hi,
    > >>>>> how to concatenate a "hash defined" constant value to another "hash
    > >>>>> defined" constant string. For example
    > >>>>> #define ABC 100
    > >>>>> #define MYSTR "The value of ABC is"
    > >>>>> Now i need a string that will concatenate the value of ABC to MYSTR .
    > >>>>> I need this at compile time.
    > >>>>> The resultant constant string should be "The value of ABC is 100"
    > >>>>> How can i do this.
    > >>>> You can do something like:
    > >>>> _____________________________________________________________________
    > >>>> #include <stdio.h>
    > >>>> #define QUOTE_X(t)#t
    > >>>> #define QUOTE(t)QUOTE_X(t)
    > >>>> #define ABC 100
    > >>>> #define MYSTR "The value of ABC is"
    > >>>> int main() {
    > >>>>   char const concat[] = MYSTR " " QUOTE(ABC);
    > >>>>   printf("%s\n", concat);
    > >>>> /*------------------------------------------------------------*/
    > >>>>   puts("\n\n_______________________\
    > >>>> ___________________________________\npress <ENTER> to exit...");
    > >>>>   getchar();
    > >>>>   return 0;
    > >>>> }
    > >>>> _____________________________________________________________________
    > >>> chris,
    > >>> But i need to do this at compile time, meaing i can't use the code
    > >>> u've written in main () function.
    > >>> Because i will get the resultant string only during execution time.
    > >>> thanks
    > >>> sinbad
    > >> Static allocation will happen at compile time. What makes you think
    > >> the string is allocated at execution time?

    >
    > > Execution time, i mean here is , in the following statement.

    >
    > >  char const concat[] = MYSTR " " QUOTE(ABC);

    >
    > > the variable concat[] has no meaning at compile time, there is no
    > > memory allocated for it.
    > > I will try to explain it more simple. There are two macro constants.
    > > My requirement is to concatenate the two constants and produce a
    > > new constant. (specifically i am referring to constant strings). Like

    >
    > > #define A "It is A"
    > > #define B "It is B"

    >
    > > whatever may be the value of A and B, I need a resultant constant
    > > string ,which is obtained by concatenating both A and B.
    > > In this case i need "It is AIt is B".

    >
    > > thanks

    >
    > #define C A B
    >
    > C will result in "It is AIt is B" when finally evaluated.
    >
    > So will simply A B
    >
    > So will "It is A" "It is B".  The concatenation is done at compile time
    > only, not run time.  How do you intend on using this result?
    >
    > --
    > Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>


    Daniel,

    The problem would have been simpler if both A and B were string
    constants.
    But in the above two constants one is an integer constant. The example
    i've given was not entirely correct.

    #define A "the value of A is"
    #define B 100

    Now i need a string constant that would be "the value of A is 100"

    the following code worked for me. it prints "the value of A is 100".

    #define A "the value of A is "
    #define B 100

    #define STR(x) #x
    #define XSTR(x) STR(x)

    void foo (const char*);

    int main ()
    {
    foo (A XSTR(B));
    return 0;
    }

    void foo (const char *str)
    {
    printf("%s",str);
    }

    Thanks for all your time guys.
    sinbad
     
    sinbad, Jun 19, 2008
    #7
  8. sinbad

    sinbad Guest

    On Jun 19, 9:28 pm, "Chris Thomasson" <> wrote:
    > "sinbad" <> wrote in message
    >
    > news:...
    > [...]
    >
    > The problem would have been simpler if both A and B were string
    > constants.
    > But in the above two constants one is an integer constant. The example
    > i've given was not entirely correct.
    >
    >
    >
    > > #define A "the value of A is"
    > > #define B 100
    > > Now i need a string constant that would be "the value of A is 100"
    > > the following code worked for me. it prints "the value of A is 100".
    > > #define A "the value of A is "
    > > #define B 100
    > > #define STR(x) #x
    > > #define XSTR(x) STR(x)
    > > void foo (const char*);
    > > int main ()
    > > {
    > > foo (A XSTR(B));
    > > return 0;
    > > }
    > > void foo (const char *str)
    > > {
    > > printf("%s",str);
    > > }

    >
    > which has the same end effect as the initial solution I provided to you!


    Yeah Chris,
    I got an idea about it after referring to your program.

    Thanks for our time.
    sinbad
     
    sinbad, Jun 19, 2008
    #8
    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. Replies:
    80
    Views:
    2,516
    Stephen J. Bevan
    Nov 7, 2003
  2. Replies:
    1
    Views:
    489
    Marco Antoniotti
    Oct 7, 2003
  3. Replies:
    5
    Views:
    513
  4. Michael T. Babcock

    Re: Explanation of macros; Haskell macros

    Michael T. Babcock, Nov 3, 2003, in forum: Python
    Replies:
    0
    Views:
    540
    Michael T. Babcock
    Nov 3, 2003
  5. Carlos

    Concatenate/De-Concatenate

    Carlos, Oct 12, 2012, in forum: VHDL
    Replies:
    10
    Views:
    959
Loading...

Share This Page