函数中未初始化的变量

Variables uninitialized in function

我的任务是编写一个接受三个输入并返回三个值的程序。整个程序在这里:

#include <stdio.h>


    int findPercent(int percent)
    {
    int p = percent / 100;
    return p;
    }

    double convertSquareYards(int a)
    {
    double sy = a * 4840;
    return sy;
    }

    double convertCubicYards(double sy, double i, int p)
    {
    double cy = (sy * p) * (i / 36);
    return cy;
    }

    double convertGallons(double cy)
    {
    double g = cy / 201.974026;
    return g;
    }

    double convertPounds(double g)
    {
    double lb = g / 8.3430;
    return lb;
    }

    double convertTonnes(double lb)
    {
    double t = lb / 2000;
    return t;
    }

    double convertCubicFeet(double cy)
    {
    double cf = cy * 27;
    return cf;
    }

    double findHeight(double cf)
    {
    double h = ((cf / (360 * 160)) / 5280);
    return h;
    }

    int main(void)
    {

    double g, t, h, lb, i, cy, a, percent, cf;

    printf("What is the size of the county in acres? ");
    scanf("%lf", &a);

    printf("How much rainfall was recieved, in inches? ");
    scanf("%lf", &i);

    printf("What percent of the county recieved rainfall? ");
    scanf("%lf", &percent);


    g = convertGallons(cy);
    t = convertTonnes(lb);
    h = findHeight(cf);

    printf("%lf gallons fell in the county.\n", g);
    printf("The rainfall weighed %lf tonnes.\n", t);
    printf("The height of the football field is %lf miles.\n", h);

    return 0;
    }

当我尝试编译这个程序时,出现以下错误:

project.c:67:3: warning: ‘cy’ is used uninitialized in this function [-Wuninitialized]
g = convertGallons(cy);
  ^
project.c:68:3: warning: ‘lb’ is used uninitialized in this function [-Wuninitialized]
t = convertTonnes(lb);
  ^
project.c:69:3: warning: ‘cf’ is used uninitialized in this function [-Wuninitialized]
h = findHeight(cf);
  ^

我敢肯定这个程序有很多问题(这是我的第一个真正的程序),如果您愿意,我很乐意听到任何其他意见。先感谢您。

在这种情况下,警告非常具有描述性;您在初始化变量之前使用变量,这在 C 中不是一个好主意。例如

 g = convertGallons(cy);

cy在上面使用之前没有初始化。使用

double cy = 0; // Or some other initial value

声明期间(其他变量相同)。读取未初始化变量的值(这是将变量传递给函数时发生的情况 - 制作了它的副本,因此在某种程度上是 read)是未定义的行为并且似乎编译器对此有所抱怨。

因为变量已声明但未初始化为任何值(它们可以包含任何值)。您可能需要执行某些计算以将它们初始化为某些值,然后将它们传递给函数。

Amount of rainfall = area * inches of rainfall,等等

cy = a * (percent/100.0) * i ; 
/*calculates the volume of rainfall in acres * inches unit and provided you
  took percentage as 56% and not 0.56 (in which case, remove the division by 100.0)*/

考虑这部分代码:

printf("What is the size of the county in acres? ");
scanf("%lf", &a);

printf("How much rainfall was recieved, in inches? ");
scanf("%lf", &i);

printf("What percent of the county recieved rainfall? ");
scanf("%lf", &percent);


g = convertGallons(cy);
t = convertTonnes(lb);
h = findHeight(cf);

假设 您要根据输入的值进行计算。但是此代码为 aipercent 分配了一个值,同时将未初始化的变量 cylbcf 提供给您的计算功能。

所以我进一步假设存在 基本 对变量如何工作以及它们的范围有什么误解。您在块内声明的变量(一对 {})仅存在于该块内。程序中可以有其他变量 同名 在另一个块中——它们是不同的并且完全不相关。

特别是,函数的参数也是局部变量,并且仅在该函数内部有效。

因此,以您的代码为例,此处 cy

double convertGallons(double cy)
{
    double g = cy / 201.974026;
    return g;
}

是一个与这里的 cy 完全不同的变量:

int main(void)
{

    double g, t, h, lb, i, cy, a, percent, cf;

cy in main 需要有一个值,如果你想做 convertGallons(cy) - 那么函数调用将分配 cy 在你的 main()convertGallons() 中的 cy。为了更好地理解这一点,请尝试为这些变量赋予不同的名称。但是,当然,同名不是错误,在某些情况下很常见。