以下代码的输出是什么? [英] What is the output of the following code?

查看:109
本文介绍了以下代码的输出是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#define print (a) printf ("%d", a)
int a;
void A (int p)
{
	p+ = a;
	print (p);
}
void B(int *q)
{
	int p = *q + 3;
	A (a);
	*q = a – 2;
	print  (a);
	}
Main (void)
{
	a = 6;
	B(&a);
	print(a);
}





我的尝试:



无法弄清楚以下代码的输出



What I have tried:

unable to figure out the output of the following code

推荐答案

引用:

以下代码的输出是什么?

What is the output of the following code?

有一种简单的方法可以知道这段代码的输出,只需运行它就可以看到。



否则,要了解代码的作用,请尽快学习使用调试器。

调试器允许您逐行执行,检查变量,您将看到有一点让它停止做你期望的事情。

调试器 - 维基百科,免费的百科全书 [ ^ ]

< a href =http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn>掌握V调试isual Studio 2010 - 初学者指南 [ ^ ]

There is an easy way to know the output of this code, just run it and you will see.

Otherwise, to understand what the code is doing, learn to use the debugger as soon as possible.
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.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


这是伪代码吗?因为以下行,

Is this pseudo-code? Because the following line,
Main (void)



应更改为,


should be changed to,

int main (void)



然后程序的其余部分将编译并显示您想要的输出。但首先,您可能希望进行一些更改。看看下面的程序(没有编译错误),


Then the rest of the program would compile and show the output that you want. But first, there are a few changes that you might want to make. Have a look at the following program (without compilation errors),

#include <stdio.h>
#define print(a) printf("%d\n",a) // Removed the spaces.

int a;

void A (int p)
{
	p += a; // + = is not same as +=
	print (p);
}
void B(int *q)
{
	int p = *q + 3; // Why create this?
	A (a);
	*q = a - 2;
	print  (a);
}
	
int main (void)
{
	a = 6;
	B(&a);
	print(a);
}
/* Output:
 * 12
 * 4
 * 4
 *
 */



所做的更改是你应该避免向宏添加空格,它期望右边部分已经开始,或者左右。其次,C / C ++程序应该是ASCII(或仅在字符串的情况下为Unicode),并且你不使用那种编码,编译器会抱怨。我建议首先了解C编程的基础知识 [ ^ ]。此外,如果您希望以后共享此类程序,请在cpp.sh上创建一个在线C / C ++小提琴,如下所示, C ++ Shell [ ^ ]



如果这不是预期的输出,请查看程序的逻辑。


The changes made were that you should avoid adding spaces to the macro, it expects that the right-part has started, or so. Secondly, C/C++ programs should be ASCII (or Unicode in the case of strings only), and it you don't use that encoding, compiler will complain. I recommend learn the basics of C programming first[^]. Also, if you want to share such programs in future, please create an online C/C++ fiddle at cpp.sh, like this, C++ Shell[^]

If that was not the expected output, please have a look at the logic of your program.


这篇关于以下代码的输出是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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