我们可以在定义函数之前先调用函数吗? [英] Can we call functions before defining it?

查看:149
本文介绍了我们可以在定义函数之前先调用函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>

void main()

{

    m();

}

void m()

{

    printf("hi");

}

输出

hi

警告

main.c:11:10: warning: conflicting types for 'm' [enabled by default]
     void m()
          ^
main.c:7:9: note: previous implicit declaration of 'm' was here
         m();
         ^

为什么即使在定义m()之前调用了该程序,该程序仍能成功运行? 警告的含义是什么?

Why this program runs successfully even though m() is called before it is defined? And what is the meaning of the warning?

推荐答案

C89通过将函数的返回类型和传递给它的参数隐式转换为int来允许此操作.请参见此处.

C89 allow this by implicitly converting the return type of function and parameter passed to it to int. See here.

但是,这在C99及更高版本中无效.该标准已将其省略.您必须为函数声明原型或在main之前定义它.请在此处查看结果.在这种情况下,存在编译时错误.

But, this is not valid in C99 and later. This has been omitted from the standard. Either you have to declare a prototype for your function or define it before main. See the result here. There is a compile time error in this case.

这篇关于我们可以在定义函数之前先调用函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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