SCANF()

scanf() skips whitespace for most formats like %d, %f, and %s, but not for %c. To avoid reading leftover whitespace (like newlines) when using %c, add a space before %c in the format string

#include <stdio.h>

int main() {
    int num;
    char ch;

    scanf("%d", &num);

    scanf(" %c", &ch);  // Note the space before %c

    printf("Number: %d, Character: %c\n", num, ch);

    return 0;
}

 * (space before %c): Skips any whitespace characters (spaces, tabs, newlines) before 
   reading the character.

Last updated