我该如何解决这个问题? [英] How can i solve this question?

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

问题描述

请解释一下这个C代码输出及其方法。谢谢。

Please explain me about this C code output and its methodology.Thank you.

int f(int);
int g(int);
void main()
{
	int x,y,s=2;
	s *= 3;
	y = f(s);
	x = g(s);
	printf("%d %d %d",s,y,x);
	
}
int t = 8;

int f(int a)
{
	a += -5;
	t -= 4;
	return(a + t);
}
int g(int a)
{
	a = 1;
	t += a;
	return(a + t);
}





我的尝试:



是的,但我没想到这个输出:6 5 6



What I have tried:

Yes but I was not expecting this output : 6 5 6

推荐答案

注意f和g的参数在两种情况下均按值传递!因此,f不会改变s的值!



如果考虑到这一点,你可以很容易地看出输出为6 5 6的原因。
Note that the parameter of f and g is in both cases transferred by value! Hence, f does not change the value of s!

If you take that into account you can easily see why the output is 6 5 6.


您应该学习尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]

您应该学会使用调试器尽快。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


我看不到这种代码中的方法论。它是 C ,但它有它的规则,你的代码会破坏它们中的一些(例如 main 返回值必须是 int )。

这就是说,通过代码检查来预测输出(或者使用调试器,因为 ppolymorphe 已经建议):



I don't see 'a methodology' in such a code. It is C, however, it has its rules and your code breaks some of them (e.g. main return value must be int).
That's said, it is not difficult to foresee the output by code inspection (or using a debugger, as ppolymorphe already suggested):

int x,y,s=2;
s *= 3; // here s = 6



然后执行 f(6)并将其结果分配给 y ,让我们按照 f(6 )执行:


then f(6) is executed and its result assigned to y, let's follow f(6) execution:

a += -5; // here a = 1
t -= 4; // here the global variable t becomes 4
return(a + t); // here 1+4 = 5 becomes the return value



因此 y = f(6)= 5

我想你现在能够找出分配给<的值code> x 按 g(6)执行。


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

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