【问题标题】:confliction type for a user defined function in cc中用户定义函数的冲突类型
【发布时间】:2016-02-16 15:05:18
【问题描述】:

我在 c 中工作了很长时间。在这里我必须实现三个功能,其中包括

  1. 得到一个数字并显示一半

2.得到数字的平方

3.得到两个数并显示它们的和和减法。

我正在使用 devC++,当我编译代码时,我得到了我在标题中提到的错误 conflict type if squareInput。这里有什么问题:

#include<stdio.h>
#include<conio.h>
int main(){

    float x;
    printf("enter a number\n");
    scanf("%f",&x);

    //TASK 1 : display half of the number 
    pirntf("half of x is = %.3f",x);

    //TASK 2 : square of number 

    squareInput(x); //call square function from here

    // TASK 3 : get two numbers and display both summation and sabtraction

    float num1,num2;   // declare two floating number( floating numbers can hold decimal point numbers
    printf("enter num1 \n");
    scanf("num1 is =%f",&num1);
    printf("enter num2 \n");
    scanf("num2 is =%f",num2);
    calculate(num1,num2);// call calculate function

    getch();
}

float squareInput(float input){

    float square=input*input;
    printf("\n square of the number is %.3f \n",square);
    return 0;
}

float calculate(float num1,float num2){

    //summation
    float summation= num1+num2; // declare antoher variable called summation to hold the sum 
    //sabtraction
    float sabtraction=num1-num2;

    printf("summation is %.2f \n",summation);
    printf("sabtraction is %.2f \n",sabtraction);

    return 0;
}

【问题讨论】:

  • 由于代码是用C++编译的,请去掉c标签。
  • 头文件conio.h 不可移植。建议用getchar() 替换对getch() 的调用并删除#include &lt;conio.h&gt; 语句

标签: c dev-c++


【解决方案1】:

没有原型就会出错。添加

float squareInput(float input);
float calculate(float num1,float num2);

int main()前面。

如果您在调用之前未声明函数,编译器会将其假定为返回 int 的函数。但是,squareInput() 返回浮点数,所以编译器(或链接器,也许)会向您抱怨。

还要注意定义是声明(但显然反之不行),因此将 squareInput()calculate() 的定义移到它们被调用的位置之前也可以。

【讨论】:

  • 为什么?,,,,,,,,,,,,,,,,,,,,,,
  • @AL-zami,因为这就是 c 中定义符号查找的方式。
  • @AL-zami,编译器对你的伤害比
  • @AL-zami:因为如果您使用的是 C99 或 C11,则不允许在没有声明的范围内使用函数,并且如果您落后于 C89/C90 的时代, 那么未声明的函数将被假定返回int(当函数被调用时你的float值将被转换为double),但这不是正确的返回类型。
  • @AL-zami 另外,请注意,如果您在定义 main 之前定义函数,则不需要原型。
【解决方案2】:

在您调用squareInputcalculate 时,它们尚未定义。所以 C 假定隐式声明 int squareInput()int calculate()。这些隐式声明与这些函数的定义冲突。

您可以通过在main 之前为每个函数添加声明来解决此问题:

float squareInput(float input);
float calculate(float num1,float num2);

或者通过简单地将函数整体移动到main之前。

【讨论】:

    【解决方案3】:

    请务必在使用函数时添加原型。这样您就不必太担心调用它们的顺序。

    如果可以的话,也试着把你的问题分成更小的部分。像 TAKS1 这样的注释表明您实际上想要一个具有该名称的函数。

    #include <stdio.h>
    
    //prototypes
    void AskUserForOneNumer(float * number, const char * question );
    void TASK_1(float x);
    void TASK_2(float x);
    void TASK_3(float a, float b);
    
    int main()
    {
        float x, a, b;
        AskUserForOneNumer(&x, "enter x");
        AskUserForOneNumer(&a, "enter a");
        AskUserForOneNumer(&b, "enter b");
        TASK_1(x);
        TASK_2(x);
        TASK_3(a, b);
    }
    
    void TASK_1(float x)
    {
        printf("x = %g\n", x);
        printf("0.5 * x = %g\n", 0.5 * x);
    }
    
    void TASK_2(float x)
    {
        printf("x = %g\n", x);
        printf("x * x = %g\n", x * x);
    }
    
    void TASK_3(float a, float b)
    {
        printf("a = %g\n", a);
        printf("b = %g\n", b);
        printf("a + b = %g\n", a + b);
        printf("a - b = %g\n", a - b);
    }
    
    void AskUserForOneNumer(float * number, const char * question)
    {
        float x;
        printf("%s\n", question);
        scanf("%f", &x);
        printf("your input was %g\n", x);
        *number = x;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 1970-01-01
      • 2013-08-08
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多