string.h頭文件以及stdio.h頭文件下的sscanf和sprintf補充

string.h頭文件下的常用函數
strlen(字符數組的首地址):返回值是一個字符數組的大小(不包括\0)
strlen(字符數組1,字符數組2):返回值分爲三種情況
(1)1小於2,返回一個負整數
(2)1等於2,返回零
(3)1大於2,返回一個正整數
strcpy(字符數組1,字符數組2):把2複製給1,包括\0
strcat(字符數組1,字符數組2):把2接在1的後面

sscanf:把字符數組str中的內容以%d的格式寫到n中(從左到右)

#include <stdio.h>

int main()
{
	int n;
	double db;
	char str2[100];
	char str[100]="2018:3.14,hello";
	sscanf(str,"%d:%lf,%s",&n,&db,str2);
	printf("n=%d,db=%.2f,str2=%s\n",n,db,str2);
} 
//n=2018,db=3.14,str2=hello

sprintf:把n以%d的格式寫到str字符數組中(從右到左)

#include <stdio.h>

int main()
{
	int n=12;
	double db=3.1415;
	char str[100];
	char str2[100]="good";
	sprintf(str,"%d:%.2f,%s",n,db,str2);
	printf("str=%s\n",str);
}
//str=12:3.14,good
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章