문자의 입력

C : 2007. 11. 2. 11:33
 

getchar()  -  입력된 한 문자를 반환,   하나의 키를 누를 때까지 기다린다.

getche()   -   하나의 키를 누르면 즉시 반환한다. 
                   라인 버퍼 입력.  헤더 파일 <conio.h> 필요.


p.101
#include <stdio.h>
int main(void)
{
 char ch;

 ch = getchar();
 printf("you typed : %c, ch);

 return 0;
}

예제 1 p.103
#include <conio.h>
#include <stdio.h>

int main(void)
{
 char ch;
 printf("enter a character: ");
 ch = getche();
 printf("\nIts ASCII code is %d", ch);
 
 printf("\n\n");

 return 0;
}

ㄴgetchar()
#include <stdio.h>

int main(void)
{
 char ch;
 printf("enter a character: ");
 ch = getchar();
 printf("\nIts ASCII code is %d", ch);

 printf("\n\n");

 return 0;
}

예제2 p.104
#include <stdio.h>
int main(void)
{
 int a, b;
 char ch;
 printf("Do you want to:\n);
 printf("Add, Subtract, Multiply, or Divide?\n");
 printf("Enter first letter:");
 ch = getchar();
 printf("\n");

 printf("Enter first number:");
 scanf("%d", &a);
 printf("Enter second number:");
 scanf("%d", &b);

 if(ch=='A') printf("%d", a+b);
 if(ch=='S') printf("%d", a-b);
 if(ch=='M') printf("%d", a*b);
 if(ch=='D' && b!=0) printf("%d", a/b);

 return 0;
}

연습문제 1  p.105
#include <stdio.h>
#include <conio.h>

int main(void)
{
 int i;
 char ch, j='z';

 printf("문자 10 개 입력:\n");
 
 for(i=1; i<11; i++) {
  ch = getche();
  if(ch<j) j=ch;
 }
 printf("%c", j);

 printf("\n\n");
 return 0;
}

Posted by 청웨일