user93
user93

Reputation: 1866

scanf not working as expected

I tried to execute the following simple code in ubuntu 15.10 But the code behaves odd than expected

#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

The second scanf to get the value to be searched seems not to be working. The rest of the code executes after the first scanf without getting input second time.

Upvotes: 3

Views: 1597

Answers (4)

msc
msc

Reputation: 34568

You can use " %c" instead of "%c" for the format string. The blank causes scanf() to skip white space (including newlines) before reading the character.

Upvotes: 0

Ferit
Ferit

Reputation: 9647

After first scanf(), in every scanf(), in formatting part, put a whitespace

So change this

scanf("%c",&val);

into this

scanf(" %c",&val);

Reason is, scanf() returns when it sees a newline, and when first scanf() runs, you type input and hit enter. scanf() consumes your input but not remaining newline, so, following scanf() consumes this remaining newline.

Putting a whitespace in formatting part makes that remaining newline consumed.

Upvotes: 1

KunMing Xie
KunMing Xie

Reputation: 1667

printf("\nEnter the value : ");
scanf("%s",a);
printf("\nEnter the value to be searched : ");
scanf("%d",&val);   // here is different

i don't know why, but code above working...

scanf("%d",&val);

Upvotes: 0

orestisf
orestisf

Reputation: 1453

You can use 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;
}

or clear 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;
}

Also, see C: Multiple scanf's, when I enter in a value for one scanf it skips the second scanf

Upvotes: 0

Related Questions