473,807 Members | 2,823 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String concatenation

Pan
#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.
Sep 4 '07 #1
8 2326
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
Sep 4 '07 #2
Pan

"Richard Heathfield" <rj*@see.sig.in validwrote in message
news:sq******** *************** *******@bt.com. ..
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");
Sep 4 '07 #3
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
Sep 4 '07 #4
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

Sep 4 '07 #5
In article <46************ ***@yahoo.com>,
CBFalconer <cb********@mai neline.netwrote :
>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
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Sep 4 '07 #6
"Richard Heathfield" <rj*@see.sig.in valida écrit dans le message de news:
sq************* *************** **@bt.com...
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.
Sep 20 '07 #7
Charlie Gordon said:
"Richard Heathfield" <rj*@see.sig.in valida écrit dans le message de
news: sq************* *************** **@bt.com...
<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
Sep 20 '07 #8
"Richard Heathfield" <rj*@see.sig.in valida écrit dans le message de news:
eJ************* *************** **@bt.com...
Charlie Gordon said:
>"Richard Heathfield" <rj*@see.sig.in valida écrit dans le message de
news: sq************* *************** **@bt.com...

<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 "preprocess ing" 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 "preprocess ing output" to be produced. I
think it implies that "preprocess ing" 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.
Sep 20 '07 #9

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

Similar topics

5
3649
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
20
11332
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular expressions and I sure there is a lot ways, but I need realy efficient one
3
11163
by: John Ford | last post by:
For simple string concatenation, is there a difference between... Dim s As String s += "add this to string" ....and... Dim s As String s = String.Concat(s, "add this to string")
9
2054
by: Justin M. Keyes | last post by:
Hi, Please read carefully before assuming that this is the same old question about string concatenation in C#! It is well-known that the following concatenation produces multiple immutable String objects for each statement: String a = "a"; a += "b";
16
2157
by: Mark A. Sam | last post by:
Hello, I am having a problem with imputting into a string variable: Dim strSQL As String = "INSERT INTO tblContactForm1 (txtName, txtCompany, txtPhone, txtEmail, txtComment, chkGrower, chkProduceDealer, txtOtherCustType, chkStandardBags, chkCustomBags,txtOtherBags) " + _ "VALUES ('" + txtName.Text + "','" + txtCompany.Text + "','" + txtPhone.Text + "','" + txtEmail.Text + "','" + txtComment.Text + "','" +
33
4698
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most of articles I read from different experts and programmers tell me that their "gut feelings" for using stringBuilder instead of string concatenation is when the number of string concatunation is more then N ( N varies between 3 to max 15 from...
12
2719
by: Richard Lewis Haggard | last post by:
I thought that the whole point of StringBuilder was that it was supposed to be a faster way of building strings than string. However, I just put together a simple little application to do a comparative analysis between the two and, surprisingly, string seems to out perform StringBuilder by a significant amount. A string concatenation takes not quite twice as long using StringBuilder than it does with a string. This doesn't sound right to...
34
2671
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ____________ THE OVERVIEW I don't remember where I picked it up, but I remember reading years ago that the simple, obvious Python approach for string concatenation: x = "a" + "b"
10
13647
by: =?Utf-8?B?RWxlbmE=?= | last post by:
I am surprised to discover that c# automatically converts an integer to a string when concatenating with the "+" operator. I thought c# was supposed to be very strict about types. Doesn't it seem like c# is breaking its own rules? This works: int b = 32; string a = "ABC " + b; Result: a = "ABC 32"
34
3563
by: raylopez99 | last post by:
StringBuilder better and faster than string for adding many strings. Look at the below. It's amazing how much faster StringBuilder is than string. The last loop below is telling: for adding 200000 strings of 8 char each, string took over 25 minutes while StringBuilder took 40 milliseconds! Can anybody explain such a radical difference?
0
9719
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
10624
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10371
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...
0
9193
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
7650
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
6877
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5684
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
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
3853
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.