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
  • 115,525
  • 42
  • 225
  • 281
parth_07
  • 1,182
  • 15
  • 20

1 Answers1

1

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
  • 130,437
  • 16
  • 177
  • 247