#include <stdio.h>

int main(int argc, const char * argv[]) {

    int a;

    char b;

    float c;

    printf("please input an integer:\n");

    scanf("%d",&a);

    printf("Integer:%d\n",a);

    printf("please input a character:\n");

    getchar();//直接将缓冲区的字符读取避免被后面的变量当作有效字符读入,也可以在scanf("  %c",&b),%c前面加一个空格也可以避免读取缓冲区的字符。

    scanf("%c",&b);

    printf("Character:%c\n",b);

    printf("please input a float number:\n");

    scanf("%f",&c);

    printf("float:%f\n",c);    

}

运行结果:

C格式符以及解决问题

这是因为在输入前一个数据12的时候,输入到缓冲区的是12和回车,12被a读取,而回车对于字符型数据来说是一个有效字符,直接从缓冲区读取了回车,不需要键盘再次输入。但为什么最后面的没有读取回车呢,因为回车只对字符型数据有效。需要加getchar()将缓冲区的回车读取即可解决这个问题。

C格式符以及解决问题

相关文章:

  • 2021-09-25
  • 2021-07-07
  • 2022-12-23
  • 2021-07-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-04-03
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2022-01-07
相关资源
相似解决方案