文字列同士を連結する関数・strcatについて記します。
c-top.html is not found or not readable.以下にstrcat関数を使用した例を記します。
strcatを使用するには、string.hをインクルードする必要があります。 また、書式は以下のようになります。
#include <string.h> char *strcat(char *dest, const char *src);
destに連結した文字列が入ります。
したがって、dest側は連結した文字列数以上のエリアを確保しておかなければ行けません。
空のchar配列に文字を連結していくCサンプルコードになります。 &ref(): File not found: "strcat.c" at page "文字列/文字列を連結する・strcat"; (改行コードLF)
#include <stdio.h> #include <string.h> int main(void) { char all[128]; memset(all, '\0', sizeof(all)); strcat(all, "Hello"); strcat(all, " "); strcat(all, "World"); strcat(all, "!"); printf("all = %s\n", all); return 0; }
上記サンプルの例は、charのエリアを\0で初期化しています。
その後、strcat関数でHello World!文字列を連結し、printfで表示しています。
以下にコンパイルと実行した時の出力結果を記します。
$ gcc strcat.c -o strcat $ ./strcat all = Hello World!
以上、strcat関数のCサンプルコードでした。
c-btm.html is not found or not readable.