Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > String concatenation

Reply
Thread Tools

String concatenation

 
 
Pan
Guest
Posts: n/a
 
      09-04-2007
#include <stdio.h>
#define MYSTR "World"

void foo(char *p)
{
puts(p);
}

int main()
{
foo("Hello" MYSTR);
}

Is this guaranteed to print HelloWorld - it does so on my system,
but just wanted to check.
Basically I wanted to know how to pass a concat of 2 strings, one
of which is in a macro into a function.


 
Reply With Quote
 
 
 
 
Pan
Guest
Posts: n/a
 
      09-04-2007

"Richard Heathfield" <> wrote in message
news:...
> Pan said:
>
>> #include <stdio.h>
>> #define MYSTR "World"
>>
>> void foo(char *p)
>> {
>> puts(p);
>> }
>>
>> int main()
>> {
>> foo("Hello" MYSTR);
>> }
>>
>> Is this guaranteed to print HelloWorld - it does so on my system,
>> but just wanted to check.

>
> Yes (and a newline).
>
>> Basically I wanted to know how to pass a concat of 2 strings, one
>> of which is in a macro into a function.

>
> Two string literals, yes. Whether one or the other or neither or both is
> a macro is neither here nor there, but they must both be string
> literals if you want the preprocessor to glue them together for you.


Thank you, Richard.
I assume this would work for more than 2 also
i.e.

foo("Hello" MYSTR "Goodbye");


 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      09-04-2007
Pan said:

> #include <stdio.h>
> #define MYSTR "World"
>
> void foo(char *p)
> {
> puts(p);
> }
>
> int main()
> {
> foo("Hello" MYSTR);
> }
>
> Is this guaranteed to print HelloWorld - it does so on my system,
> but just wanted to check.


Yes (and a newline).

> Basically I wanted to know how to pass a concat of 2 strings, one
> of which is in a macro into a function.


Two string literals, yes. Whether one or the other or neither or both is
a macro is neither here nor there, but they must both be string
literals if you want the preprocessor to glue them together for you.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-04-2007
Pan said:

<snip>

> I assume this would work for more than 2 also
> i.e.
>
> foo("Hello" MYSTR "Goodbye");


puts("Yes, that's"
" right, you"
" can glue t"
"ogether as "
"many as you"
" like (with"
"in reason).");

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-04-2007
Richard Heathfield wrote:
> Pan said:
>
> <snip>
>
>> I assume this would work for more than 2 also i.e.
>>
>> foo("Hello" MYSTR "Goodbye");

>
> puts("Yes, that's"
> " right, you"
> " can glue t"
> "ogether as "
> "many as you"
> " like (with"
> "in reason).");


Where "in reason" translates to about 500 chars. It is specified
in the standard somewhere.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

 
Reply With Quote
 
Richard Tobin
Guest
Posts: n/a
 
      09-04-2007
In article <>,
CBFalconer <> wrote:

>Where "in reason" translates to about 500 chars. It is specified
>in the standard somewhere.


Just before the limit of 32767 bytes in an object.

-- Richard
--
"Consideration shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-20-2007
"Richard Heathfield" <> a écrit dans le message de news:
...
> Pan said:
>
>> #include <stdio.h>
>> #define MYSTR "World"
>>
>> void foo(char *p)
>> {
>> puts(p);
>> }
>>
>> int main()
>> {
>> foo("Hello" MYSTR);
>> }
>>
>> Is this guaranteed to print HelloWorld - it does so on my system,
>> but just wanted to check.

>
> Yes (and a newline).
>
>> Basically I wanted to know how to pass a concat of 2 strings, one
>> of which is in a macro into a function.

>
> Two string literals, yes. Whether one or the other or neither or both is
> a macro is neither here nor there, but they must both be string
> literals if you want the preprocessor to glue them together for you.


This is a late reply, but contrary to popular belief, it is not the
preprocessor that glues adjacent string literals together, but the compiler
in translation phase 6 (6.4.5p4).

--
Chqrlie.


 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      09-20-2007
Charlie Gordon said:

> "Richard Heathfield" <> a écrit dans le message de
> news: ...


<snip>
>>
>> Two string literals, yes. Whether one or the other or neither or both is
>> a macro is neither here nor there, but they must both be string
>> literals if you want the preprocessor to glue them together for you.

>
> This is a late reply, but contrary to popular belief, it is not the
> preprocessor that glues adjacent string literals together, but the
> compiler in translation phase 6 (6.4.5p4).


It's the implementation, in fact: and phase 6 comes *before* translation
(which happens in TP7), so it's still *pre*-processing. I have read
6.4.5(4) and failed to find any mention of a compiler.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-20-2007
"Richard Heathfield" <> a écrit dans le message de news:
...
> Charlie Gordon said:
>
>> "Richard Heathfield" <> a écrit dans le message de
>> news: ...

>
> <snip>
>>>
>>> Two string literals, yes. Whether one or the other or neither or both is
>>> a macro is neither here nor there, but they must both be string
>>> literals if you want the preprocessor to glue them together for you.

>>
>> This is a late reply, but contrary to popular belief, it is not the
>> preprocessor that glues adjacent string literals together, but the
>> compiler in translation phase 6 (6.4.5p4).

>
> It's the implementation, in fact: and phase 6 comes *before* translation
> (which happens in TP7), so it's still *pre*-processing. I have read
> 6.4.5(4) and failed to find any mention of a compiler.


All C preprocessors I have tested leave the adjacent strings intact. It
does not prove my point beyond common sense understanding of the split
between "preprocessing" and "compiling".

Translation phases are described in c99 5.1.1.2: it is clear that
translation phase 4 is included in the "preprocessing". Translation phase 5
would have to be reverted for "preprocessing output" to be produced. I
think it implies that "preprocessing" covers phases 1 through 4, but
excludes phases 5 and 6, but it would not break much to include them and go
through extra work to produce parsable output where adjacent string literals
have been concatenated. It is just not what compiler writers seem to
choose.

--
Chqrlie.


 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String concatenation vs. string formatting Andrew Berg Python 13 07-10-2011 11:24 PM
String Concatenation & Removing Space Sparky Arbuckle ASP .Net 5 09-01-2005 10:47 PM
String Concatenation problems Daniel Bergquist Perl 2 07-16-2004 01:43 AM
Re: Use of uninitialized value in concatenation (.) or string Error Sukhbir Dhillon Perl 1 04-05-2004 02:31 AM
what's the difference between VHDL 93 CONCATENATION and VHDL 87 CONCATENATION? walala VHDL 3 09-18-2003 04:17 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57