在C中连接字符串

aus*_*tin 3 c string concatenation

我想知道是否有一种方法可以为字符串添加值,而不是像1 + 1 = 2但像1 + 1 = 11.

Gan*_*ant 9

我认为你需要字符串连接:

#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;
}
Run Code Online (Sandbox Code Playgroud)

来自:http://irc.essex.ac.uk/www.iota-six.co.uk/c/g6_strcat_strncat.asp

  • @Jonathan:不知道"非常难",但它更安全. (2认同)

Eri*_*ler 6

要连接两个以上的字符串,可以使用sprintf,例如

char buffer[101];
sprintf(buffer, "%s%s%s%s", "this", " is", " my", " story");
Run Code Online (Sandbox Code Playgroud)