0

So this is my c code

#include <stdio.h>
void main()
{
    int a;
    char b;
    printf("Enter input \n");
    scanf("%d", &a);
    printf("Enter char input \n");
    scanf("%c", &b);
    printf("%d,%c", a, b);
}

And I'm getting output as

Enter input 
3
Enter char input 
3,

Can you explain me why its not taking character value the second time. This program works well if i take charcter input first.

Raw Newton
  • 53
  • 9
  • There's a newline character left in the input stream after the first `scanf`; the second, with the `%c` format, reads that as its input. Add a space before the format specifier: `scanf(" %c", &b);` to fix. This has been answered in other posts; looking for a good one... – Adrian Mole Jan 27 '21 at 15:37
  • I didn't gave space earlier. – Raw Newton Jan 27 '21 at 15:40

1 Answers1

0

you are entering space or enter button after integer input, So b is assigned that space/enter.

add getchar(); before input b