我是C的新手,并且有一个基本程序会向用户询问一个数字,然后打印这个数字以及它所在的范围,例如1-49。
答案 0 :(得分:2)
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int n;
do {
// Read the number into n
printf("Enter a number: ");
if (scanf("%d", &n) != 1) {
perror("scanf");
exit(EXIT_FAILURE);
}
// Check arbitrary condition
if (n >= 1 && n <= 49) {
printf("%d is in the range 1-49\n", n);
}
} while (n != 0);
return EXIT_SUCCESS;
}
答案 1 :(得分:0)
#include <stdio.h>
int main() {
for (;;) {
printf("Enter a number: ");
char buf[10];
fgets(buf, 10, stdin);
printf("You entered %s\n", buf);
// Code for displaying the range
if (buf[0] == '0')
break;
}
printf("Outside the loop\n");
return 0;
}