Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating strings in C

How to concatenate strings in C, not like 1 + 1 = 2 but like 1 + 1 = 11.

like image 654
austin Avatar asked Dec 04 '22 15:12

austin


2 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

like image 64
Gant Avatar answered Dec 06 '22 04:12

Gant


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");
like image 42
Erich Kitzmueller Avatar answered Dec 06 '22 04:12

Erich Kitzmueller