C getchar()

C getchar()

#include <stdio.h>

int main()
{
    int size = 5;
    char str[size];
    int now = 0;
    char ch;

    printf("Enter whatever you want: ");
    while ((ch = getchar()) != '\n') {
        if (now == size-1) {
            while ((ch = getchar()) != '\n') {
                continue;
            }
            break;
        } else {
            str[now++] = ch;
        }
    }
    str[now] = '\0';
    
    printf("%s\n", str);

    return 0;
}
posted @ 2019-11-19 14:31  no樂on  阅读(129)  评论(0编辑  收藏  举报