One question of C

S

santosh

drazet said:
why the answer is 12 13 13?

Code:
#include <stdio.h>
int x;
int m1(void);
int c1(int x);

int main(void){
int x= 10;[/QUOTE]

This 'x' shadows the global 'x' while it's in scope. Bad idea.
[QUOTE]
x++;[/QUOTE]

Local 'x' is now 11.
[QUOTE]
c1(x);[/QUOTE]

Local 'x' is still 11 since c1 gets, and acts on it's own copy of 'x'.
You also discard it's return value.
[QUOTE]
x++;[/QUOTE]

Local 'x' is now 12.
[QUOTE]
m1();[/QUOTE]

m1 updates the global 'x' and adds 10 to it.
Global 'x' is now 10.
[QUOTE]
printf("%d\t",x);[/QUOTE]

This prints the value of the local 'x', i.e., 12.
[QUOTE]
x++;[/QUOTE]

And increment it to 13.
[QUOTE]
c1(x);[/QUOTE]

Once more c1 gets it's own copy of main's 'x' and increments it by one
and returns that value, which you discard once again.
[QUOTE]
printf("%d\t",x);[/QUOTE]

Local 'x' is printed again, i.e., 13.
[QUOTE]
m1();[/QUOTE]

Global 'x' is incremented by 10 again.
[QUOTE]
printf("%d\n",x);[/QUOTE]

Once more local 'x' is printed, i.e., 13.
[QUOTE]
return 0;
}[/QUOTE]
[QUOTE]
int m1(void){
return (x+=10);[/QUOTE]

Here 'x' refers to the global object of that name.
[QUOTE]
}[/QUOTE]
[QUOTE]
int c1(int x){
return (x+=1);
}

[code end]
[/QUOTE]
 
C

Chris Dollin

drazet said:
why the answer is 12 13 13?

Because that's the right answer.
Code:
#include <stdio.h>
int x;
int m1(void);
int c1(int x);

int main(void){
int x= 10;[/QUOTE]

A new `x`, nothing to do with the `x` above, initialised to 10.
[QUOTE]
x++;[/QUOTE]

x := 11.
[QUOTE]
c1(x);[/QUOTE]

`x` is unaffected.
[QUOTE]
x++;[/QUOTE]

x := 12.
[QUOTE]
m1();[/QUOTE]

`x` is unaffected.
[QUOTE]
printf("%d\t",x);[/QUOTE]

Prints `12`.[QUOTE]
x++;[/QUOTE]

x := 13.
[QUOTE]
c1(x);[/QUOTE]

`x` is unaffected.
[QUOTE]
printf("%d\t",x);[/QUOTE]

Prints `13`.
[QUOTE]
m1();[/QUOTE]

`x` is unaffected.
[QUOTE]
printf("%d\n",x);[/QUOTE]

Prints `13` again.
[QUOTE]
return 0;
}
int m1(void){
return (x+=10);[/QUOTE]

Updates the /global/ `x` (which you never refer to in `main`).
[QUOTE]
}
int c1(int x){
return (x+=1);[/QUOTE]

Updates c1's /local/ `x` (which you can't refer to from `main`).

["C does not have call-by-reference".]
[QUOTE]
}

[code end]
[/QUOTE]
 
K

Kenneth Brody

drazet said:
why the answer is 12 13 13?

Code:
[/QUOTE]
[... snip homework assignment ...][QUOTE]
[code end][/QUOTE]

First, explain why you feel the output should be otherwise.

Then, research "scope", "call by value", and "call by reference".

Finally, explain why you still feel the output should be otherwise,
if you still don't understand.

-- 
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody        | www.hvcomputer.com | #include              |
| kenbrody/at\spamcop.net | www.fptech.com     |    <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:[email protected]>
 
D

drazet

why the answer is 12 13 13?

Code:
#include <stdio.h>
int x;
int m1(void);
int c1(int x);

int main(void){
	int x= 10;
	x++;
	c1(x);
	x++;
	m1();
	printf("%d\t",x);
	
	x++;
	c1(x);
	printf("%d\t",x);
	m1();
	printf("%d\n",x);
	return 0;
}
int m1(void){
	return (x+=10);
}
int c1(int x){
	return (x+=1);
}

[code end]
 
J

jaysome

why the answer is 12 13 13?

The first thing I do to help me in answering your question is to run
PC-lint on your code.
Code:
#include <stdio.h>
int x;
int m1(void);
int c1(int x);

int main(void){
	int x= 10;[/QUOTE]

Warning 578: Declaration of symbol 'x' hides symbol 'x' (line 2)
[QUOTE]
x++;
	c1(x);[/QUOTE]

Ignoring return value of function 'c1(int)' (compare with line 4)
[QUOTE]
x++;
	m1();[/QUOTE]

Warning 534: Ignoring return value of function 'm1(void)' (compare
with line 3)
[QUOTE]
printf("%d\t",x);
	
	x++;
	c1(x);[/QUOTE]

Warning 534: Ignoring return value of function 'c1(int)' (compare with
line 4)
[QUOTE]
printf("%d\t",x);
	m1();[/QUOTE]

Warning 534: Ignoring return value of function 'm1(void)' (compare
with line 3)
[QUOTE]
printf("%d\n",x);
	return 0;
}
int m1(void){
	return (x+=10);
}
int c1(int x){[/QUOTE]

Warning 578: Declaration of symbol 'x' hides symbol 'x' (line 2)
[QUOTE]
return (x+=1);
}

[code end][/QUOTE]

These warnings should give you enough fodder to use in helping you to
answer your own question.

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top