473,883 Members | 1,626 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 21880
"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
6182
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 example out there on how to transform a simple XML string using XSL? -AC
1
3106
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
3472
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
3901
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
10174
by: minguskhan | last post by:
Does anyone know how to reverse a string using a loop?
2
10336
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 decoded are unicode string. I'm trying to use
11
3964
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
2460
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
3157
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 for the pivot table code cannot be fixed. How do i do this? ...
0
9943
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10750
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10858
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9577
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7974
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5996
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4619
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
2
4225
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3237
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.