본문 바로가기
C/Definition&Grammar

[C] getch / putch

by 꾸압 2021. 3. 12.

getch();

정의 : 문자 하나를 입력받는 함수. 대기함수로서 문자가 입력될 때까지 기다린다.

 

<예시>

#include <stdio.h>
#include <conio.h>

int main(){
	
	int word;
	
	printf("프로그램을 계속 진행하시겠습니까? (y/n)\n");
	word = getch();
	
	if(word == 'y'){
		printf("\n%c 를 입력하셨습니다.\n", word);
	}
	
	else{
		printf("\n프로그램을 종료합니다.\n");
	}
	
	return 0;
}

getch 를 사용하려면 <conio.h> 함수를 사용한다.

 

- getche(); 라는 함수도 있다. 이는 메아리(Echo) 처럼 입력받은걸 화면에 확인시키듯 보여준다.

<예시>

#include <stdio.h>
#include <conio.h>

int main(){
	
	int word;
	
	word = getche();
	
	printf("%c\n", word);
	
	return 0;
}

 

 

putch();

정의 :

   - <conio.h> 헤더 파일의 출력 함수. 출력버퍼(printf)를 쓰지 않아도 문자 하나를 바로 출력 가능.

   - <stdio.h> 헤더 파일의 putchar(); 함수는 출력 버퍼 사용.

 

<예제>

#include <stdio.h>
#include <conio.h>

int main(){
	
	int word = 'p';
	
	putch(word);
	
	return 0;
}

 

 

 

'C > Definition&Grammar' 카테고리의 다른 글

[C] 버퍼(buffer)  (0) 2021.03.15
[C] 유니온 (Union)  (0) 2021.03.12
[C] 그래프 알고리즘 (Graph Algorithm)  (0) 2021.03.11
[C] 삽입 / 선택 / 버블 정렬  (0) 2021.03.10

댓글