printf的奇怪行为。请参阅下面的代码,请告诉它为什么会这样。 [英] Strange behaviour of printf. See the code below and please tell why it behaves in this way.

查看:44
本文介绍了printf的奇怪行为。请参阅下面的代码,请告诉它为什么会这样。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include< stdio.h>

typedef struct test {

int i;

int j;

}测试;


main(){

test var;

var.i = 10;

var.j = 20;


printf(" i ==%di ==%d \ n",var,var.i);

返回1;

}


VC ++中的编译器,看到你得到的输出是i == 10 i == 20


任何人都可以解释为什么会发生这种情况?即使我给了var.i的

地址为什么j的值是打印的?

#include<stdio.h>
typedef struct test{
int i;
int j;
}test;

main(){
test var;
var.i=10;
var.j=20;

printf("i==%d i==%d \n",var,var.i);
return 1;
}

Compiler in VC++ and see that you get the output is i==10 i==20

Can any one explain Why this thing happens?even though i give the
address of var.i why the value of j is geting printed?

推荐答案

DeltaOne写道:
DeltaOne wrote:

#include< stdio.h>
typedef struct test {
int i;
int j;
} test;

main(){
test var;
var.i = 10 ;
var.j = 20;

printf(" i ==%di ==%d \ n",var,var.i);
返回1 ;
}

用VC ++编译,看看你的哟你得到的输出是i == 10 i == 20

任何人都可以解释为什么会发生这种事情?即使我给出了var.i
地址/>
你不是。

(int *)& var是var.i的地址。

& var.i是var.i的地址

为什么j的值打印?

#include<stdio.h>
typedef struct test{
int i;
int j;
}test;

main(){
test var;
var.i=10;
var.j=20;

printf("i==%d i==%d \n",var,var.i);
return 1;
}

Compiler in VC++ and see that you get the output is i==10 i==20

Can any one explain Why this thing happens?even though i give the
address of var.i
You''re not.
(int *)&var is the address of var.i.
&var.i is the address of var.i.
why the value of j is geting printed?




即使你给它的地址是var。 i,

%d用于int类型参数,而不是指针,

因此代码的含义未定义

并且任何事情都可能发生。


-

pete



Even if you were giving it the address of var.i,
%d is for int type arguments, not pointers,
so the meaning of your code is undefined
and anything can happen.

--
pete


你好


DeltaOne写道:
Hello

DeltaOne wrote:
#include< stdio.h>
typedef struct test {
int i;
int j;
}测试;

main(){
test var;
var.i = 10;
var.j = 20;

printf (" i ==%di ==%d \ n",var,var.i);
返回1;
}

VC ++编译器看到你得到的输出是我== 10 i == 20


VC ++应该拒绝这段代码,因为它不是有效的C ++。但是,你在C组中询问有关C代码的问题,所以我假设你真的想编译这个作为C程序的
。然后编译器会出于几个原因发出警告。


首先,据我所知,即使在
C中也不鼓励使用隐式int声明。其次,编译器会注意到printf的参数不是
匹配格式字符串。这也是观察到

行为的原因。
您正在提供结构测试,然后是int。 printf需要两个整数。

现在你的结构测试不过是两个整数(i和j)。

这些是打印的。


GCC说:

7:警告:返回类型默认为'int''

函数`main'':

12 :警告:int格式,测试arg(arg 2)

可以解释为什么会发生这种情况?即使我给出var.i的
地址为什么j的值是geting打印?
#include<stdio.h>
typedef struct test{
int i;
int j;
}test;

main(){
test var;
var.i=10;
var.j=20;

printf("i==%d i==%d \n",var,var.i);
return 1;
}

Compiler in VC++ and see that you get the output is i==10 i==20
VC++ ought to reject this code, as it is not valid C++. However, you''re
asking in a C group about C code, so I assume you really want to compile
this as a C program. Then the compiler should warn for several reasons.

Firstly, as far as I know, implicit int declaration is discouraged even in
C. Secondly, the compiler will notice that the arguments of printf don''t
match the format string. This is also the reason for the observed
behaviour.
You''re supplying a struct test, then an int. printf expects two ints.
Now accidentally your struct test is nothing but two ints (i and j).
These are printed.

GCC says:
7: warning: return type defaults to `int''
In function `main'':
12: warning: int format, test arg (arg 2)
Can any one explain Why this thing happens?even though i give the
address of var.i why the value of j is geting printed?




你没有提供任何地址。

这需要一个指针,比如& var.i。


Markus



You''re not giving any addresses.
This would require a pointer, like &var.i.

Markus


DeltaOne写道:
DeltaOne wrote:
#include< stdio.h>
typedef struct test {
int i;
int j;
} test;

main(){
test var;
var.i = 10;
var.j = 20;

printf(" i ==%di ==%d \ n",var,var.i);
返回1;
}

用VC ++编译,看看你搞定了put is i == 10 i == 20

任何人都可以解释为什么会发生这种事情?即使我给出var.i的
地址为什么j的值是打印的?
#include<stdio.h>
typedef struct test{
int i;
int j;
}test;

main(){
test var;
var.i=10;
var.j=20;

printf("i==%d i==%d \n",var,var.i);
return 1;
}

Compiler in VC++ and see that you get the output is i==10 i==20

Can any one explain Why this thing happens?even though i give the
address of var.i why the value of j is geting printed?




这是因为您在2005年5月18日之前在特定的

系统上调用了未定义的行为。我建议不要在明天尝试

它可能会导致西斯的黑暗领主绕过来杀死你。


具体来说,你传递的var是一个结构,当在

格式说明符,你答应传递一个int。

-

Flash Gordon

生活在有趣的时代。

虽然我的电子邮件地址是垃圾邮件,但它是真的,我读了它。



It is happening because you invoked undefined behaviour on your specific
system before 18th May 2005. I would suggest not trying it tomorrow as
it might cause a dark lord of the Sith to come round and kill you.

Specifically, you passed var which is a struct to printf when in the
format specifier you promised to pass an int.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.


这篇关于printf的奇怪行为。请参阅下面的代码,请告诉它为什么会这样。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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