Copy STDIN to STDOUT
#include<stdio.h>
int main() {
char c;
while ((c = getchar()) != EOF) {
putchar(c);
}
return 0;
}
- stdio.h is needed to get the definition of
EOFEnd of file.
gcc copy.c
./a.out
Type some text
Ctrl-D
- On Linux and macOS we can press
Ctrl-Don an empty line to sendEOF. - We can also redirect some file. e.g.
./a.out < copy.cwill redirect the content of thecopy.cfile to the STDIN of the program and that will print it to the screen.