关于scanf()

scanf函数原型:

int scanf(const char *format…..);

从标准输入流stdin中按格式format将数据写到参数表中;若操作成功,返回写到参数表中的参数个数,否则返回EOF

%d 会自动过滤掉空格和回车,

%c会读入所有的字符,包括空格和回车

#include <stdio.h>
int main() {
   char command; int x1, x2;
   while ( int c = scanf( "%c%d%d\n", &command, &x1, &x2 ) ) { //read no more than 3 items in current line
      switch ( c ) { //#items scanf() reads
         case EOF : return 0; //end of input stream
         case 0 : printf("impossible\n"); break; //blank lines are ignored by scanf()
         case 1 : printf("single: %c\n", command); break;
         case 2 : printf("double: %c %d \n", command, x1); break;
         case 3 : printf("triple: %c %d %d\n", command, x1, x2); break;
         default : printf("impossible\n"); break;
      }
   }

以上代码表示读入的文本中,最多读入3个输入;若不足3个输入,继续输入的空格和回车都将视为等待下一个数字的读入,若下一个是除空格和回车以外的字符,则认为一次输入结束,开始下一次输入。

Guess you like

Origin blog.csdn.net/juttajry/article/details/49925811