2

I am having problem scanning char variable , my code is

#include <stdio.h>
#include <conio.h>

void main() 
{
clrscr();
int a;
float b;
char c;

printf("Enter value for int variable \n");
scanf("%d",&a);

printf("Enter value for float variable \n");
scanf("%f",&b);

printf("Enter value for char variable \n");
scanf("%c",&c); //scanning is automatically skipped !!! 

getch();
}

Please tell me , why is this happening and what can i do to solve it !

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
parth_07
  • 1,322
  • 16
  • 22

1 Answers1

2

because of the stored enter key press [considered as character input]. use one getch(); before the third scanf().

alternatively, use (scanf(" %c",&c);) [mind the space before %c] which will get rid of any number of whitespace [buffered] character present before actual input.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261