getchar and putchar in c language

getchar and putchar can only read or output one character at a time

#include <stdio.h>

int main()
{
    int ch;
    int a_begin=1;
    int line;
    line =0;
    while((ch=getchar())!='3'){
        if (a_begin==1) {
            a_begin=0;
            line+=1;
            printf("%d.",line);
            
        }
        putchar(ch);
        if (ch=='\n') {
            a_begin=1;
        }
     
    }
    return 0;
}

Here, when putchar reads 3, it will stop running.
The return value of getchar is int, so the type of ch should be declared as int. ≠≠≠


Guess you like

Origin blog.csdn.net/solo_bro/article/details/104662293