【发布时间】:2017-07-04 23:08:01
【问题描述】:
这是用 'c' 编写并用 gcc 编译的。我不确定您还需要知道什么。
我可以放在一起的最小的完整示例如下所示。变量 'numatoms' 在到达第 23 行时(在 scanf() 之后)丢失了它的值。
我被难住了。也许它与 scanf() 覆盖 numatoms 的空间有关?
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
/*
*
*/
int main(int argc, char** argv) {
uint8_t numatoms;
uint8_t r;
char a[20];
do {
printf("NO. OF ATOMS?");
fflush(stdout);
scanf("%d", &numatoms);
printf("\r\n");
for(;;){
printf("value of numatoms is %u\r\n", numatoms);
printf("RAY?");
fflush(stdout);
scanf("%u", &r);
printf("value of numatoms is %u\r\n", numatoms);
if(r < 1)
break;
else {
printf("value of numatoms is %u\r\n", numatoms);
}
}
printf("CARE TO TRY AGAIN?");
fflush(stdout);
scanf("%s", a);
printf("\r\n");
} while (a[0] == 'y' || a[0] == 'Y');
return (EXIT_SUCCESS);
}
【问题讨论】:
-
@PatrickRoberts 不是吗?
-
没关系,显然我错了。
-
scanf("%d", &numatoms);-->scanf("%" SCNu8, &numatoms);(<inttypes.h>) -
为了便于阅读和理解:1) 一致地缩进代码。在每个左大括号“{”后缩进。在每个右大括号 '}' 之前不缩进。建议每个缩进级别 4 个空格。 2) 通过一个空行分隔代码块(for、if、else、while、do...while、switch、case、default)。
-
向用户输出提示时,不需要刷新标准输出。调用输入函数
scanf()的行为将导致显示提示。