😸

[C] strcat 関数

に公開

機能

文字列を結合する関数[1]

使用例

"Hello, World!" と表示する

\n は改行を表す エスケープシーケンス[2]

コード

char str[20] = "Hello, ";
strcat(str, "World!");
printf("%s\n", str);
全文
#include <stdio.h>
#include <string.h>

void main() {
    char str[20] = "Hello, ";
    strcat(str, "World!");
    printf("%s\n", str);
}

実行結果

Hello, World!

変数から "Goodbye, World!" を表示する

コード

char str1[20] = "Goodbye, ";
char str2[] = "World!";
strcat(str1, str2);
printf("%s\n", str1);
全文
#include <stdio.h>
#include <string.h>

void main() {
    char str1[20] = "Goodbye, ";
    char str2[] = "World!";
    strcat(str1, str2);
    printf("%s\n", str1);
}

実行結果

Goodbye, World!
脚注
  1. strcat は複数の文字列をつなげて、ひとつの文字列として扱いたいときに使う。 ↩︎

  2. 改行やタブなど、画面に表示されない制御文字のこと。 ↩︎

Discussion