为什么编译器说变量 "z" 被重新声明为不同的变量类型?

Why does the compiler say that the variable "z" is redeclared as a different variable type?

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

注意根据编译器的问题是 "z not "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;
  }

}

这是问题所在:

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 语句。当控制到达非空函数的末尾时,这将避免未定义的行为。

另请注意,避免使用相同的标识符命名变量和函数是个好主意,因此最好在 main.[=17= 中重命名 div 变量]

你做错了几件事:

(根据 BLUEPIXY 的评论,您还应该避免使用名称 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.

中声明了与函数参数同名的变量

不过问题比较多,按出现的先后顺序一一列举

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

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

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

  4. 您将整数相除,结果将被截断,例如 z > x0

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

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

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(),以及其中的 return 值 "which is not strictly needed",我将它命名为result,以反映计算结果将放在其中,而在main()中,divide()的return值称为qoutient 也让 reader 期望它可能来自哪里,而不会看到您分配给它的行。