LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   #define and strings (https://www.linuxquestions.org/questions/programming-9/define-and-strings-782415/)

rafton 01-15-2010 10:43 AM

#define and strings
 
Hello,
I would like to create a string constant with a #define and concatenate it with another string.
I thought doing this:

#define USB blabla
static char[256] = {"/"USB"5"};

in hope to print this --> "/blabla5"

What is the rightn way to do?

Please, I need your help.

Raf

Sergei Steshenko 01-15-2010 11:08 AM

Quote:

Originally Posted by rafton (Post 3827669)
Hello,
I would like to create a string constant with a #define and concatenate it with another string.
I thought doing this:

#define USB blabla
static char[256] = {"/"USB"5"};

in hope to print this --> "/blabla5"

What is the rightn way to do?

Please, I need your help.

Raf

Visit http://gcc.gnu.org/onlinedocs/cpp/ -> http://gcc.gnu.org/onlinedocs/cpp/Co...#Concatenation .

ForzaItalia2006 01-15-2010 04:38 PM

Hey,

Quote:

Originally Posted by rafton (Post 3827669)
I would like to create a string constant with a #define and concatenate it with another string.

Concatenation of string literals (constants) in C is realized by just writing:

Code:

char *str = "String1" "String2"; // "String1String2"
The compiler will then concatenate the strings for you in the output file. If you want to concatenate strings whereof one is NOT a constant, just use the strcat/strncat C library function.

Quote:

Originally Posted by rafton (Post 3827669)
I thought doing this:
Code:

#define USB blabla
static char[256] = {"/"USB"5"};


You could do this:

Code:

#define USB "blabla"
static char str[256] {"/" USB "5"};

I hope that is what you're searching for ...

- Andi -

bigearsbilly 01-15-2010 05:53 PM

yes but why?
it's a bit naff.

Code:

#include <stdio.h>
#define USB "blabla"

main(){

    static char buff[256] = "/" USB "5";
    puts(buff);
}

edit:doh!
been done

Sergei Steshenko 01-16-2010 04:24 AM

http://gcc.gnu.org/onlinedocs/cpp/St...tringification

ForzaItalia2006 01-16-2010 05:02 AM

Quote:

Originally Posted by Sergei Steshenko (Post 3828468)

Yes, that could be used as well, but you need to take care about this restriction:

Unlike normal parameter replacement, the argument is not macro-expanded first. This is called stringification.

That's how you could theoretically do it in C with macros:

Code:

#define UBS blabla
#define stringify(s) #s

char str[256] = {"/"stringify(UBS)"5"}; // "/UBS5"
char str1[256] = {"/" stringify(blabla) "5"}; // "/blabla5"

- Andi -

Sergei Steshenko 01-16-2010 05:10 AM

Quote:

Originally Posted by ForzaItalia2006 (Post 3828488)
Yes, that could be used as well, but you need to take care about this restriction:

Unlike normal parameter replacement, the argument is not macro-expanded first. This is called stringification.

That's how you could theoretically do it in C with macros:

Code:

#define UBS blabla
#define stringify(s) #s

char str[256] = {"/"stringify(UBS)"5"}; // "/UBS5"
char str1[256] = {"/" stringify(blabla) "5"}; // "/blabla5"

- Andi -

And it doesn't come from your code, but

Code:

char str[256]
is a bad isea in the first place - because the OP is talking about a string constant.

Something like

Code:

const char * const str = ...;
better serves the purpose.

ForzaItalia2006 01-16-2010 06:39 AM

Hey,

yes, you're completely right. I was just copying the initial code, but your approach is much better :-)

- Andi -

smeezekitty 01-16-2010 12:43 PM

What about the case that if the code modifies it later?

Sergei Steshenko 01-16-2010 01:40 PM

Quote:

Originally Posted by smeezekitty (Post 3828861)
What about the case that if the code modifies it later?

What about reading what the OP wrote - string constant ?

rafton 01-18-2010 07:41 AM

Thank you very muche ForzaItalia2006!

That was exactly what I was looking for, something simple and quite close to wath I usually do with integers.

Quote:

Originally Posted by ForzaItalia2006 (Post 3828026)
Hey,



Concatenation of string literals (constants) in C is realized by just writing:

Code:

char *str = "String1" "String2"; // "String1String2"
The compiler will then concatenate the strings for you in the output file. If you want to concatenate strings whereof one is NOT a constant, just use the strcat/strncat C library function.



You could do this:

Code:

#define USB "blabla"
static char str[256] {"/" USB "5"};

I hope that is what you're searching for ...

- Andi -

Thanks again.

Raf


All times are GMT -5. The time now is 10:21 AM.