原型的重要性 [英] The importance of prototypes

查看:111
本文介绍了原型的重要性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,C中的原型纯粹是可选的。至于我的

体验,直到今天我猜,情况一直如此(除了

消除编译器警告信息)。


但是,今天我编写了一个例程来搜索一个数组,其值大于某个限制值(双倍),并且发生了一些奇怪的事情。

在调试时我注意到了我将限制值传递给例程

但是在函数内部这个值是垃圾。这对于我来说是非常奇怪的,而这个双倍的价值就像10 ^ -315那么它

看起来它可能是一个整数值我认为是一个整数值双重。

想想在

场景背后有某种类型的投射,我添加了原型并且问题消失了。

所以,我错了,原型是强制性的,有点像C ++?

编译器会自动假设未知函数只有
整数参数,因此为我输入一个类型?这不会是
影响指针或int,long,chars但只有浮动和双打

有点像我所看到的。


我正在使用gcc 3.3.4如果有所作为。


谢谢,

Zach

It is my understanding that prototypes in C are purely optional. To my
experience, until today I guess, this has been the case (other than
eliminating compiler warning messages).

However, today I wrote a routine to search an array for values greater
than a certain limiting value (double) and something weird happened.
When debuging I noticed that I passed the limiting value to the routine
yet inside the function this value was garbage. This was very odd to
me and the value was something like 10^-315 for this double so it
looked like it was maybe an integer value which I thought was a double.
Thinking that there was some kind of type casting going on behind the
scenes, I added the prototype and the problem went away.

So, am I wrong and prototypes are mandatory, kinda like in C++? Does
the compiler automatically assume that unknown functions have only
integer arguments and thus puts in a type cast for me? This wouldn''t
affect pointers or int, long, chars but only floats and doubles which
is kinda what I am seeing.

I am using gcc 3.3.4 if that makes a difference.

Thanks,
Zach

推荐答案

elzacho说:
elzacho said:
我的理解是C中的原型纯粹是可选的。
It is my understanding that prototypes in C are purely optional.




如果你没有为函数提供原型,那么标准要求编译器使用标准来假设函数返回一个int,并且不能

对传递给函数的参数进行类型检查。此外,如果

你调用一个可变函数,该函数在范围内没有有效的函数原型

,那么程序的行为是不确定的。

因此,忽略原型有时是可选的这一事实,并且总是提供正确的原型(通过#include包含它的b / b的标题或者通过自己黑客入侵)。通过永远不会省略

合适的原型,你可以确定你不会因为省略适当的原型而遭受

问题!


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 1999年7月29日
http://www.cpax.org.uk

电子邮件rjh在上述域名



If you don''t provide a prototype for a function, the compiler is required by
the Standard to assume that the function returns an int, and is unable to
do type checking on the arguments you pass to the function. Furthermore, if
you call a variadic function that does not have a valid function prototype
in scope, the behaviour of the program is undefined.

So ignore the fact that prototypes are sometimes optional, and always
provide the correct prototype (either by #include-ing the header that
contains it or by hacking it in yourself). By never omitting the
appropriate prototype, you can be sure that you will not suffer the
problems that can be caused by omitting the appropriate prototype!

--
Richard Heathfield
"Usenet is a strange place" - dmr 29 July 1999
http://www.cpax.org.uk
Email rjh at the above domain


Richard Heathfield写道:
Richard Heathfield wrote:
elzacho说:
elzacho said:
我的理解是C中的原型纯粹是可选的。
如果你没有为函数提供原型,那么编译器是必需的。标准假设函数返回一个int,
It is my understanding that prototypes in C are purely optional.
If you don''t provide a prototype for a function, the compiler is required
by the Standard to assume that the function returns an int,



如果提供了一个声明(没有原型),例如...


double sin(); / *非原型声明* /


double f(double x){return sin(x); }


[可悲的是,恕我直言,] C99仍然允许这样的代码。

并且无法对传递给函数的参数进行类型检查。
此外,如果你调用一个在范围内没有有效
函数原型的可变参数函数,程序的行为是不确定的。

所以忽略原型是这样的事实有时是可选的,并且总是提供正确的原型(通过#include包含它的标题或者通过自己进行黑客攻击)。


对前者有压力。 :)

永远不要省略相应的原型,你可以确定你不会遇到因省略相应原型而导致的问题!



Not if a declaration (sans prototype) is supplied, e.g. ...

double sin(); /* non-prototype declaration */

double f(double x) { return sin(x); }

[Sadly, IMHO,] C99 still allows code like this.
and is unable to do type checking on the arguments you pass to the function.
Furthermore, if you call a variadic function that does not have a valid
function prototype in scope, the behaviour of the program is undefined.

So ignore the fact that prototypes are sometimes optional, and always
provide the correct prototype (either by #include-ing the header that
contains it or by hacking it in yourself).
With stress on the former. :)
By never omitting the
appropriate prototype, you can be sure that you will not suffer the
problems that can be caused by omitting the appropriate prototype!




如果有任何ANSI / ISO C90编译器没有b $ b可以选择启用''所需的原型',我会感到惊讶'。


-

彼得



I''d be surprised if there was any ANSI/ISO C90 compiler that doesn''t
have an option to enable ''required prototypes''.

--
Peter


Richard Heathfield< in * ****@invalid.invalid>写道:
Richard Heathfield <in*****@invalid.invalid> writes:
elzacho说:
elzacho said:
我的理解是C中的原型纯粹是可选的。
如果你不提供原型对于一个函数,标准要求编译器假定函数返回一个int,并且无法对传递给函数的参数进行类型检查。此外,如果你在范围内调用一个没有有效函数原型的可变函数,那么程序的行为是不确定的。
It is my understanding that prototypes in C are purely optional.
If you don''t provide a prototype for a function, the compiler is required by
the Standard to assume that the function returns an int, and is unable to
do type checking on the arguments you pass to the function. Furthermore, if
you call a variadic function that does not have a valid function prototype
in scope, the behaviour of the program is undefined.




一个额外的细节:如果你提供的声明不是
原型,你可以指定返回类型而不是参数类型:


double func();

因此,忽略原型有时是可选的这一事实,并始终提供正确的原型(通过#include包含它的标题或通过在你自己黑客攻击)。通过永远不会省略相应的原型,您可以确保您不会遇到因省略相应原型而导致的问题!



One additional detail: If you provide a declaration that isn''t a
prototype, you can specify the return type but not the argument types:

double func();
So ignore the fact that prototypes are sometimes optional, and always
provide the correct prototype (either by #include-ing the header that
contains it or by hacking it in yourself). By never omitting the
appropriate prototype, you can be sure that you will not suffer the
problems that can be caused by omitting the appropriate prototype!




同意。没有理由*不*为所有

函数提供原型。


(C99有更严格的规则,C90。我不记得细节了,但是

你应该遵循更严格的规则,即使你使用的是C90

编译器。)


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http:// www。 ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



Agreed. There is no reason *not* to provide prototypes for all
functions.

(C99 has stricter rules that C90. I don''t remember the details, but
you should follow the stricter rules even if you''re using a C90
compiler.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于原型的重要性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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