【发布时间】:2017-01-07 22:38:48
【问题描述】:
这个代码单元是一个更大的 C 数据库代码的一部分。这部分需要学生的成绩。要求是使用 typedef unsigned char uint8 而不是简单的 int。 为了我的生活,我无法让它工作。当我在 scanf 中使用 %c 时,它会跳过。如果它有时会打印第一个数字,则在打印中。所以这是带有 int 的代码,它工作正常,我如何使它与 uint8 或 unsigned char 一起工作???
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>
#include <inttypes.h>
#include <string.h>
//typedef unsigned char uint8;
typedef unsigned int uint8;
int main(void)
{
uint8 grades_t[3], gr_c, g_temp;
for (gr_c = 0; gr_c < 3;)
{
printf("please enter the grade (0-100) of subject no%d: ", gr_c+1);
scanf("%d", &g_temp);
if (g_temp < 0 || g_temp > 100) //only accept values between 0-100
{
printf("please enter valid grade from 0 - 100!\n");
gr_c --; //if value is out of range, decrement
}
else //store value
{
grades_t[gr_c] = g_temp;
}
gr_c++;
}
printf("grade = %d\n", grades_t[0]); //unite test
printf("grade = %d\n", grades_t[1]);
printf("grade = %d", grades_t[2]);
return 0;
}
【问题讨论】:
-
欢迎来到 Stack Overflow。请注意,在这里说“谢谢”的首选方式是投票赞成好的问题和有用的答案(一旦你有足够的声誉这样做),并接受对你提出的任何问题最有帮助的答案(这也给出了你的声誉小幅提升)。请查看About 页面以及How do I ask questions here? 和What do I do when someone answers my question?
标签: c char printf scanf uint8t