【问题标题】:Why does the compiler say that the variable "z" is redeclared as a different variable type?为什么编译器会说变量“z”被重新声明为不同的变量类型?
【发布时间】:2015-01-17 21:14:45
【问题描述】:

我开始学习如何在 c 中使用函数,我试图创建一个除整数的函数,但编译器给了我一个错误。我搜索了谷歌,这导致我在这里经常问这个问题,但我不明白错误到底是什么。所以,我重新发布它。

请注意,编译器的问题在于“z 而不是“div”。

代码如下:

 #include<stdio.h>

float div(int x, int z);
void main()
{
  int n,m;
  float div;
  printf("Enter first number: \n");
  scanf("%d",&n);
  printf("Enter second number: \n");
  scanf("%d",m);
  if(m!=0)
  {
    div=div(n,m);
    printf("%d/%d=%f \n",n,m,div);
  }
}

float div(int x, int z)
{
  int x,z;
  float div;

  if(z!=0){
    div=x/z;
    return div;
  }

}

【问题讨论】:

  • div 现在是一个函数和一个浮点数。
  • 即使我编辑“div”并让它说,divee,问题还是出现了。事实上,编译器说问题出在 z 而不是 div。
  • 你的div函数的参数是xz,但是你声明了同名的局部变量,你不能。函数参数是从外部传入的局部变量。它们必须与所有其他局部变量一起存在而不会发生名称冲突。
  • 请将您的第二个scanf 更改为scanf("%d", &amp;m),地址运算符为&amp;。 (你猜对了,在上面两行。)
  • @learner:很好。许多人都按照同样的思路发布了长而详细的答案。请接受其中之一。

标签: c function compiler-errors declaration


【解决方案1】:

你做错了几件事:

(来自BLUEPIXY的cmets,另外你应该避免名称div,因为一些标准库使用它)

#include <stdio.h>

/*Prototypes*/
float divve(int x, int z);
    //^^^^^ Changed name of the function since you have a float variable with the same name in main

int main() {
//^ Default return type of main is int

    int n, m;
    float div;

    printf("Enter first number: \n");
    scanf("%d",&n);
    printf("Enter second number: \n");
    scanf("%d", &m);
              //^ You forgot the address operator here

    if(m != 0) {
        div = divve(n, m);
        printf("%d/%d=%f \n", n, m, div);
    }

    return 0;
  //^^^^^^^^^ Return to end the program nicely 

}

float divve(int x, int z) {

    //Removed second declaration of x and z
    float div;

    if(z != 0){
     //^ Changed y to z since you don't have a y variable in the function | BTW Also you already checked if m is != 0 in the main function
        div = (float)x / (float)z;
        return div;
    }

    return 0;
  //^^^^^^^^^ return something if the if statement fails

}

【讨论】:

  • div 必须避免,因为标准库中的某些函数名称。还有x / z --> (float)x / z;
  • @BLUEPIXY,为什么 (float)x/y 不只是 x/y?
  • @learner 自己试试看,你会看到的! 3/5 (x/y) -&gt; 0.0 | 3/5 ((float)x /y) -&gt; 0.6
  • @learner 这是1/2 0.5 而不是0 所必需的。
【解决方案2】:

问题来了:

float div(int x, int z)
{ //                 ^
    int x,z; // << Two 'z's
    float div;

    if(y!=0){
        div=((float)x) / z; // Add a cast to avoid integer division
        return div;
    }
    return 0;
}

div 内的作用域中已经有一个z,所以第二个声明(局部变量)与第一个声明(函数的形参)冲突。

请注意,我为y 为零的情况添加了return 语句。当控制到达非 void 函数的末尾时,这将避免未定义的行为。

还请注意,最好避免使用相同的标识符命名变量和函数,因此最好在 main 中重命名 div 变量。

【讨论】:

    【解决方案3】:

    您正在声明与函数div 中的函数参数同名的变量。

    但是问题比较多,按照出现的顺序列举出来

    1. 您将函数命名为div(),然后在main() 中声明了一个同名变量来存储函数的返回值。

    2. 您的第二个 scanf() 缺少 &amp;,我认为这是一个错字。

    3. 您在 div() 函数中声明了 xz,它们是函数参数的名称,然后您测试未在任何地方声明的 y == 0

    4. 您将整数相除,如果例如 z &gt; x0,则会截断结果。

    5. 退出div()函数,如果z == 0没有返回任何值,你可能会返回0,或者任何有意义的值,表明操作无法执行。

    我修复了你的代码,在这里

    float divide(int x, int z);
    
    int main()
    {
        int n, m;
        float quotient;
    
        printf("Enter first number: \n");
        scanf("%d", &n);
    
        printf("Enter second number: \n");
        scanf("%d", &m);
    
        if (m != 0)
        {
            quotient = divide(n,m);
            printf("%d/%d = %f \n", n, m, quotient);
        }
    }
    
    float divide(int x, int z)
    {
        float result;
    
        if (z != 0) {
            result = (float)x / (float)z;
            return result;
        }
        return 0;
    }
    

    你应该给你的变量起有意义的名字,这样可以提高可读性并完全避免这种问题。

    看看我如何将div() 函数重命名为更具表现力的名称divide(),以及其中的返回值“这不是严格需要”,我将其命名为result 以反映计算的结果将进入其中,并且在main() 中将divide() 的返回值称为qoutient 也让读者在没有看到您分配给它的行的情况下预期它可能来自哪里。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-13
      • 1970-01-01
      • 2013-08-03
      • 2016-07-03
      • 2013-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多