
- C 標準庫
- C 庫 - 首頁
- C 庫 - <assert.h>
- C 庫 - <complex.h>
- C 庫 - <ctype.h>
- C 庫 - <errno.h>
- C 庫 - <fenv.h>
- C 庫 - <float.h>
- C 庫 - <inttypes.h>
- C 庫 - <iso646.h>
- C 庫 - <limits.h>
- C 庫 - <locale.h>
- C 庫 - <math.h>
- C 庫 - <setjmp.h>
- C 庫 - <signal.h>
- C 庫 - <stdalign.h>
- C 庫 - <stdarg.h>
- C 庫 - <stdbool.h>
- C 庫 - <stddef.h>
- C 庫 - <stdio.h>
- C 庫 - <stdlib.h>
- C 庫 - <string.h>
- C 庫 - <tgmath.h>
- C 庫 - <time.h>
- C 庫 - <wctype.h>
- C 標準庫資源
- C 庫 - 快速指南
- C 庫 - 有用資源
- C 庫 - 討論
- C 程式設計資源
- C 程式設計 - 教程
- C - 有用資源
C 庫 - strcat() 函式
C 庫 strcat() 函式接受兩個指標變數作為引數(例如 dest、src),並將 src 指向的字串追加到 dest 指向的字串末尾。此函式僅連線字串資料型別。我們不能使用任何其他資料型別,例如 int、float、char 等。
語法
以下是 C 庫 strcat() 函式的語法 -
char *strcat(char *dest_str, const char *src_str)
引數
此函式接受以下引數 -
dest_str - 這是指向目標陣列的指標,該陣列應包含 C 字串,並且應足夠大以容納連線後的結果字串。
src_str - 要追加的字串。這應該不與目標重疊。
返回值
此函式返回指向結果字串(即 dest)的指標。
請注意,該函式在執行時動態地組合字串。
示例 1
以下是說明使用 strcat() 函式進行字串連線的程式碼片段的基本 C 庫程式。
#include <stdio.h> #include <string.h> int main() { char dest_str[50] = "C "; const char src_str[] = "JAVA"; // Concatenate src_str to dest_str strcat(dest_str, src_str); // The result store in destination string printf("%s", dest_str); return 0; }
輸出
執行上述程式碼後,我們將獲得以下輸出 -
Final destination string : |This is destinationThis is source|
示例 2
在以下示例中,我們使用 strcpy() 以及 strcat() 函式,將新字串 (“point”) 與現有字串 (“tutorials”) 合併。
#include <stdio.h> #include <string.h> int main() { char str[100]; // Copy the first string to the variable strcpy(str, "Tutorials"); // String concatenation strcat(str, "point"); // Show the result printf("%s\n", str); return 0; }
輸出
上述程式碼產生以下輸出 -
Tutorialspoint
示例 3
雖然連線兩個不同的字串始終需要兩個引數,但這裡我們使用整數作為 strcat() 的引數,這會導致錯誤。
#include <stdio.h> #include <string.h> int main() { int x = 20; int y = 24; strcat(x, y); printf("%d", x); return 0; }
輸出
執行上述程式碼後,我們將獲得以下結果 -
main.c: In function ‘main’: main.c:7:12: warning: passing argument 1 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion] 7 | strcat(x, y); | ^ | | | int In file included from main.c:2: /usr/include/string.h:149:39: note: expected ‘char * restrict’ but argument is of type ‘int’ 149 | extern char *strcat (char *__restrict __dest, const char *__restrict __src) | ~~~~~~~~~~~~~~~~~^~~~~~ main.c:7:15: warning: passing argument 2 of ‘strcat’ makes pointer from integer without a cast [-Wint-conversion] 7 | strcat(x, y); | ^ | | | int In file included from main.c:2: /usr/include/string.h:149:70: note: expected ‘const char * restrict’ but argument is of type ‘int’ 149 | extern char *strcat (char *__restrict __dest, const char *__restrict __src) |
廣告