getchar & putchar
示例
#include <stdio.h>
void cp_v1()
{
int c;
c = getchar();
while (c != EOF)
{
putchar(c);
c = getchar();
}
}
void cp_v2()
{
int c;
while ((c = getchar()) != EOF)
putchar(c);
}
int main()
{
cp_v1();
return 0;
}
$ gcc copy.c -o copy
$ echo -e "hello\nworld" > README.md
$ ./copy <README.md
hello
world
$ ./copy
123
123
^D
#include <stdio.h>
long cc_v1()
{
long count;
for (count = 0; getchar() != EOF;)
count++;
return count;
}
long cc_v2()
{
long count = 0;
while (getchar() != EOF)
count++;
return count;
}
int main()
{
long count;
count = cc_v1();
printf("%ld\n", count);
}
$ gcc countofchar.c -o countofchar
$ ./countofchar
1234
5678
10
#include <stdio.h>
int main()
{
int c;
long line;
line = 0;
while ((c = getchar()) != EOF)
{
if (c == '\n')
line++;
}
printf("%ld\n", line);
}
$ gcc linecount.c -o linecount
$ ./linecount
123456
abcdef
2
#include <stdio.h>
int main()
{
int c;
while ((c = getchar()) != EOF)
{
}
}