问题描述
考虑以下code:
#include<stdio.h>
int main() {
int i=3, j=4;
scanf("%d c %d",&i,&j);
printf("%d %d",i,j);
return 0;
}
它的工作原理,如果我给 2C3
或 2 C 3
或 2C 3
作为输入,如果我必须要改变的变量的值。我应该怎么做,如果我希望用户,我想是指输入相同的模式,如果%直流%d个
则仅 2C3
是可以接受的,而不是 2 C 3
反之亦然,如果它是%直流%d个
?
It works if I give 2c3
or 2 c 3
or 2c 3
as input if I have to change the value of variables. What should I do if I want the user to enter the same pattern as I want means if %dc%d
then only 2c3
is acceptable and not 2 c 3
and vice versa if it is %d c %d
?
推荐答案
空格格式字符串匹配的输入0个或多个空格字符。
Whitespace in the format string matches 0 or more whitespace characters in the input.
所以%DC%D
预计数,则空白字符的数量,那么字符 C
,然后空白字符的数量,并在年底另一个号码。
So "%d c %d"
expects number, then any amount of whitespace characters, then character c
, then any amount of whitespace characters and another number at the end.
%的直流%D
预计数, C
,数量。
另外请注意,如果你使用 *
格式字符串,它燮presses分配:结果%* C
=读1个字符,但不要把它分配给任何一个变量
Also note, that if you use *
in the format string, it suppresses assignment:%*c
= read 1 character, but don't assign it to any variable
所以,你可以使用%D%* CC%* C%D
如果你想为力量用户输入:数字,至少1个字符后面的空格字符任何金额, C
,至少有1个字符,然后再按的空白字符的任意金额和数量。
So you can use "%d%*c c%*c %d"
if you want to force user to enter: number, at least 1 character followed by any amount of whitespace characters, c
, at least 1 character followed by any amount of whitespace characters again and number.
这篇关于空白格式字符串(scanf函数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!