函数原型与函数声明 [英] function prototype vs function declaration

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

问题描述

亲爱的C专家,


在准备C课程的内容时,我制作了部分功能

原型。

请你能否就其正确性发表意见。谢谢!


Q12:函数原型和函数有什么区别

声明?


如果你做对了,你必须对从K& R开始的C

编程进行相当多的研究。 C.事实证明,函数声明

和函数原型在标准C中是相同的。在K& RC中,
函数声明不需要具有类型它的参数是

指定。


因此声明如下:


int Spi_Tx();


完全可以接受,即使它的定义是实际的:


int Spi_Tx(uint8 TragetId,uint8 byteArray [],uint16 count)


{


}


程序员很容易用不正确的方式调用函数

参数不用说具有灾难性的运行后果。因此

ANSI C强制函数原型作为函数声明的方法。


问候,

Ravishankar

解决方案

Ravishankar S说:


< snip>


事实证明,函数

声明和函数原型在标准C中是相同的。



不,它不是。


确实所有的函数原型都是函数声明,但它并不是所有函数声明都是函数原型的.b / b不是真的。


< snip>


-

Richard Heathfield< http://www.cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日




" Richard Heathfield" < rj*@see.sig.invalidwrote in message

news:eN *************************** *** @ bt.com ...


Ravishankar S说:


< snip>


事实证明,函数

声明和函数原型在标准C中是相同的。



不,它没有。


确实所有的函数原型都是函数声明,但它不是所有函数都是
声明是函数原型。


< snip>



这里有一个例子可以帮助。


Ravishankar S写道:


>

" Richard Heathfield" < rj*@see.sig.invalidwrote in message

news:eN *************************** *** @ bt.com ...


> Ravishankar S说:

< snip>


事实证明,函数

声明和函数原型在标准中是一回事

C.

不,它没有。

确实所有函数原型都是函数声明,
但并非所有函数声明都是函数
原型。

< snip>



一个例子可以帮到这里。



int fx();


这是函数声明,但不是原型。事实上,对于像这样声明的函数,参数

check被转换了。这是为了向后兼容

,不推荐用于新代码。使用:


int fx(void);


或明确指定参数,作为原型。


Dear C Experts,

While prepating a content for a C course,I made section on function
prototypes.
Could you kindly provide me your comments on its correctness. Thank you !

Q12: What is the difference between a function prototype and a function
declaration?

If you get this right, you must have done fair amount of research on C
programming beginning with "K&R" C. It turns out that function declarations
and function prototypes are the same thing in standard C. In K&R C a
function declaration did not need to have the types of its arguments to be
specified.

Thus a declaration like:

int Spi_Tx();

was perfectly acceptable, even though its definition was infact:

int Spi_Tx(uint8 TragetId,uint8 byteArray[],uint16 count)

{

}

It was easy for programmers to call the function with incorrect
parameters which needless to say had disastrous runtime consequences. Thus
ANSI C mandates function prototypes as the method for function declaration.

Regards,
Ravishankar

解决方案

Ravishankar S said:

<snip>

It turns out that function
declarations and function prototypes are the same thing in standard C.

No, it doesn''t.

It is true that all function prototypes are function declarations, but it
is not true that all function declarations are function prototypes.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999



"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:eN******************************@bt.com...

Ravishankar S said:

<snip>

It turns out that function
declarations and function prototypes are the same thing in standard C.


No, it doesn''t.

It is true that all function prototypes are function declarations, but it
is not true that all function declarations are function prototypes.

<snip>

an example would help here.


Ravishankar S wrote:

>
"Richard Heathfield" <rj*@see.sig.invalidwrote in message
news:eN******************************@bt.com...

>Ravishankar S said:

<snip>

It turns out that function
declarations and function prototypes are the same thing in standard
C.


No, it doesn''t.

It is true that all function prototypes are function declarations,
but it is not true that all function declarations are function
prototypes.

<snip>


an example would help here.

int fx();

This is a function declaration, but not a prototype. In fact, argument
checking is turned of for functions declared like this. This is for
backwards compatibility and not recommended for new code. Use:

int fx(void);

or specify the parameters explicitly, as a prototype.


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

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