LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-15-2010, 10:43 AM   #1
rafton
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Rep: Reputation: 0
#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
 
Old 01-15-2010, 11:08 AM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by rafton View Post
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 .
 
Old 01-15-2010, 04:38 PM   #3
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Hey,

Quote:
Originally Posted by rafton View Post
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 View Post
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 -
 
Old 01-15-2010, 05:53 PM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
Old 01-16-2010, 04:24 AM   #5
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
http://gcc.gnu.org/onlinedocs/cpp/St...tringification
 
Old 01-16-2010, 05:02 AM   #6
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Quote:
Originally Posted by Sergei Steshenko View Post
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 -
 
Old 01-16-2010, 05:10 AM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by ForzaItalia2006 View Post
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.
 
Old 01-16-2010, 06:39 AM   #8
ForzaItalia2006
Member
 
Registered: Dec 2009
Location: Walldorf, Germany
Distribution: (X)Ubuntu, Arch, Gentoo
Posts: 205

Rep: Reputation: 67
Hey,

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

- Andi -
 
Old 01-16-2010, 12:43 PM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
What about the case that if the code modifies it later?
 
Old 01-16-2010, 01:40 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by smeezekitty View Post
What about the case that if the code modifies it later?
What about reading what the OP wrote - string constant ?
 
Old 01-18-2010, 07:41 AM   #11
rafton
LQ Newbie
 
Registered: Jan 2010
Posts: 2

Original Poster
Rep: Reputation: 0
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 View Post
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a #define Wynd Programming 4 04-22-2007 03:30 PM
m4 define in a define Four Programming 0 03-04-2007 09:41 PM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
define jhon Linux - Networking 2 08-30-2004 07:42 PM
using #define Jo_Nak Programming 4 06-11-2003 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration