[英]Take input string in c
To take input string in c, we can opt for 2 ways( as per my knowledge please add more if i missing something )要在 c 中获取输入字符串,我们可以选择 2 种方式(据我所知,如果我遗漏了什么,请添加更多)
char name[10];
scanf(" %s",name); // First case
scanf("%[\n]s",name); // Second case
1st one will consider the string till the first blank space and 2nd will take complete sentence till the new line break as a string but to take input we need to improvise the 2nd statement as第一个将考虑字符串直到第一个空格,第二个将整个句子直到换行符作为字符串但要输入我们需要即兴创作第二个语句
scanf(" %[\n]s",name);
my question is what is extra spacing doing here ?我的问题是这里的额外间距是什么? because sometime my compiler will behave normally even if i will remove the 's' from scanf statement and without extra spacing before '%' but sometime it wont work.因为有时我的编译器会正常运行,即使我将从 scanf 语句中删除 's' 并且在 '%' 之前没有额外的空格,但有时它不会工作。
The space before the %
operator will make it so that any white-space before the entered string is removed as well as whatever effect the format specifiers after the %
operator have on the input. %
运算符之前的空格将删除输入字符串之前的任何空格,以及%
运算符之后的格式说明符对输入的任何影响。
Ex: "\\n\\t Hello World" entered with " %[^\\n]"
will be recorded as "Hello World".例如:用" %[^\\n]"
输入的“\\n\\t Hello World " %[^\\n]"
将被记录为“Hello World”。
如果空白字符留在第一个输入流中并被下一个输入scanf
错误读取:要从输入流中删除空白字符,请在格式字符串前使用空格或使用fflush
(stdin) 函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.