C程式語言中字串到數字和數字到字串的轉換是什麼意思?
有兩種可用來轉換的函數。它們是-
我們可以使用sscanf() 函數將字串轉換為數字-
sscanf (string name, “control string”,variable list)
#include<stdio.h> main (){ char a[20] = “02 01 2010”; int day, mon, yr; clrscr(); sscanf (a, “%d%d %d”, &day, &mon, &yr); printf ( “Day =%d”, day); printf ( “Month = %d”, mon); printf ( “Year = %d”, yr); getch (); }
Day = 02 Month = 01 Year = 2010
#我們可以使用sprintf()函數將字串轉換為數字−
sprintf ( string name, “control string”, variable list)
#include<stdio.h> main (){ char a[50]; int day,mon,yr; day = 02; mon = 01; yr = 2010; crlscr(); sprintf (a, “%d/%d/%d”, day, mon, yr); printf ( “today’s date =%s”,a); getch (); }
Today’s date is 02/01/2010.
以上是使用C語言將字串轉換為數字和數字轉換為字串的詳細內容。更多資訊請關注PHP中文網其他相關文章!