sizeof()O / P. [英] sizeof() O/P

查看:79
本文介绍了sizeof()O / P.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码的输出感到惊讶。用turbo编译C


void main()

{

printf("%d",sizeof(printf() );

}

输出结果是:2

输出结果如何为2?实际上sizeof()的属性是什么? />
和printf()?这两个函数有没有返回类型?希望

的积极回复...非常感谢提前。

i''m surprised at the output of the following code. compiled in turbo C

void main()
{
printf("%d",sizeof(printf());
}
the output was : 2
how come the output is 2? actually what is the property of sizeof()
and printf()?are there any return types for the two functions? hoping
for the positive responses.. thanks a lot in advance.

推荐答案

raghu说:
raghu said:
我对以下代码的输出感到惊讶。编译在turbo C

void main()


不要。任何为main()展示错误返回类型的程序

未定义的行为,所以任何行为,无论是否令人惊讶,都是允许的,因为与C语言有关。

{
printf("%d" ,sizeof(printf());
i''m surprised at the output of the following code. compiled in turbo C

void main()
Don''t be. Any program which gives the wrong return type for main() exhibits
undefined behaviour, so any behaviour, surprising or not, is permissible as
far as the C language is concerned.
{
printf("%d",sizeof(printf());




在范围内调用没有正确原型的printf调用undefined

行为。在没有任何参数的情况下调用printf会调用undefined

行为。


像这样开始你的程序:


#include< stdio.h>


int main(无效)


....然后再次尝试你的程序,先修复任何诊断由

你的编译器发出。

-

Richard Heathfield

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

电子邮件:rjh在上面的域名(但显然放弃了www)



Calling printf without a correct prototype in scope invokes undefined
behaviour. Calling printf without any arguments invokes undefined
behaviour.

Start your program like this:

#include <stdio.h>

int main(void)

....and then try your program again, first fixing any diagnostics issued by
your compiler.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)


在文章< 11 **** ******************@f14g2000cwb.googlegroups .com> ;,

raghu< ra ********* @ gmail。 COM>写道
In article <11**********************@f14g2000cwb.googlegroups .com>,
raghu <ra*********@gmail.com> writes
我对以下代码的输出感到惊讶。编译成涡轮增压C

void main()
{
printf("%d",sizeof(printf());
}
输出是:2
为什么输出是2?实际上sizeof()
和printf()的属性是什么?这两个函数是否有任何返回类型?希望
为积极的回应..非常感谢。
i''m surprised at the output of the following code. compiled in turbo C

void main()
{
printf("%d",sizeof(printf());
}
the output was : 2
how come the output is 2? actually what is the property of sizeof()
and printf()?are there any return types for the two functions? hoping
for the positive responses.. thanks a lot in advance.




忽略它应该是


#include< stdio。 h>

int main(无效)

{

printf("%d",sizeof(printf());

返回0;

}


谁设置这个家庭工作?

-

\ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \

\ / \ / \ / \ / \ Chris Hills Staffs England / \ / \ / \ / \ /

/ \ / \ / ch * ** @ phaedsys.org www.phaedsys.org \ / \ / \

\ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \ / \\ \\ /



Ignoring that it should have been

#include <stdio.h>
int main(void)
{
printf("%d",sizeof(printf());
return 0;
}

Who set this home work?
--
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
\/\/\/\/\ Chris Hills Staffs England /\/\/\/\/
/\/\/ ch***@phaedsys.org www.phaedsys.org \/\/\
\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/


raghu写道:
我对以下代码的输出感到惊讶。编译成涡轮增压C

void main()
{
printf("%d",sizeof(printf());
}
输出是:2
为什么输出是2?实际上sizeof()
和printf()的属性是什么?这两个函数是否有任何返回类型?希望
为积极的回应..非常感谢提前。
i''m surprised at the output of the following code. compiled in turbo C

void main()
{
printf("%d",sizeof(printf());
}
the output was : 2
how come the output is 2? actually what is the property of sizeof()
and printf()?are there any return types for the two functions? hoping
for the positive responses.. thanks a lot in advance.




无论是什么原因,都应该没有任何意外的惊喜

输出是否是如果代码让恶魔从你的鼻子中飞出来,你不应该感到惊讶

,因为它b / b
不是一次调用未定义的行为,不是两次,而是三次:


- main()函数不是'void''


- 调用类似printf()的可变函数是UB >
没有原型范围


- UB将非'''''传递给'%d'指示符

(在某些异国情调系统中,`size_t''较窄

比`int'这可能没问题,所以我真的应该说b b $ b表示行为是定义的还是未定义的

是实现定义的。)


....而且最重要的是,在输出流的末尾,换行符换行和b $ b的命运是实现定义的。


(UB的三个实例英国的一个,或者可能是两个英国的b $ b。和两个I.D.B. - 所有只有四行,两个

,仅包括大括号。这种错误的密度是多少 -

密度的密度,有人可能会说 - 这是一张

吉尼斯书的候选人?可悲的是,我不担心。)


这是一个清理版本:


#include< stdio.h> / * for printf()prototype * /

int main(void){

printf("%d \ n",/ * note newline * /

(int)sizeof printf()); / *注意类型强制* /

返回0; / *在C90中是必需的,在C99中是可选的* /

}


现在,您可能仍然对
$ b的输出感到困惑$ b清理你的bletcherous代码版本。如果你思考两个问题,那么很多就会变得更清楚:


- `sizeof''运算符对它的论点做了什么?


- printf()函数返回什么类型?


回答这些,Grasshopper,你将在路径上

到启示 - 但是如果你继续编写你在这里提供的

样本代码,那就意味着你走在正确的道路上

但走错了方向。


-

Eric Sosman
es ***** @ acm-dot-org.inva 盖子


这篇关于sizeof()O / P.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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