Click here to Skip to main content
14,767,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#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
Posted
Updated 13-Aug-16 23:04pm
v2

Quote:
What is the output of the following code?
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[^]
   
v2
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
 *
 */

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.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900