关于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个输入,继续输入的空格和回车都将视为等待下一个数字的读入,若下一个是除空格和回车以外的字符,则认为一次输入结束,开始下一次输入。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值