Printf问题 [英] Printf question

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

问题描述

嗨CLCers,

下面的程序会打印一些数字而我不会理解为什么它的行为是这样的。我没有提供

printf函数的必要参数和

根据我的理解程序应该抛出

一些错误。请清除我的疑惑。


#include< stdio.h>

int main()

{

printf("%d");

返回0;

}


提前致谢。


Sha

解决方案

一个******* @ coolgroups.com 潦草地写着以下内容:

嗨CLCers,
下面的程序打印一些数字,我不打算>明白为什么它表现得像这样。我没有提供
printf函数的必要参数和
根据我的理解程序应该抛出一些错误。请清除我的疑虑。
#include< stdio.h>
int main()
{
printf("%d");
返回0;
}




您正在创建未定义的行为。 printf()期望一个参数,

,因为格式字符串,但你没有提供。这通过

本身会产生未定义的行为,因此任何事情都可能发生,

包括打印随机数。最有可能的是它从堆栈顶部读取了一个

的值,但无论价值是什么,都是实现定义的



-

/ - Joona Palaste(pa*****@cc.helsinki.fi)-------------芬兰--- ----- \

\ - http: //www.helsinki.fi/~palaste ---------------------规则! -------- /

玫瑰是红色的,紫罗兰是蓝色的,我是schitzophrenic,我也是。

- Bob Wiley


Joona I Palaste写道:


一个******* @ coolgroups.com 潦草地写着以下内容:

嗨CLCers,
下面的程序打印一些数字,我不打算>明白为什么它表现得像这样。我没有提供
printf函数的必要参数和
根据我的理解程序应该抛出一些错误。请清除我的怀疑。


#include< stdio.h>
int main()
{/> printf("%) d");
返回0;
}



您正在创建未定义的行为。 printf()期望一个参数,
因为格式字符串,但你没有提供。这个由
本身产生未定义的行为,因此任何事情都可能发生,包括打印随机数。最有可能的是它从堆栈顶部读取值,但无论价值是什么,都是实现定义的。




它是未定义的。

它不需要记录,也不需要重复。


-

pete


pete< pf ***** @ mindspring.com>潦草地写道:

Joona I Palaste写道:

*** ****@coolgroups.com 潦草地写道:

>嗨CLCers,
>下面的程序打印一些数字,我不打算
>明白为什么它表现得像这样。我不提供
> printf函数的必要参数和
>根据我的理解,程序应该抛出
>一些错误。请清除我的怀疑。

> #include< stdio.h>
> int main()
> {
> printf("%d");
>返回0;
> }



您正在创建未定义的行为。 printf()期望一个参数,
因为格式字符串,但你没有提供。这个由
本身产生未定义的行为,因此任何事情都可能发生,包括打印随机数。最有可能它是从堆栈顶部读取值,但无论价值是什么,都是实现定义的。



这是未定义的。
它不需要记录,也不需要重复。




这就是为什么我说'你正在创造未定义的行为,那个'

也是我说最有可能的原因。而不是肯定。


-

/ - Joona Palaste(pa*****@cc.helsinki.fi) - -----------芬兰-------- \

\-- http://www.helsinki.fi/~palaste ---------------------规则! -------- /

"''我'是世界上最美丽的词。"

- John Nordberg


Hi CLCers,
The program below prints some number and i dont
understand why it is behaving like this. I am not supplying
the necessary arguments to the printf function and
according to my understanding the program shiould throw
some error. Please clear my doubts.

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

Thanks in advance.

Sha

解决方案

an*******@coolgroups.com scribbled the following:

Hi CLCers,
The program below prints some number and i dont
understand why it is behaving like this. I am not supplying
the necessary arguments to the printf function and
according to my understanding the program shiould throw
some error. Please clear my doubts. #include<stdio.h>
int main()
{
printf("%d");
return 0;
}



You are creating undefined behaviour. printf() expects an argument,
because of the format string, but you didn''t supply one. This, by
itself, creates undefined behaviour, so anything might happen,
including printing random numbers. Most probably it''s reading a
value from the top of the stack, but whatever value that is is
implementation-defined.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Roses are red, violets are blue, I''m a schitzophrenic and so am I."
- Bob Wiley


Joona I Palaste wrote:


an*******@coolgroups.com scribbled the following:

Hi CLCers,
The program below prints some number and i dont
understand why it is behaving like this. I am not supplying
the necessary arguments to the printf function and
according to my understanding the program shiould throw
some error. Please clear my doubts.


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



You are creating undefined behaviour. printf() expects an argument,
because of the format string, but you didn''t supply one. This, by
itself, creates undefined behaviour, so anything might happen,
including printing random numbers. Most probably it''s reading a
value from the top of the stack, but whatever value that is is
implementation-defined.



It''s undefined.
It need not be documented and it need not be repeatable.

--
pete


pete <pf*****@mindspring.com> scribbled the following:

Joona I Palaste wrote:

an*******@coolgroups.com scribbled the following:

> Hi CLCers,
> The program below prints some number and i dont
> understand why it is behaving like this. I am not supplying
> the necessary arguments to the printf function and
> according to my understanding the program shiould throw
> some error. Please clear my doubts.

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



You are creating undefined behaviour. printf() expects an argument,
because of the format string, but you didn''t supply one. This, by
itself, creates undefined behaviour, so anything might happen,
including printing random numbers. Most probably it''s reading a
value from the top of the stack, but whatever value that is is
implementation-defined.


It''s undefined.
It need not be documented and it need not be repeatable.



That''s why I said "you are creating undefined behaviour", and that''s
also why I said "most probably" instead of "certainly".

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"''I'' is the most beautiful word in the world."
- John Nordberg


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

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