How to concatenate strings in C, not like 1 + 1 = 2 but like 1 + 1 = 11.
-
I'm afraid your question doesn't make much sense currently. Can you be a bit more specific?Mitch Wheat– Mitch Wheat2009-06-09 05:37:58 +00:00Commented Jun 9, 2009 at 5:37
-
Thanks for editing that Vinko, I figured I would just be flamed for my question, but I actually got what I wanted. Thanks stackoverflow.comaustin– austin2009-06-09 06:15:23 +00:00Commented Jun 9, 2009 at 6:15
-
austin, would you mind marking your accepted answer that helps you the most?Seh Hui Leong– Seh Hui Leong2009-06-09 06:26:12 +00:00Commented Jun 9, 2009 at 6:26
-
sure, sorry about that. this is my first time using stackoverflow.austin– austin2009-06-09 07:43:18 +00:00Commented Jun 9, 2009 at 7:43
Add a comment
|
5 Answers
I think you need string concatenation:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello ";
char str2[] = "World";
strcat(str1, str2);
printf("str1: %s\n", str1);
return 0;
}
from: http://irc.essex.ac.uk/www.iota-six.co.uk/c/g6_strcat_strncat.asp
6 Comments
Jonathan Leffler
Note that strncat() (with the 'n') is extremely hard to use correctly - so don't use it.
Vinko Vrsalovic
Why is it harder than counting your buffers in strcat?
Tal Pressman
@Jonathan: Don't know about "extremely hard", but it's much safer.
Thomas
not much safer. There is a reason strlcat() exists. Although that's not a complete fix either. It's just a drop-in improvement. I'd use snprintf(). It has the best behavior.
RBerteig
strncat() doesn't guarantee the result is nul terminated. strlcat() does, but may not be widely available. strcat() is blissfully ignorant of the size of the destination buffer. In short, the C standard library is burdened by a lot of history that gets in the way of preventing buffer overruns.
|
To concatenate more than two strings, you can use sprintf, e.g.
char buffer[101];
sprintf(buffer, "%s%s%s%s", "this", " is", " my", " story");
2 Comments
Erich Kitzmueller
ynimous: snprintf is not available everywhere
Johannes Schaub - litb
many people can only rely on c89. sweet snprintf isn't available for them :(
Try taking a look at the strcat API. With sufficient buffer space, you can add one string onto the end of another one.
char[50] buffer;
strcpy(buffer, "1");
printf("%s\n", buffer); // prints 1
strcat(buffer, "1");
printf("%s\n", buffer); // prints 11
Reference page for strcat
Comments
'strcat' is the answer, but thought there should be an example that touches on the buffer-size issue explicitly.
#include <string.h>
#include <stdlib.h>
/* str1 and str2 are the strings that you want to concatenate... */
/* result buffer needs to be one larger than the combined length */
/* of the two strings */
char *result = malloc((strlen(str1) + strlen(str2) + 1));
strcpy(result, str1);
strcat(result, str2);
3 Comments
Vinko Vrsalovic
sizeof(char) is 1, always. drj11.wordpress.com/2007/04/08/sizeofchar-is-1
RBerteig
This leaks memory by overwriting the pointer
result, and needs at least one strcpy() as well. Also, it extends str1, but the implication is that you intended both str1 and str2 to be read only.aib
malloc() may return NULL, causing UB.