logo

C의 Getchar() 함수

이번 절에서는 C 프로그래밍 언어의 getchar() 함수를 배워보겠습니다. ㅏ getchar() 기능은 비표준 그 의미가 이미 정의되어 있는 함수 표준입력.h 사용자로부터 단일 입력을 받아들이는 헤더 파일입니다. 즉, stdin에서 단일 문자(부호 없는 문자)를 가져오는 것은 C 라이브러리 함수입니다. 그러나 getchar() 함수는 getc() 함수와 유사하지만, getchar() 함수와 getc() 함수 간에는 약간의 차이가 있습니다. C 프로그래밍 언어 . getchar()는 표준 입력에서 단일 문자를 읽는 반면, getc()는 모든 입력 스트림에서 단일 문자를 읽습니다.

C의 Getchar() 함수

통사론

 int getchar (void); 

매개변수가 없습니다. 그러나 읽은 문자를 unsigned char 형태로 int형으로 반환하고, 파일에 오류가 있으면 EOF 파일 끝에.

이제 C에서 단일 문자를 받아들이고 putchar() 함수를 사용하여 이를 인쇄하는 여러 getchar() 함수 프로그램을 작성합니다.

getchar() 함수를 사용하여 단일 문자 읽기

C에서 getchar() 함수를 사용하여 싱글을 취하는 프로그램을 생각해 봅시다.

프로그램.c

 #include #include void main() { char c; printf ('
 Enter a character 
'); c = getchar(); // get a single character printf(' You have passed '); putchar(c); // print a single character using putchar getch(); } 

산출

df.loc
 Enter a character A You have passed A 

위 프로그램에서 볼 수 있듯이 런타임 시 getchar() 함수를 사용하여 사용자로부터 단일 문자를 가져옵니다. 문자를 가져온 후 putchar() 함수를 통해 문자를 인쇄합니다.

getchar() 함수를 사용하여 사용자로부터 n개의 문자를 읽습니다.

C에서 getchar() 함수를 사용하여 n 문자를 읽는 프로그램을 고려해 보겠습니다.

Getchar.c

 #include #include #include int main() { char ch; printf (' Enter a character ( If we want to exit press #) 
'); while (ch != '#') /* accept the number till the user does not enter the # to exit from the loop. */ { ch = getchar(); printf (' 
 We have entered the character : '); putchar (ch); // print a single character printf ('
'); } return 0; } 

산출

 Enter a character ( If we want to exit.. press #) A We have entered the character: A We have entered the character: B We have entered the character: B We have entered the character: C We have entered the character: C We have entered the character: 

위 출력에서 ​​볼 수 있듯이 while 루프는 사용자가 # 문자를 전달하지 않을 때까지 계속해서 사용자의 문자를 받아들입니다. 여기서 getchar() 함수는 표준 입력에서 단일 문자를 가져와 ch 변수에 할당합니다. putchar() 함수는 읽은 문자를 인쇄합니다.

scanf() 함수를 사용하여 단일 문자 읽기

C에서 scanf() 라이브러리 함수를 사용하여 문자를 읽는 프로그램을 고려해 보겠습니다.

Prog.c

배열 목록.정렬
 #include #include int main() { char ch; printf ('
 Enter the character 
'); scanf ('%c', &ch); // get a single character, numeric or words printf( ' You have entered %c', ch); /* It print a single character or first letter of the words. */ return 0; } 

산출

 Enter the character A You have entered A 

보시다시피 위 프로그램을 실행할 때 getchar() 함수 대신 scanf() 라이브러리 함수를 사용하여 단일 문자 또는 문자 그룹을 사용합니다. 그러나 약간의 차이가 있습니다. scanf() 함수는 사용자로부터 단일 문자 또는 문자 그룹을 가져올 수 있는 반면, getchar() 함수는 단일 문자만 허용할 수 있습니다.

여기서 다시 위 프로그램을 실행하면, 이번에는 아래와 같은 결과를 보여줍니다.

 Enter the character Apple You have entered A 

do-while 루프를 사용하여 문자 읽기

C에서 do while과 getchar() 함수를 사용하여 문자를 읽는 프로그램을 생각해보자.

Dowhile1.c

날짜에 대한 문자열
 #include #include int main() { int ch, i = 0; char str[150]; printf (' Enter the characters from the keyboard (Press Enter button to stop).
'); // use do while loop to define the condition do { ch = getchar(); // takes character, number, etc from the user str[i] = ch; // store the ch into str[i] i++; // increment loop by 1 } while (ch != '
'); // ch is not equal to '
' printf('Entered characters are %s ', str); return 0; } 

산출

 Enter the characters from the keyboard (Press Enter button to stop). Well b47gvb come Entered characters are Well b47gvb come 

위 프로그램에서 do-while 루프는 사용자가 문자를 통과할 때까지 계속해서 문자를 받아들입니다. 입력하다 루프를 종료하려면 버튼을 누르세요.