简体   繁体   English

关于 function 类型冲突的问题

[英]Question about conflicting types for a function

I'm a student of C and I've got a problem with types of my variables in my function.我是 C 的学生,我的 function 中的变量类型有问题。 It's a function to calculate the capital in addition with interest rate in a certain span and it says that there is a conflicting type for "capital_a_terme".这是一个 function 来计算资本加上一定范围内的利率,它说“capital_a_terme”的类型存在冲突。

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

int main()
{
    float capital_initial, taux_interet_fixe, nb_annee_placement;
    printf("Saisir le capital initial.\n");
    scanf("%f", &capital_initial);
    printf("Saisir le taux d'interet fixe.\n");
    scanf("%f", &taux_interet_fixe);
    printf("Saisir le nombre d'annee de placement.\n");
    scanf("%f", &nb_annee_placement);
    printf("le capital a terme vaut : %f.\n", capital_a_terme(capital_initial, taux_interet_fixe, nb_annee_placement));
}

float capital_a_terme(float capital_initial, float taux_interet_fixe, float nb_annee_placement)
{
    if (nb_annee_placement == 0)
    {
        return capital_initial;
    }
    else
    {
        return (capital_a_terme(capital_initial + capital_initial * taux_interet_fixe / 100, taux_interet_fixe, nb_annee_placement - 1));
    }
}

If the compiler doesn't know a function signature it defaults its return type to int .如果编译器不知道function 签名,则默认其返回类型为int

As capital_a_terme is being used before the compiler knows what its signature is, it does exactly that, but when it reaches the line where the function is used it notices that the type does not match with what it thought it was, so the compilation fails and the error is issued.由于在编译器知道它的签名是什么之前就使用了capital_a_terme ,它确实这样做了,但是当它到达使用 function 的行时,它注意到类型与它认为的不匹配,所以编译失败并且发出错误。

You need to either place a prototype of the function before you use it, in this case, before main :您需要在使用之前放置 function 的原型,在这种情况下,在main之前:

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

// prototype here, before it's used
float capital_a_terme(float capital_initial, float taux_interet_fixe, float nb_annee_placement);

int main()
{
    float capital_initial, taux_interet_fixe, nb_annee_placement;
    printf("Saisir le capital initial.\n");
    //...

Or move the implementation up, to before you use it, again before main :或者将实现向上移动到使用它之前,再次在main之前:

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

// Or the whole function here, before it's used
float capital_a_terme(float capital_initial, float taux_interet_fixe, float nb_annee_placement)
{
    if (nb_annee_placement == 0)
    {
        return capital_initial;
    }
    //... 
}

int main()
{
    float capital_initial, taux_interet_fixe, nb_annee_placement;
    printf("Saisir le capital initial.\n");
    //...

you need to declare this function (above main), so main will recognize it.您需要声明这个 function(在 main 上方),所以 main 会识别它。

on the other side you can define this function above main and then it will be recognized by main.另一方面,您可以在 main 上方定义此 function,然后它将被 main 识别。

there's a difference between declaration and definition of a function: function 的声明和定义之间存在差异:

every function needs declaration so other part of the program will recognize it.每个 function 都需要声明,因此程序的其他部分会识别它。

also, you can declare a function without defining it and you will able to compile it and run it.此外,您可以声明 function 而不定义它,您将能够编译并运行它。

but, you can not define function without declare it.但是,您不能在没有声明的情况下定义 function。

if you define function above the part of the program that actually use it, the definition is also it's declaration.如果你在实际使用它的程序部分上面定义了function,那么定义也是它的声明。

hope you got it.希望你明白了。

2 things: 2件事:

  1. space out the return statement:将 return 语句隔开:
return (capital_a_terme(capital_initial + capital_initial * taux_interet_fixe / 100, taux_interet_fixe, nb_annee_placement-1));
  1. 100 needs to be a float: (this is the only way I could figure out how to do it based off of my very limited understanding of c, though you might be able to replace onehundred with 100f or %f100 , I don't really know tbh) 100需要是一个浮点数:(根据我对 c 的非常有限的理解,这是我能弄清楚如何做到这一点的唯一方法,尽管你可以用100f%f100替换onehundred ,我真的不知道tbh)
float onehundred = 100;
return (capital_a_terme(capital_initial + capital_initial * taux_interet_fixe / onehundred, taux_interet_fixe, nb_annee_placement-1));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM