关于printf的问题 [英] question about printf

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

问题描述

我正在使用turbo c;

我输入的代码如下

#include< stdio.h>

main ()

{

int a = 5;

printf("%d%d%d",a ++,++ a, a);

}

上面代码的输出非常有趣。

任何人都可以帮助我输出上面的代码

请告诉我相同的原因

感谢你

提前

解决方案

pratik写道:

我正在使用turbo c;
我输入的代码如下
#include< stdio.h>
main()
{a / 5;
printf("%d%d%d",a ++,++ a,a);
} 任何人都可以帮我输出上面的代码




1234你好42666


是一种可能的输出


阅读常见问题解答!它就在那里。


-

托马斯。


pratik写道:

我正在使用turbo c;
我输入的代码如下
#include< stdio.h>
main()
{
int a = 5;
printf("%d%d%d",a ++,++ a,a);
}
上述代码的输出很有意思。
任何人都可以帮我解决上面代码的输出
请告诉我相同的原因




虽然我不知道为什么你问一个关于某事的问题

你不提供:输出,我可以告诉你原因

你的惊喜。


在C中,评估函数参数的顺序

未指定。所以你可能期望看到577但是

也可能是665.这里有一个问题:有多少

可能的结果(在不同的实现上

我的意思是)?


案例




pratik < PR ************** @ yahoo.com>在留言新闻中写道:c1 ************************** @ posting.google.c om ...

我正在使用turbo c;
我输入的代码如下
#include< stdio.h>
main()
{
int a = 5 ;
printf("%d%d%d",a ++,++ a,a);
}
上面代码的输出非常有趣。
可以有人帮我提供上面代码的输出
请告诉我相同的原因
感谢你
提前




这个程序可以说是不可移植的。原因是参数评估的订单没有具体说明。上面的程序,在编译时,

给出以下警告信息。


F:\ Vijay \ C> gcc printf_params.c -Wall

printf_params.c:函数`main'':

printf_params.c:5:警告:对'a''的操作可能是未定义的

printf_params.c:5:警告:a上的操作可能未定义

printf_params.c:5:警告:对a的操作可能未定义


考虑一个例子,


(* fptr)((i = 2),k);


这里,首先评估哪个表达式 - 即fptr,(i = 2)或k -

未被标准指定。同样地,哪个参数会将
推到堆栈上 - 如果这是_the_参数传递方法 - 也是

未指定。但是,保证在调用函数之前所有副作用都需要




您的答案特别适用于您使用的编译器,但是,通常

从右到左传递参数似乎很常见。


有很多方法可以将参数传递给函数。这个
CPU(对不起,不知道任何特定的名称)可能有单独的

寄存器用于参数传递;编译器甚至不使用堆栈!对于

程序的最大可移植性,不应做出任何假设。考虑

另一个例子:


void

mango(注册int a,int b)

{

/ * .. * /

}


int

main(void)

{

int k,j;

/ * .. * /

芒果(k,j);

返回0;

}


假设编译器完成了使用
$寄存器的请求b $ b第一个参数(如果只有一个寄存器可用怎么办?)。

第二个参数怎么样?


-

Vijay Kumar R Zanvar

我的家页面 - http://www.geocities.com/vijoeyz/


i am working a turbo c;
the code i typed in is as follows
#include<stdio.h>
main()
{
int a=5;
printf("%d%d%d",a++,++a,a);
}
The output of the above code is very interesting.
can anyone help me with the output of the above code
Please tell me the reason for the same
Thanking you
in advance

解决方案

pratik wrote:

i am working a turbo c;
the code i typed in is as follows
#include<stdio.h>
main()
{
int a=5;
printf("%d%d%d",a++,++a,a);
}
The output of the above code is very interesting.
can anyone help me with the output of the above code



1234 hello 42666

is one possible output

Read the FAQ! It is all there.

--
Thomas.


pratik wrote:

i am working a turbo c;
the code i typed in is as follows
#include<stdio.h>
main()
{
int a=5;
printf("%d%d%d",a++,++a,a);
}
The output of the above code is very interesting.
can anyone help me with the output of the above code
Please tell me the reason for the same



Although I wonder why you ask a question about something
you do not supply: the output, I can tell you the reason
of your surprize.

In C the order in which function arguments are evaluated
is not specified. So you might expect to see 577 but it
might as well be 665. Here''s a question for you: How many
possible results are there (on different implementations
I mean)?

Case



"pratik" <pr**************@yahoo.com> wrote in message news:c1**************************@posting.google.c om...

i am working a turbo c;
the code i typed in is as follows
#include<stdio.h>
main()
{
int a=5;
printf("%d%d%d",a++,++a,a);
}
The output of the above code is very interesting.
can anyone help me with the output of the above code
Please tell me the reason for the same
Thanking you
in advance



This program can said to be unportable. The reason is that the order
of evaluation of arguments is unspecified. The above program, upon compilation,
gives the following warning messages.

F:\Vijay\C> gcc printf_params.c -Wall
printf_params.c: In function `main'':
printf_params.c:5: warning: operation on `a'' may be undefined
printf_params.c:5: warning: operation on `a'' may be undefined
printf_params.c:5: warning: operation on `a'' may be undefined

Consider an example,

(*fptr)( (i=2), k );

Here, which expression -- i.e., "fptr", (i=2), or k -- would be evaluated first
is unspecified by the standard. And in the same way, which argument will pushed
onto the stack -- if that is _the_ method of parameter passing -- is also
unspecified. However, it is guaranteed that all the side effects would take
place before the function is called.

Your answer is particular to the compiler you are using, though, generally
passing parameters from right to left seems to be common.

There are many ways in which parameters can be passed to a function. This
again depends on the underlying CPU architecture. Many heavy-performance based
CPUs (sorry, don''t know any particulare name) may have separate sets of
registers for parameter passing; the compiler doesn''t even use stack! For
maximum portability of programs, no assumptions should be made. Consider
another example:

void
mango ( register int a, int b )
{
/* .. */
}

int
main ( void )
{
int k, j;
/* .. */
mango ( k, j );
return 0;
}

Assume that the compiler fulfills the request to use a register for the
first parameter (what if only one register is available?). How about the
second parameter?

--
Vijay Kumar R Zanvar
My Home Page - http://www.geocities.com/vijoeyz/


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

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