473,545 Members | 2,049 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

concatenate a constant to constant string using macros

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
13 21789
"sinbad" <si***********@ gmail.comwrote in message
news:76******** *************** ***********@x19 g2000prg.google groups.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
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
On Jun 18, 8:01*am, "Chris Thomasson" <cris...@comcas t.netwrote:
"sinbad" <sinbad.sin...@ gmail.comwrote in message

news:76******** *************** ***********@x19 g2000prg.google groups.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
"sinbad" <si***********@ gmail.comwrote in message
news:66******** *************** ***********@d45 g2000hsc.google groups.com...
On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcas t.netwrote:
"sinbad" <sinbad.sin...@ gmail.comwrote in message

news:76******** *************** ***********@x19 g2000prg.google groups.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(A BC))
#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
sinbad wrote:
On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcas t.netwrote:
>"sinbad" <sinbad.sin...@ gmail.comwrote in message

news:76******* *************** ************@x1 9g2000prg.googl egroups.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
On Jun 18, 9:40 am, sinbad <sinbad.sin...@ gmail.comwrote:
On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcas t.netwrote:
"sinbad" <sinbad.sin...@ gmail.comwrote in message
news:76******** *************** ***********@x19 g2000prg.google groups.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
On Jun 18, 12:48 pm, rahul <rahulsin...@gm ail.comwrote:
On Jun 18, 9:40 am, sinbad <sinbad.sin...@ gmail.comwrote:
On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcas t.netwrote:
"sinbad" <sinbad.sin...@ gmail.comwrote in message
>news:76******* *************** ************@x1 9g2000prg.googl egroups.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
sinbad wrote:
On Jun 18, 12:48 pm, rahul <rahulsin...@gm ail.comwrote:
>On Jun 18, 9:40 am, sinbad <sinbad.sin...@ gmail.comwrote:
>>On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcas t.netwrote:
"sinbad" <sinbad.sin...@ gmail.comwrote in message
news:76***** *************** **************@ x19g2000prg.goo glegroups.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
sinbad wrote:
On Jun 18, 12:48 pm, rahul <rahulsin...@gm ail.comwrote:
>On Jun 18, 9:40 am, sinbad <sinbad.sin...@ gmail.comwrote:
>>On Jun 18, 8:01 am, "Chris Thomasson" <cris...@comcas t.netwrote:
"sinbad" <sinbad.sin...@ gmail.comwrote in message
news:76***** *************** **************@ x19g2000prg.goo glegroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
6157
by: Andrew Connell | last post by:
Having fits transforming an XML string using an XSL file. In the 1.1 version of the framework, I see that the XmlResolver is heavily used in the XslTransform class. However, that looks like I am only supposed to use that only when you have an xml ~file~... not an XML string (my XML is coming from a database field). Is there a cut & dry...
1
3089
by: pcb | last post by:
Hi all, Perhaps a silly question. I've been trying to format the list items in a listbox by appending a constant text string to the values. Here is the code: ******** 'Build SQL string strSQL = "SELECT tblResults.Results ...bla, bla...<snip>;" 'Populate listbox
2
3452
by: Poewood | last post by:
Okay I know this is basic C++ but can someone tell me how to concatenate char to string. Ex: char hexChar={'A','B','C','D','E','F'}; String *str ="A"; how can I do this: str = hexChar + hexChar; or str = str + hexChar; thanx,
2
3841
by: Bruno | last post by:
Hello friends I'm creating one application that will send content to another app using HTTP connection. To authenticate my connection, I need to send a string with the username and password encoded using Base64. Dim username as String Dim pass as String Dim info_user as String info_user = username + ":" + pass
3
10105
by: minguskhan | last post by:
Does anyone know how to reverse a string using a loop?
2
10260
by: aurora | last post by:
I have some unicode string with some characters encode using python notation like '\n' for LF. I need to convert that to the actual LF character. There is a 'unicode_escape' codec that seems to suit my purpose. >>> encoded = u'A\\nA' >>> decoded = encoded.decode('unicode_escape') >>> print len(decoded) 3 Note that both encoded and...
11
3918
by: wreckingcru | last post by:
I'm trying to tokenize a C++ string with the following code: #include <iostream> #include <stdio> #include <string> using namespace std; int main(int argc, char* argv)
5
2439
by: 2di | last post by:
Hi every one, I got this problem, i need to create classes in c++ by using macros. Thats what i want: #define createObject(type) type a; main(){ string className = "int"; createObject(className); }
6
3139
by: nicolenwn | last post by:
Hi everyone(: I'm having trouble creating pivot tables using macros. First i tried recording it then running the exact same thing. It worked fine the first time but a week later when i tried it again the pivot table failed me. As i am using raw data as the data source, hence the number of rows changes everyday and therefore the datasource...
0
7656
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7808
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7757
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5972
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5329
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4945
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3450
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1884
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
704
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.