• 카테고리

    질문 & 답변
  • 세부 분야

    프로그래밍 언어

  • 해결 여부

    미해결

버퍼에 대한 궁금증

23.10.31 20:07 작성 조회수 88

1

#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>

int main()
{
    char ch;
    int characters=0, words=0, lines=0;
    bool word_flag=false;
    bool line_flag=false;
    
    printf("Enter text: \n");
    while( (ch=getchar())!='.'){
        if(isspace(ch)==false)//공백이 아닐경우
            characters++;
        if(isspace(ch)==false && line_flag==false){ 
            lines++;
            line_flag=true;
        }
        if(ch=='\n')
            line_flag=false;
        if(isspace(ch)==false && word_flag==false){
            words++;
            word_flag=true;
        }
        if(isspace(ch)==true)
            word_flag=false;
    }
    printf("characters:%d words: %d lines: %d",characters,words,lines);
    
    return 0;
}

위 코드에서 버퍼에 '.'(따옴표)까지 저장이 되는지 그전 문자까지만 저장이 되는지 궁금합니다.

답변 1

답변을 작성해보세요.

1

Soobak님의 프로필

Soobak

2023.11.01

안녕하세요, 답변 도우미 Soobak 입니다.

 

버퍼에는 입력된 모든 문자와 함께 마침표 `.` 까지 저장됩니다.
getchar() 함수가 버퍼로부터 `.` 문자를 읽어 들이면, while() 반복문이 종료됩니다.

 

예를 들어서, "Hello World." 라고 입력하면, 버퍼에는 "Hello World." 가 저장되고,
while() 반복문 내에서는 "Hello World" 까지만 처리하며, `.` 문자를 만나면 반복문이 종료됩니다.