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()) != '
') {
        if (now == size-1) {
            while ((ch = getchar()) != '
') {
                continue;
            }
            break;
        } else {
            str[now++] = ch;
        }
    }
    str[now] = '';
    
    printf("%s
", str);

    return 0;
}
原文地址:https://www.cnblogs.com/noonjuan/p/11889226.html