MinGW的编译器不需要函数声明? [英] MinGW compiler doesn't need function declarations?

查看:194
本文介绍了MinGW的编译器不需要函数声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这两个文件:

// first.c
int main(void) {
  putint(3);
}

// second.c
#include <stdio.h>
void putint(int n) {
  printf("%d",n);
  getchar();
}

当我在Win XP下运行gcc 4.6.1:

When I run gcc 4.6.1 under Win XP:

gcc first.c second.c -o program.exe

它有没有问题,写3到stdout。它不需要 putint 声明中的 first.c 的。这怎么可能?这是标准的行为?

It has no problem and writes 3 to stdout. It doesn't need putint declaration in first.c. How is this possible? Is this standard behavior?

我已经在MSVC测试这2008例preSS,它只能与预期的声明运行。

I have tested this on MSVC 2008 Express and it runs only with the declaration as expected.

// first.c
void putint(int);
int main(void) {
  putint(3);
}


解决,感谢提示,这些选项有助于显示警告:


Solved, thanks for hints, these options helped to show the warning:


  • -Wimplicit

  • -std = C99(4.6的MinGW依旧采用的 gnu90 通过默认)

  • -Wimplicit
  • -std=c99 (MinGW 4.6 still uses gnu90 by default)

推荐答案

这是C,它不应该被以前作为几十年遗留下来的功能。您应该使用编译器的设置,如果你做这样的事情,我们会警告你。 GCC有几个开关使用时,与放大器,您应该指定;其中一人会给你一个警告这一点。

This is a legacy "feature" of C that should not be used as of several decades ago. You should use a compiler with settings that will warn you if you do something like this. Gcc has several switches that you should specify when using it & one of them will give you a warning for this.

编辑:我没有被使用gcc自己,但你应该检查开关-pedantic,-Wall,-Wextra和-std

I haven't been using gcc myself, but switches that you should check out are -pedantic, -Wall, -Wextra, and -std.

这是接受这是假设,每个旧的语言定义,因为你没有看到适合否则告诉它的编译器,函数)返回一个int值和b)因为你传递一个int(或如果你通过它的东西,可以被提升到一个int)函数期望参数是一个int。

The compiler that is accepting this is assuming, per the old language definition, that since you didn't see fit to tell it otherwise, the function a) returns an int value and b) since you pass it an int (or if you passed it something that could be promoted to an int) the function expects that argument to be an int.

由于@veer正确地指出,这应该一般工作在你的具体情况。在其他情况下,但是,对于一个函数没有原型的隐含假设和函数的实际签名之间的差异会使得事情热潮。

As @veer correctly points out, this should generally work in your particular case. In other cases, however, differences between the implicit assumptions for a function without a prototype and the function's actual signature would make things go boom.

这篇关于MinGW的编译器不需要函数声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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