-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.c
41 lines (35 loc) · 876 Bytes
/
input.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <stdio.h>
#include <assert.h>
void test_fgets() {
char content[60] = {0};
FILE * fp = fopen("readme.md", "r");
char * r = fgets(content, sizeof(content), fp);
if (NULL != r) {
// "hello\n", '\000' <repeats 53 times>
puts(content);
assert(content[5] == '\n');
}
fclose(fp);
}
void test_scanf() {
char content[4] = {0};
printf("Enter a string:");
scanf("%5s",content);
printf("%s\n", content);
return;
}
void test_getc() {
char c = '\0';
// getc只接收一个字符,如果输入多个,会自动缓冲给下面的getc
printf("input a char:");
c = getc(stdin);
putc(c, stdout);
c = getc(stdin);
putc(c, stdout);
}
int main() {
test_fgets();
test_getc();
test_scanf(); // 如果getc输多了,会buffer给scanf,而且可能给不全
return 0;
}