0

This is my code:

#include<stdio.h>

int main()
{
    int i;
    float f;
    char c;

    scanf("%d", &i);
    scanf("%f", &f);
    scanf("%c", &c);

    printf("The integer value: %d \n", i);
    printf("The floating point value: %f \n", f);
    printf("The character value: %c \n", c);

    return 0;
}

When I ran this code, only scanf for i and f worked. But scanf for c not worked and it skipped to the next print function.

42
3.152
The integer value: 42
The floating point value: 3.152000
The character value:


Process returned 0 (0x0)   execution time : 6.871 s
Press any key to continue.

I can't find any solution for this problem. Is it a bug of compiler?

Nurul Alom Ador
  • 377
  • 1
  • 2
  • 10
  • 1
    As noted in the duplicate, `scanf()` leaves the newline character in the input buffer. Most conversion specifiers skip white space (and white space includes newlines), but there are three that don't — `%c`, `%[…]` (scan sets), and `%n`. You are running foul of `%c`. The usual solution is to use `" %c"` instead of just `"%c"`. The blank skips white space (such as the newline left in the input buffer after reading a float), and then processes the next character. – Jonathan Leffler Nov 26 '22 at 07:00

0 Answers0