C中的scanf fscanf sscanf

//----------------------------------------------------

//AUTHOR: lanyang123456

//DATE: 2014-11-09

//---------------------------------------------------


       #include <stdio.h>

       int scanf(const char *format, ...);
       int fscanf(FILE *stream, const char *format, ...);
       int sscanf(const char *str, const char *format, ...);

scanf 从标准输入流stdin中读取输入;fscanf从stream输入流中读取输入;sscanf从字符串str中读取输入;

按照format给出的格式进行匹配和赋值。


返回值:返回成功匹配和赋值的个数;或者返回EOF,如果到达输入末端或发生读错误;


代码实例:


#include <stdio.h>


int main()
{
	int number;
	const char *str = "China has 56 ethnic groups.";

	//sscanf(str, "has %d", &number);
	//sscanf(str, "has %d ", &number);
	sscanf(str, "China has %d ", &number);
	printf("the number of ethnic groups = %d\n", number);
}

$ gcc -o str5 str5.c
$ ./str5
the number of ethnic groups = 134513851
$ gcc -o str5 str5.c
$ ./str5
the number of ethnic groups = 134513851
$ gcc -o str5 str5.c
$ ./str5
the number of ethnic groups = 56



参考man page

你可能感兴趣的:(C中的scanf fscanf sscanf)