【问题标题】:scanf not working as expectedscanf 没有按预期工作
【发布时间】:2015-12-25 03:45:57
【问题描述】:

我尝试在 ubuntu 15.10 中执行以下简单代码,但代码的行为比预期的要奇怪

#include<stdio.h>
int main(){
int n,i=0;
char val;
char a[20];

printf("\nEnter the value : ");
scanf("%s",a);
printf("\nEnter the value to be searched : ");
scanf("%c",&val);

int count=0;
for(i=0;i<20;i++){
 if(a[i]==val){
   printf("\n%c found at location %d",val,i);
   count++;
 }
}
printf("\nTotal occurance of %c is %d",val,count);
   return 0;
}

output:
--------------------------
Enter the value : 12345678
Enter the value to be searched : 
Total occurance of is 0

获取要搜索的值的第二个 scanf 似乎不起作用。其余代码在第一次 scanf 之后执行,第二次没有输入。

【问题讨论】:

  • 1) scanf("%c",&amp;val); --> scanf(" %c",&amp;val); 2) i&lt;20 --> i&lt;20 &amp;&amp; a[i] 3) scanf("%s",a); --> scanf("%19s", a); (i&lt;20 --> @987654329 @)
  • ia[i] 我的错字为a[i]
  • 为什么不检查scanf的返回值呢?也让它不超出数组
  • 如果你正为此痛苦地嚎叫,scanf() 正在按预期工作。
  • %c 接受 '\n', 12345678(Enter) -> 12345678'\n'

标签: c scanf


【解决方案1】:

在第一个scanf()之后,在每个scanf()中,在格式化部分,放一个空格

所以改变这个

scanf("%c",&val);

进入这个

scanf(" %c",&val);

原因是,scanf() 在看到换行符时返回,并且当第一次 scanf() 运行时,您输入输入并回车。 scanf() 会消耗您的输入,但不会消耗剩余的换行符,因此,跟随 scanf() 会消耗剩余的换行符。

在格式化部分放置一个空格会消耗剩余的换行符。

【讨论】:

    【解决方案2】:

    你可以使用fgets():

    #include<stdio.h>
    int main() {
        int n, i = 0;
        char val;
        char a[20];
    
        printf("\nEnter the value : ");
        fgets(a, 20, stdin);
        printf("\nEnter the value to be searched : ");
        scanf("%c", &val);
    
        int count = 0;
        for (i = 0; i < 20; i++) {
            if (a[i] == val) {
                printf("\n%c found at location %d", val, i);
                count++;
            }
        }
        printf("\nTotal occurance of %c is %d", val, count);
        return 0;
    }
    

    或清除stdin

    #include<stdio.h>
    
    void clearstdin(void) {
        int c;
        while ((c = fgetc(stdin)) != EOF && c != '\n');
    }
    
    int main() {
        int n, i = 0;
        char val;
        char a[20];
    
        printf("\nEnter the value : ");
        scanf("%s",a);
        clearstdin();
        printf("\nEnter the value to be searched : ");
        scanf("%c", &val);
    
        int count = 0;
        for (i = 0; i < 20; i++) {
            if (a[i] == val) {
                printf("\n%c found at location %d", val, i);
                count++;
            }
        }
        printf("\nTotal occurance of %c is %d", val, count);
        return 0;
    }
    

    另外,请参阅C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf

    【讨论】:

      【解决方案3】:
      printf("\nEnter the value : ");
      scanf("%s",a);
      printf("\nEnter the value to be searched : ");
      scanf("%d",&val);   // here is different
      

      我不知道为什么,但是上面的代码可以工作...

      scanf("%d",&val);
      

      【讨论】:

      • scanf 清除空格,除非使用字符格式 (%c)。在 OP 的示例中,键入 12345678 并按 Enter 意味着 a 得到 12345678 并且标准输入缓冲区中有一个换行符 ('\n')。 %d 删除它,但 %c 没有
      【解决方案4】:

      您可以使用“%c”代替“%c”作为格式字符串。空白会导致 scanf() 在读取字符之前跳过空格(包括换行符)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-05-21
        • 2021-10-19
        • 2020-03-18
        • 2012-06-14
        • 2014-11-15
        • 1970-01-01
        • 2012-07-02
        • 2011-09-07
        相关资源
        最近更新 更多