scanf中用','作为字符串分隔符

代码:

	char msg1[1024];
	char msg2[1024];
	char msg3[1024];
	
	scanf("%[^,],%[^,],%s", msg1, msg2, msg3);
	printf("%s,%s,%s\n", msg1, msg2, msg3);

你能解释一下上面代码的意思吗?


对输入:hello world,huntinux,stay foolish stay hungry

输出为:hello world,huntinux,stay

原因:

[^...]的解释为: matches the longest non-empty string of input characters not from the setbetween brackets(摘自《The C Programming Language》第二版P204)。[^,]代表不包含','的最长匹配串

%s 的解释为:string of non-white space characters即不包含空格,tab的最长匹配



版权声明:本文为chj90220原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。