C언어 수업정리/3월 수업정리

3월 13일 정리 문자입출력함수

송시혁 2013. 3. 13. 17:00

문자입출력함수

getchar() - 한글자 읽을 때 사용

putchar() - 한글자 출력할 때 사용


#include <stdio.h>
int main()
{
  printf("hello\n");
  getchar();
  return 0;
}



#include <stdio.h>
int main()
{
  char ch;
  printf("Please enter any character: ");
  ch = getchar();
  putchar (ch);
  printf(" is a letter you typed.\n");
  fflush (stdin);
  printf("Please enter any character:");
  scanf("%c"&ch);
  printf("%c is a letter you typed.\n", ch);
  return 0;
}



출력화면에서 보듯이 d를 입력하고 엔터를 쳐야 해당 프린트문이 생성된다.

즉, 버퍼를 기다리고 그것을 실행한다.

여기서 fflush는 보이지 않는 버퍼를 없애버린다. 여기서 보이지 않는 값은  엔터 키이다. 버퍼를 지움으로서 그 다음 문장인 printf("Please enter any character:");를 기다리게 하는 기능이다. 그러니깐 먼저, d is a letter you typed. 먼저 실행되고 입력을 받을 때까지 기다리다 문자를 입력하면 비로소 Please enter any character: 에 문자를 입력해야 a is a letter you typed가 생성된다.


#include <conio.h>// getch(), getch(), putch()
#include <stdio.h> printf()
int main()
{
  int ch;
  printf("Please enter any character: ");
  ch = getche();
  putch (ch);
  printf(" is a letter you typed:\n");
  
  printf("Please enter any character: \n");
  ch = getch(); //입력문자는 화면에 출력되지 않음.
  putch(ch);
  printf(" is a letter you typed.\n");
  return 0;
}

conio.h는 콘솔입출력 함수.
getch()는 출력화면 입력시 아예 보여주지 않는다. 리눅스 패스워드
getche() 는 값을 출력이 되나, 값과는 다른 형태로 출력. 예) 패스워드:*******
putch() 출력

a를 입력하면 엔터키를 치지 않아도 화면에 출력이 된다.


반복문

while(정지조건) -> 논리값

while(참) 정지, 그렇지 않으면 무한반복

프로그램 정지시키는 법 ctrl + break, ctrl + c 가능하면 ctrl + break를 
쓴다 컨트롤 시는 안먹힐 때가 있다.

형식

while()

{

명령문

}


#include <stdio.h>
int main()
{
  int num;
  num = 0//제어변수 n의 초기화
  while(num <=5//반복여부를 결정하는 논리식
  {
    printf("%d\t", num); //\t tab건너뛰다. num = num +1;으로 6이되면 탈출.
      num = num +1// 제어변수의 값의 변경
  }
  printf("\n");
   return 0;
}




#include <stdio.h>
int main()
{
  unsigned int uiCnt = 490;
  
  while(1)
  //while(500 >=uiCnt)

  {
    if(uiCnt>500)
    {
      break;
      
    }
    printf("%u he\n", uiCnt);
    ++uiCnt;

  }  
  
/*      
  while(1)
  {
  }
*/

  
   return 0;
}
while문에 uiCnt가 500이 되었을때 실행을 멈춘다. 그전에는 조건을 계속 만족시켜

 반복적으로 실행이 된다.그래서 490 he, 491 he....가 차례대로 실행된다.

여기서 주의 할것은 아래 출력화면을 보면 500 he까지 출력되는 것을 확인 할 수 있다.초기값 490에서

500이 넘으면 실행이 되지 않다고 조건을 걸고 있으나, 실제로

실행하는 횟수는 11번이다.그래서 500 he까지는 출력이 되고 501 he가 되면

 정지가 되어 실행되지 않는다.



5단 구구셈

#include <stdio.h>
int main()
{
  unsigned int uiCnt;
  unsigned char ucdan = 5;
  uiCnt = 1;// 여기에 초기값을 선언해야한다.
  
  while(uiCnt<=9)
  {
    printf("%d * %d= %d\n",ucdan, uiCnt, uiCnt*ucdan);
    uiCnt = uiCnt +1;
  }
  

  
    
   return 0;
  
}