What is the common error occurred while using scanf() statement in C language?

The scanf() function in C is commonly used to read formatted input, but it often causes issues when reading strings after numeric values. The main problem occurs because scanf() leaves a newline character in the input buffer after reading numbers, which interferes with subsequent string input functions.

Common Error

When using scanf() to read numeric data followed by string input with gets() or fgets(), the string input is skipped because the leftover newline character from the previous scanf() is immediately consumed.

Example: Demonstrating the Problem

Here's a program that shows the common error when reading a roll number and name −

#include <stdio.h>

struct student {
    char name[50];
    int roll;
} s;

int main() {
    printf("Enter information of students:<br>");
    printf("Enter roll number: ");
    scanf("%d", &s.roll);
    
    printf("Enter name: ");
    fgets(s.name, sizeof(s.name), stdin);
    
    printf("\nDisplaying Information:<br>");
    printf("Roll number: %d<br>", s.roll);
    printf("Name: %s", s.name);
    
    return 0;
}
Enter information of students:
Enter roll number: 123
Enter name: 
Displaying Information:
Roll number: 123
Name: 

Solution: Clear the Input Buffer

To fix this issue, we need to consume the leftover newline character before reading the string −

#include <stdio.h>

struct student {
    char name[50];
    int roll;
} s;

int main() {
    printf("Enter information of students:<br>");
    printf("Enter roll number: ");
    scanf("%d", &s.roll);
    
    /* Clear the input buffer */
    while (getchar() != '<br>');
    
    printf("Enter name: ");
    fgets(s.name, sizeof(s.name), stdin);
    
    printf("\nDisplaying Information:<br>");
    printf("Roll number: %d<br>", s.roll);
    printf("Name: %s", s.name);
    
    return 0;
}
Enter information of students:
Enter roll number: 123
Enter name: John Doe
Displaying Information:
Roll number: 123
Name: John Doe

Alternative Solution: Using scanf() for Strings

Another approach is to use scanf() with proper format specifiers for string input −

#include <stdio.h>

struct student {
    char name[50];
    int roll;
} s;

int main() {
    printf("Enter information of students:<br>");
    printf("Enter roll number: ");
    scanf("%d", &s.roll);
    
    printf("Enter name: ");
    scanf(" %49s", s.name); /* Note the space before %s */
    
    printf("\nDisplaying Information:<br>");
    printf("Roll number: %d<br>", s.roll);
    printf("Name: %s<br>", s.name);
    
    return 0;
}
Enter information of students:
Enter roll number: 123
Enter name: John
Displaying Information:
Roll number: 123
Name: John

Key Points

  • Always clear the input buffer with while (getchar() != '
    ');
    after scanf() when reading strings next.
  • Use a leading space in scanf(" %s", ...) to skip whitespace characters including newlines.
  • Avoid gets() as it's unsafe and removed from C11. Use fgets() instead.
  • Specify buffer limits in scanf() format strings to prevent buffer overflow.

Conclusion

The common scanf() error occurs due to leftover newline characters in the input buffer. Use buffer clearing techniques or proper format specifiers to handle mixed numeric and string input correctly.

Updated on: 2026-03-15T13:06:07+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements