Click here to Skip to main content
15,902,447 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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);
}


What I have tried:

Yes but I was not expecting this output : 6 5 6
Posted
Updated 13-Feb-16 4:05am
v2
Comments
Jochen Arndt 13-Feb-16 10:08am    
What did you expect?

Note that the argument of function g() does not care because it is overwritten by a=1.
Sergey Alexandrovich Kryukov 13-Feb-16 11:27am    
Just use the debugger. What do you want to achieve and what's the problem?
—SA

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.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 13-Feb-16 11:28am    
5ed.
—SA
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[^]
 
Share this answer
 
Comments
CPallini 13-Feb-16 16:18pm    
5.
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):

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

then f(6) is executed and its result assigned to y, let's follow f(6) execution:
C
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

hence y=f(6)=5.
I suppose you are now able to figure out the value assigned to x by g(6) execution.
 
Share this answer
 
Comments
Patrice T 13-Feb-16 17:48pm    
+5 too
CPallini 14-Feb-16 3:43am    
Thank you.

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