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