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.


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.