By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,352 Members | 1,593 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,352 IT Pros & Developers. It's quick & easy.

concatenate a constant to constant string using macros

P: n/a
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
Jun 27 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a
"sinbad" <si***********@gmail.comwrote in message
news:76**********************************@x19g2000 prg.googlegroups.com...
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 <ENTERto exit...");
getchar();
return 0;
}

__________________________________________________ ___________________

Jun 27 '08 #2

P: n/a
sinbad wrote:
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.
This is Question 11.17 in the comp.lang.c Frequently
Asked Questions (FAQ) list at <http://www.c-faq.com/>.
The solution given there will work for the ABC you've shown,
but if you have something like

#define ABC (24 + 18)

you'll get "The value of ABC is (24 + 18)", not "... 42".

--
Eric Sosman
es*****@ieee-dot-org.invalid
Jun 27 '08 #3

P: n/a
On Jun 18, 8:01*am, "Chris Thomasson" <cris...@comcast.netwrote:
"sinbad" <sinbad.sin...@gmail.comwrote in message

news:76**********************************@x19g2000 prg.googlegroups.com...
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 <ENTERto 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
Jun 27 '08 #4

P: n/a
"sinbad" <si***********@gmail.comwrote in message
news:66**********************************@d45g2000 hsc.googlegroups.com...
On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcast.netwrote:
"sinbad" <sinbad.sin...@gmail.comwrote in message

news:76**********************************@x19g2000 prg.googlegroups.com...
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:
__________________________________________________ ___________________
[...]
__________________________________________________ ___________________
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.
Humm... Well, if that won't work, then about the only other thing I can
think about is:


#include <stdio.h>

#define PLACE(t)t

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

#define CONCAT_X(t1, t2)t1##t2
#define CONCAT(t1, t2)CONCAT_X(t1, t2)

#define ABC_VAL 100

#define GET_VAL(t)PLACE(CONCAT(t, _VAL))

#define MYSTR_1 "The value of ABC is"
#define MYSTR_2 The value of ABC is

#define MYSTR_1_CONCAT MYSTR_1 " " QUOTE(GET_VAL(ABC))
#define MYSTR_2_CONCAT QUOTE(MYSTR_2 GET_VAL(ABC))

int main() {
printf("%s\n", MYSTR_1_CONCAT);
printf("%s\n", MYSTR_2_CONCAT);
/*------------------------------------------------------------*/
puts("\n\n_______________________\
___________________________________\npress <ENTERto exit...");
getchar();
return 0;
}

Other than something like that, I am not sure you can do what you want to...

;^(


Although, I am not a pre-processor expert...

;^)
Jun 27 '08 #5

P: n/a
sinbad wrote:
On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcast.netwrote:
>"sinbad" <sinbad.sin...@gmail.comwrote in message

news:76**********************************@x19g200 0prg.googlegroups.com...
>>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 <ENTERto 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/>
Jun 27 '08 #6

P: n/a
On Jun 18, 9:40 am, sinbad <sinbad.sin...@gmail.comwrote:
On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcast.netwrote:
"sinbad" <sinbad.sin...@gmail.comwrote in message
news:76**********************************@x19g2000 prg.googlegroups.com...
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 <ENTERto 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?
Jun 27 '08 #7

P: n/a
On Jun 18, 12:48 pm, rahul <rahulsin...@gmail.comwrote:
On Jun 18, 9:40 am, sinbad <sinbad.sin...@gmail.comwrote:
On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcast.netwrote:
"sinbad" <sinbad.sin...@gmail.comwrote in message
>news:76**********************************@x19g200 0prg.googlegroups.com...
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 <ENTERto 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

Jun 27 '08 #8

P: n/a
sinbad wrote:
On Jun 18, 12:48 pm, rahul <rahulsin...@gmail.comwrote:
>On Jun 18, 9:40 am, sinbad <sinbad.sin...@gmail.comwrote:
>>On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcast.netwrote:
"sinbad" <sinbad.sin...@gmail.comwrote in message
news:76**********************************@x19g2 000prg.googlegroups.com...
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 <ENTERto 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".
Just write them next to each other, like

const char* string = A B;
or
char array[] = A B;
or
puts(A B);

Adjacent string literals in source code are automatically
joined together to produce the same effect as one longer
literal. This happens after macro expansion, so the literals
produced by expanding A and B are adjacent and will be
joined.

--
Er*********@sun.com
Jun 27 '08 #9

P: n/a
sinbad wrote:
On Jun 18, 12:48 pm, rahul <rahulsin...@gmail.comwrote:
>On Jun 18, 9:40 am, sinbad <sinbad.sin...@gmail.comwrote:
>>On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcast.netwrote:
"sinbad" <sinbad.sin...@gmail.comwrote in message
news:76**********************************@x19g2 000prg.googlegroups.com...
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 <ENTERto 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/>
Jun 27 '08 #10

P: n/a
On Jun 19, 4:07*am, Daniel Pitts
<newsgroup.spamfil...@virtualinfinity.netwrote:
sinbad wrote:
On Jun 18, 12:48 pm, rahul <rahulsin...@gmail.comwrote:
On Jun 18, 9:40 am, sinbad <sinbad.sin...@gmail.comwrote:
>On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcast.netwrote:
"sinbad" <sinbad.sin...@gmail.comwrote in message
news:76**********************************@x19g2 000prg.googlegroups.com...
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
Jun 27 '08 #11

P: n/a
"sinbad" <si***********@gmail.comwrote in message
news:d7**********************************@m45g2000 hsb.googlegroups.com...
[...]

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!

Jun 27 '08 #12

P: n/a
On Jun 19, 9:28 pm, "Chris Thomasson" <cris...@comcast.netwrote:
"sinbad" <sinbad.sin...@gmail.comwrote in message

news:d7**********************************@m45g2000 hsb.googlegroups.com...
[...]

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
Jun 27 '08 #13

P: n/a

"sinbad" <si***********@gmail.comwrote in message
news:44**********************************@p25g2000 pri.googlegroups.com...
On Jun 19, 9:28 pm, "Chris Thomasson" <cris...@comcast.netwrote:
>"sinbad" <sinbad.sin...@gmail.comwrote in message

news:d7**********************************@m45g200 0hsb.googlegroups.com...
[...]

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.
;^)

Jun 27 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.