2016-02-29 71 views
-1

我試着用strcat()使字符串數組,但它一直給我想要的字符,例如:char arr[50]strcat(arr, "test")當我puts(arr)printf("%s\n", arr),它總是給我@!*test代替只是test,有誰知道是什麼原因導致我的問題?隨機不必要的符號用C

謝謝!

+0

OP是不是下面的建議[先前的評論](http://stackoverflow.com/questions/35689162/check-if-something-exists-and-is-executable-在-C-使用-STAT-函數#comment59059092_35689199)。 – chux

回答

1

您必須在使用其值之前初始化數組,否則您會調用未定義的行爲

試試這個:

#include <stdio.h> 
#include <string.h> 

int main(void) { 
    char arr[50] = {0}; /* add = {0} */ 
    strcat(arr, "test"); 
    puts(arr); 
    return 0; 
} 
+0

對於字符數組,您當然可以使用'char arr [50] =「」;'。 –