在我的code,为什么没有一个函数声明的一个非问题的一个功能,而是抛出一个警告别人呢。 [英] In my code, why is lack of a function declaration a non-issue for one function, but throws a warning for another?

查看:125
本文介绍了在我的code,为什么没有一个函数声明的一个非问题的一个功能,而是抛出一个警告别人呢。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的程序,我用两个函数PRD()和显示器()。我已经在main()调用之前宣布他们既不超前的main(),而且,我已经后,主(定义)。然而,而珠三角()工程进展顺利的main(),调用显示()内显示警告的显示previous隐式声明在这里。什么是关于显示器(不同的),有它而不是警告其他功能可按珠三角()?我已经宣布他们既不开始with.Still有警告,由于一个调用,而另外一个工作正常。

In the following program,I use two functions prd() and display().I have declared neither of them ahead of main() before invoking them in main(),and I have defined both after main().Yet while prd() works smoothly inside main(),invoking display() shows the warning " previous implicit declaration of 'display' was here ".What is different about display() that there is a warning for it but not for the other funciton prd()?I have declared neither of them to begin with.Still there is warning due to invocation of one, but other one works fine.

    #include<stdio.h>

    int main()
    {
        int x=8,y=11;

        printf("The product of %d & %d is %d",x,y,prd(x,y));

        display();

        return 0;
    }

    int prd(int x,int y)
    {
        return x*y;
    }

    void display()
    {
        printf("\n Good Morning");
    }

PS:我真的AP preciate如果你能回答这个问题,二次 - 是函数声明没有必要用C在所有只要是它的一个定义是什么?我已经宣布所有的习惯。在main()函数之前的程序功能,然后在main()function.Am后,他们确定我错了吗?

PS: And I would really appreciate if you can answer this secondary question --"Is function declaration not necessary at all in C provided there is a definition for it?".I have the habit of declaring all functions of the program before the main() function, and then defining them after the main() function.Am I wrong?

推荐答案

当您使用未声明的显示()编译器隐式声明它,就好像它是返回 INT

When you use undeclared display() the compiler implicitly declares it as if it were returning int.

在编译器终于明白)的高清显示屏(,它看到的返回类型为无效,但它已经假定它是 INT 等的定义和隐含的声明有所不同,因此错误/警告。

When the compiler finally sees the definition of display(), it sees that the return type is void, but it's already assumed it be int and so the definition and the implicit declaration differ, hence the error/warning.

这篇关于在我的code,为什么没有一个函数声明的一个非问题的一个功能,而是抛出一个警告别人呢。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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