Home

The Space Before %c: scanf Input Buffer Mystery

//in the while loop, what is the difference?  
scanf(" %c", &op); 
scanf("%c", &op); 

That tiny space before the %c is easily one of the most frustrating traps in C programming. It makes a massive difference because of how scanf handles whitespace (like spaces, tabs, and especially the Enter key / newline character \n).

Here is the short answer: The space tells scanf to skip over any leftover whitespace before reading the character. Without the space, scanf grabs the very next character it finds, which is usually the Enter key you pressed on the previous loop.


The Root of the Problem: The Input Buffer

When you type a character and press Enter, you are actually putting two characters into the input buffer:

  1. The character you wanted (e.g., ‘a’)
  2. The newline character (’\n’) from pressing Enter.

Let’s look at exactly what happens inside a while loop for both versions.

Version 1: scanf("%c", &op); (No Space)

This version is literal and strict. It reads the absolute next character in the buffer, no matter what it is.

Version 2: scanf(" %c", &op); (With Space)

The space act as a vacuum cleaner for whitespace.


Note on other data types: You don’t have this issue with scanf("%d", &num) or scanf("%f", &num) because specifiers like %d and %f automatically skip leading whitespace by default. %c is unique because a space or a newline is a valid character, so C assumes you might actually want to read it.

Tags: CAlgorithms