
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
What is the common error occurred while using scanf() statement in C language?
Problem
Common error occurred while reading string and numeric data using scanf() function in C language
Solution
The scanf() function is used to read formatted input from stdin in C language. It returns the whole number of characters written in it otherwise, returns a negative value.
Generally in case of scanf() function while reading string values after integer from the user, we get frequent errors.
Example
Following is a C program which reads roll number (integer value) and name of a student −
#include <stdio.h> struct student { char name[10]; int roll; } s; int main(){ printf("Enter information of students:
"); printf("
Enter roll number: "); scanf("%d", &s.roll); printf("
Enter name: "); gets(s.name); printf("
Displaying Information of students:
"); printf("
Roll number: %d\t", s.roll); printf("
name:%s\t", s.name); return 0; }
Output
In the above example, roll no: was read by the compiler, After that compiler is not able to read name and moves to next statement which was printf("Roll Number is: %d\t, s.roll);
and the output is "Roll number: 3 name: "
This is the common error occurred while reading string and numeric data using scanf() function in C language.
Enter information of students: Enter roll number: 3 Enter name: //error Displaying Information of students: Roll number: 3 name: //error
- Related Articles
- What are the error handling techniques in C language?
- What is while loop statement in JavaScript?
- Resolve ERROR 1064 (42000) that occurred after using varchar (without providing the size)
- Problem with scanf() when there is fgets()/gets()/scanf() after it in C
- What is do...while loop statement in JavaScript?
- What is the use of ‘Using’ statement in C#?
- Error while using LOOP…..WHERE in SAP ABAP
- Explain switch statement in C language
- Find out the GCD of two numbers using while loop in C language
- What is a common debugging workflow while creating a model using Keras in Python?
- Difference between while(1) and while(0) in C language
- scanf() and fscanf() in C
- Error while using enter "!" on selection screen in SAP.
- Getting error while using schema in SAP HANA Modeling
- Common Language Runtime (CLR) in C#.NET
