13.sprintf and sscanf

sprinf

#include<stdio.h>
int main()
{
	int a = 10;
	char ch = 'd';
	char ch1[100] = "hello";
	printf("a = %d ch = %c ch1 = %s\n", a, ch, ch1);
	
	char ch2[100];
	//格式化一个字符串,把这个字符串输出到保存的数组
	sprintf(ch2, "a = %d, ch = %c ch1 = %s", a, ch, ch1);
	printf("%s", ch2);


}

sscanf

#include<stdio.h>
int main()
{
	int a, b, c;
	printf("请输入a,b,c");
	scanf("%d %d %d", &a, &b, &c);
	printf("%d %d %d", a, b, c);

	char ch[] = "1 2 3";
	a = 0;
	b = 0;
	c = 0;
	sscanf(ch, "%d %d %d", &a, &b, &c);
	printf("%d %d %d", a, b, c);
	//从ch中已制定的格式提取内容
	//提取整形变量最方便
	char str[] = "a = 1, b = 2, c = 3";
	sscanf(str, "a = %d, b = %d, c = %d", &a, &b, &c);
	printf("%d %d %d", a, b, c);


	char ch2[20] = "abc mike 250";//提取字符串默认通过空格分割
	char m[10], n[10], k[10];
	sscanf(ch2, "%s %s %s", m, n, k);//不用地址,字符数组名就是地址


	char ch2[20] = "abc, mike, 250";//提取字符串默认通过空格分割
	char m[10], n[10], k[10];
	sscanf(ch2, "%s, %s, %s", m, n, k);//不用地址,字符数组名就是地址,不可以用逗号作为分隔符,应该用空格
}
上一篇:php调用c语言获取ascp以及mas


下一篇:php通过mongdb的objectid来创建24位的唯一字符串