concatenate a constant to constant string using macros

S

sinbad

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
 
S

sinbad

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
 
D

Daniel Pitts

sinbad said:
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.
 
R

rahul

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

sinbad

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
 
D

Daniel Pitts

sinbad said:
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?
 
S

sinbad

sinbad said:
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".

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

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
 
S

sinbad

[...]

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top