3 回答
TA贡献1853条经验 获得超6个赞
%cscanf
scanf(" %c", &c);scanf%c
TA贡献2051条经验 获得超10个赞
scanf()
scanf()
#include <stdio.h>int main(void){
char ch1, ch2;
scanf("%c", &ch1); /* Leaves the newline in the input */
scanf(" %c", &ch2); /* The leading whitespace ensures it's the
previous newline is ignored */
printf("ch1: %c, ch2: %c\n", ch1, ch2);
/* All good so far */
char ch3;
scanf("%c", &ch3); /* Doesn't read input due to the same problem */
printf("ch3: %c\n", ch3);
return 0;}scanf()abcintscanf("%d", &int_var);abc
#include <stdio.h>int main(void){
int i;
while(1) {
if (scanf("%d", &i) != 1) { /* Input "abc" */
printf("Invalid input. Try again\n");
} else {
break;
}
}
printf("Int read: %d\n", i);
return 0;}scanf()fgets()
#include <stdio.h>int main(void){
int age;
char name[256];
printf("Input your age:");
scanf("%d", &age); /* Input 10 */
printf("Input your full name [firstname lastname]");
fgets(name, sizeof name, stdin); /* Doesn't read! */
return 0;}fgets()
scanf()
fgets()
#include <stdio.h>int main(void){
char line[256];
char ch;
if (fgets(line, sizeof line, stdin) == NULL) {
printf("Input error.\n");
exit(1);
}
ch = line[0];
printf("Character read: %c\n", ch);
return 0;}fgets()
char line[256];if (fgets(line, sizeof line, stdin) == NULL) {
printf("Input error.\n");
exit(1);}line[strcpsn(line, "\n")] = 0; /* removes the trailing newline, if present */- 3 回答
- 0 关注
- 707 浏览
添加回答
举报
