为什么这样"功能'X'&QUOT ;?隐式声明 [英] Why this "Implicit declaration of function 'X'"?

查看:151
本文介绍了为什么这样"功能'X'&QUOT ;?隐式声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个简单的程序来找到的总和,平均,最大的和最小的3个数字的号码。
它允许用户输入三(整数)的数字,并返回的总和,平均值,最大值和最小值。
它没有错误,但警告。下面是我的源$ C ​​$ C:

I wrote a simple program to find the Sum, average, biggest and smallest number of 3 numbers. It lets the user to input three (integer) numbers and return the sum, average, max and min. It has no errors but a warning. Here is my source code:

main.c中:

#include <stdio.h>

int main()
{
    int num1, num2, num3, sum, max, min, avg;

    printf("Enter Three \"Integer\" Numbers:");

    scanf("%i%i%i", &num1, &num2, &num3);

    sum = summation(&num1, &num2, &num3);
    avg = average(&sum);
    max = max_val(&num1, &num2, &num3);
    min = min_val(&num1, &num2, &num3);

    printf("Sum: %i Avg: %i MAX: %i MIN: %i", sum, avg, max, min);

    return 0;
}

int summation(int *n1, int *n2, int *n3)
{
    int s;
    s = *n1 + *n2 + *n3;

    return s;
}

int average(int *s)
{
    int a;
    a = *s / 3;

    return a;
}

int max_val(int *n1, int *n2, int *n3)
{
    int MAX;

    if (*n1 > *n2) MAX = *n1;
    else if (*n2 > *n3) MAX = *n2;
    else MAX = *n3;

    return MAX;
}

int min_val(int *n1, int *n2, int *n3)
{
    int MIN;

    if (*n1 < *n2) MIN = *n1;
    else if (*n2 < *n3) MIN = *n2;
    else MIN = *n3;

    return MIN;
}

我觉得没有必要让一个头文件,因为所有的功能都在类型为INT。

I think there is no need to make a header file because all functions are in type of "int".

当我编译这个

gcc main.c -o test

报告说,

main.c: In function 'main':
main.c:34:5: warning: implicit declaration of function 'summation' [-Wimplicit-function-declaration]

为什么这个警告?我无法找到该声明的任何错误。
那是什么?

Why this warning? I can't find any wrong in that declaration. What's that?

推荐答案

总和和您的其它功能定义的他们已经习惯后, ,所以编译器做了一个关于它的签名猜测;换句话说,一个的 的已假设隐含的声明。

summation and your other functions are defined after they're used in main, and so the compiler has made a guess about it's signature; in other words, an implicit declaration has been assumed.

它的使用,摆脱了警告之前,您应该声明函数。在C99规范,这是一个错误。

You should declare the function before it's used and get rid of the warning. In the C99 specification, this is an error.

无论是之前移动功能机构的 ,或包括方法签名之前 ,例如:

Either move the function bodies before main, or include method signatures before main, e.g.:

#include <stdio.h>

int summation(int *, int *, int *);

int main()
{
    // ...

这篇关于为什么这样&QUOT;功能'X'&QUOT ;?隐式声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆