You have to change
scanf("%c", &d);
to
scanf(" %c", &d);
^
|
Otherwise, scanf()
will consider the previously entered ENTER
key press.
Note:
ENTER key press generates a \n
, which is a vaild input for %c
format specifier. Adding a space before %c
tells scanf()
to ignore all leading whitespace-like inputs (including that previously stored \n
) and read the first non-whitespace character from stdin
.
As for the case with %d
format specifier, it consumes (and ignores) any leading whitespace-like inputs before scanning for numeric inputs, so the second case does not suffer any issues.