Computer >> 컴퓨터 >  >> 프로그램 작성 >> C 프로그래밍

C 언어에서 scanf() 문을 사용할 때 발생하는 일반적인 오류는 무엇입니까?

<시간/>

문제

C 언어에서 scanf() 함수를 사용하여 문자열 및 숫자 데이터를 읽는 동안 일반적인 오류가 발생했습니다.

해결책

scanf() 함수 C 언어의 stdin에서 형식화된 입력을 읽는 데 사용됩니다. 그 안에 쓰여진 전체 문자 수를 반환하고 그렇지 않으면 음수 값을 반환합니다.

일반적으로 scanf() 함수의 경우 사용자로부터 정수 이후의 문자열 값을 읽어오다가 오류가 자주 발생합니다.

예시

다음은 롤 번호(정수 값)와 학생 이름을 읽는 C 프로그램입니다. -

#include <stdio.h>
struct student {
   char name[10];
   int roll;
} s;
int main(){
   printf("Enter information of students:\n");
   printf("\nEnter roll number: ");
   scanf("%d", &s.roll);
   printf("\nEnter name: ");
   gets(s.name);
   printf("\nDisplaying Information of students:\n");
   printf("\nRoll number: %d\t", s.roll);
   printf("\nname:%s\t", s.name);
   return 0;
}

출력

위의 예에서 roll no:는 컴파일러에 의해 읽혀졌습니다. 그 후에 컴파일러는 이름을 읽을 수 없으며 다음 명령문으로 이동합니다. printf("Roll Number is:%d\t, s.roll);

and the output is "Roll number: 3
name: "

C언어에서 scanf() 함수를 이용하여 문자열과 숫자 데이터를 읽을 때 흔히 발생하는 에러입니다.

Enter information of students:
Enter roll number: 3
Enter name: //error
Displaying Information of students:
Roll number: 3
name: //error