c - 单位化变量误差

标签 c

问题:我正在进行一项看似非常简单的分配,但是,我收到一条错误,指出变量“test1”未初始化。我将其声明为 int,然后在 scanf 语句中对其进行初始化。这里有什么帮助吗?

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

void main()
{
int hours, hours2, test1, test2, test3, avg, well;
avg = (test1 + test2 + test3) / 3;

printf("Enter your Cprogram Test grades here for Test 1, 2, and 3: \n");
scanf("%d%d%d", &test1, &test2, &test3);
printf("The average of these grades are: %d. \n", avg);

最佳答案

您在该行中使用了变量test1

avg = (test1 + test2 + test3) / 3;

位于 scanf 之前。因此 test1 (以及同样的 test2test3)在当时使用时未初始化。

关于c - 单位化变量误差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35487750/

相关文章:

c - 在 C 中取消引用指向范围外静态数据的指针

编译器错误: control reaches end of non-void function

c - 如何仅使用单词初始化 C 代码

将 unsigned int 转换为有符号 int C

c - 嵌套在循环中的公式将无法正确执行

c - 无法在我的 C 代码中初始化回调。不兼容的引用类型导致未定义的行为

c - 插入节点(二叉搜索树)C

将数字读入数组时代码崩溃...C 语言

c - 是否可以使用Rust中的指针访问结构的元素?

objective-c - Apple 的 BetterAuthorizationSample 在 10.6.6 上不起作用