此功能的奇怪行为 [英] Strange Behaviour of this function

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

问题描述

#include< stdio.h>

/ *第一个例子


int a()

{

返回1;


}


b()

{
< br $>
}

int main()

{


int x;

a();

x = b();


printf("%d",x);


}


* /

//第二个例子

int a()

{

返回1;


}


b()

{

a();

}


int main()

{

int x;

x = b();

printf("%d",x);

}

请参阅第一个示例和第二个示例。我不知道为什么

输出有差异。我尝试在vc ++ 6.0中运行


提前致谢


问候

Ankush

#include <stdio.h>
/* 1st example

int a()
{
return 1;

}

b()
{

}
int main()
{

int x;
a();
x=b();

printf("%d",x);

}

*/
// 2nd example
int a()
{
return 1;

}

b()
{
a();
}

int main()
{

int x;
x=b();
printf("%d",x);
}
Please see the first example and the second example. I don''t know why
is there a difference of output. I tried it running in vc ++ 6.0

Thanks in advance

Regards
Ankush

推荐答案

g。***** **@gmail.com 写道:

#include< stdio.h>


/ *第一个例子


int a()

{

返回1;


}


b()

{


}


int main()

{


int x;

a();

x = b() ;


printf("%d",x);


}


* /


//第二个例子


int a()

{

返回1;


}


b()

{

a();

}


int main()

{


int x;

x = b();

printf("%d",x);

}


请求请参阅第一个示例和第二个示例。我不知道为什么

输出有差异。我试过它在vc ++ 6.0中运行
#include <stdio.h>
/* 1st example

int a()
{
return 1;

}

b()
{

}
int main()
{

int x;
a();
x=b();

printf("%d",x);

}

*/
// 2nd example
int a()
{
return 1;

}

b()
{
a();
}

int main()
{

int x;
x=b();
printf("%d",x);
}
Please see the first example and the second example. I don''t know why
is there a difference of output. I tried it running in vc ++ 6.0



你的代码表现出未定义的行为。

你'b''函数声明没有返回类型,表示它

默认为int。但是你没有从它返回一个int - 你应该,

注意main也被声明为返回一个int,所以从main返回一个int


Your code exhibits undefined behavior.
You ''b'' function is declared without a return type, which means it
defaults to int. Yet you don''t return an int from it -- you should,
note that main also is declared to return an int, so return an int
from main too.


" g。******* @ gmail.com" < g。******* @ gmail.comwrites:
"g.*******@gmail.com" <g.*******@gmail.comwrites:

#include< stdio.h>


/ *第一个例子


int a()

{

返回1;


}


b()

{


}
#include <stdio.h>
/* 1st example

int a()
{
return 1;

}

b()
{

}



您没有为函数b指定返回类型。在C89 / C90中,它是
默认为int。在C99中,隐含int是指功能已被删除。

明确指定类型总是更好。


您不能从函数中返回值。你需要。

You didn''t specify a return type for the function b. In C89/C90, it
defaults to int. In C99, the "implicit int" feature has been removed.
It''s always better to specify the type explicitly.

You don''t return a value from the function. You need to.


int main()

{


int x;

a();

x = b();
int main()
{

int x;
a();
x=b();



这里你使用b()返回的值,但b中没有返回

语句。这可以为x分配任意值。

Here you use the value returned by b(), but there''s no return
statement in b. This could assign any arbitrary value to x.


printf("%d",x);


}


* /


//第二个例子


int a()

{

返回1;


}


b()

{

a();

}
printf("%d",x);

}

*/
// 2nd example
int a()
{
return 1;

}

b()
{
a();
}



与第一个例子相同。

Same thing as the first example.


int main()

{


int x;

x = b() ;
int main()
{

int x;
x=b();



再次,你为x分配一些垃圾值。

And again, you assign some garbage value to x.


printf("%d" ,x);

}


请参阅第一个示例和第二个示例。我不知道为什么

输出有差异。我尝试在vc ++ 6.0中运行
printf("%d",x);
}
Please see the first example and the second example. I don''t know why
is there a difference of output. I tried it running in vc ++ 6.0



在这两种情况下你都会得到垃圾。它恰好是不同的垃圾。

不要担心为什么它会有所不同,只需修复程序。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net / ~kst>

圣地亚哥超级计算机中心< *< http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。

You get garbage in both cases. It happens to be different garbage.
Don''t worry about why it''s different, just fix the program.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Keith Thompson写道:
Keith Thompson wrote:

" g。**** ***@gmail.com" < g。******* @ gmail.comwrites:
"g.*******@gmail.com" <g.*******@gmail.comwrites:

#include< stdio.h>
#include <stdio.h>



[空白修改...]

[whitespace modified...]


int a(){return 1; }

b(){}
int a() { return 1; }
b() { }



你没有为函数b指定返回类型。在C89 / C90中,它是
默认为int。在C99中,隐含int是指功能已被删除。

明确指定类型总是更好。


您不能从函数中返回值。你需要。


You didn''t specify a return type for the function b. In C89/C90, it
defaults to int. In C99, the "implicit int" feature has been removed.
It''s always better to specify the type explicitly.

You don''t return a value from the function. You need to.



如果要使用返回值,则需要。如果没有

的返回值,并且调用者并不期望一个,那么实际上没有
要求在C90或C99中返回一个。

[C99'虽然倾销了隐含的int。这个故事在C ++中是不同的
。]

You need to if you want to use the return value. If there is no
return value, and the caller doesn''t expect one, then there isn''t
actually a requirement to return one in either C90 or C99.
[C99''s dumping of implicit int notwithstanding. The story is
different in C++.]


int main()

{


int x;

a();

x = b();
int main()
{

int x;
a();
x=b();



这里你使用b()返回的值,但b中没有返回

语句。这可以为x分配任意值。


Here you use the value returned by b(), but there''s no return
statement in b. This could assign any arbitrary value to x.



否。这不是返回任意值的问题。

仅仅尝试进行此类调用是未定义的行为

本身。


即使b()的返回类型为unsigned char,UB仍然可以调用



-

彼得

No. It''s not a question of what arbitrary value gets returned.
Merely attempting to make such a call is undefined behaviour
in itself.

Even if b() had a return type of unsigned char, UB would still
be invoked.

--
Peter


这篇关于此功能的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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