简体   繁体   English

[^:]的含义:scanf中的格式字符串

[英]Meaning of [^:]: format string in scanf

I found this in some code and could not understand what it does: 我在某些代码中发现了这一点,但无法理解它的作用:

scanf("%[^:]:%[^:]:%[^:\n]", a, b, c);

There was no mention of the [^:]: format string in the C documentation and I am quite confused. C文档中没有提到[^:]:格式字符串,我很困惑。

The format string %[..] is for specifying the possible characters. 格式字符串%[..]用于指定可能的字符。 For example %[A-DF] is for A, B, C, D, and F. And the ^ at the beginning is for any character excluding the characters specified. 例如,%[A-DF]表示A,B,C,D和F。开头的^表示除指定字符之外的任何字符。 Hence, the first format string is for reading characters excluding colon. 因此,第一个格式字符串用于读取冒号以外的字符。 And the next is colon, and so on. 接下来是冒号,依此类推。 You may test the scanf for the following input: 您可以为以下输入测试scanf:

Adam:And:Apple

Important thing you should remember is, scanf() function accepts the arguments how it is specified. 您应该记住的重要事情是,scanf()函数接受参数的指定方式。

For example: 例如:

scanf("%d %d",&a,&b); scanf(“%d%d”,&a,&b); assuming a and b are integer. 假设a和b是整数。

In the command line you have to input a and then followed by space and then b.because you have given a space in-between two "%d's". 在命令行中,您必须输入a,然​​后输入空格,然后输入b。因为您在两个“%d's”之间给出了空格。 if you are giving n number of spaces are other character then you have to enter in-between those character before the value gets accepted. 如果给n个空格是其他字符,则必须在这些字符之间输入空格,然后再接受该值。

Hence in your case: 因此,在您的情况下:

you enter the string not containing ":" in it. 您输入不包含“:”的字符串。

Consider this case, if you want to enter a string containing ":" in it. 如果要输入包含“:”的字符串,请考虑这种情况。

For example: 例如:

"some:init" is the string then, in first %[^:] the string "some" is stored,then comes the ":" and then in second %[^:] it stores init. “ some:init”是字符串,然后在字符串的首个%[^:]中存储字符串“ some”,然后是“:”,然后在第二个%[^:]中存储init。 Then it wait for one more ":" and then the string not containing ":". 然后,它再等待一个“:”,然后是不包含“:”的字符串。

ultimately accepting the input in this format doesn't fetch any thing. 最终接受这种格式的输入不会获取任何东西。

The only thing matters is how you print it on to the console. 唯一重要的是如何将其打印到控制台上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM