要把數字轉換為字串 是有一個itoa function 但並非在標準函示庫裡面 所以另一個方法 sprintf() 在 cplusplus () 裡面的 sample 是
1 /* sprintf example */ #include int main () { char buffer [50]; int n, a=5, b=3; n=sprintf (buffer, "%d plus %d is %d", a, b, a+b); printf ("[%s] is a %d char long string\n",buffer,n); return 0; }
如果是要用二維字串陣列的話 #define LEN1 5 #define LEN2 5 char array[LEN1][LEN2]; sprintf(&array[i][0], "%d+%d", a, b); 不過總覺得上面 &array[i][0] 這個寫法繞了一圈 我的觀念似乎還不太正確

coolkevin 發表在 痞客邦 留言(0) 人氣(0)