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的最长匹配


你可能感兴趣的:(scanf中用','作为字符串分隔符)