Re: One question of C
- From: santosh <santosh.k83@xxxxxxxxx>
- Date: Wed, 31 Oct 2007 17:46:12 +0530
drazet wrote:
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;
This 'x' shadows the global 'x' while it's in scope. Bad idea.
x++;
Local 'x' is now 11.
c1(x);
Local 'x' is still 11 since c1 gets, and acts on it's own copy of 'x'.
You also discard it's return value.
x++;
Local 'x' is now 12.
m1();
m1 updates the global 'x' and adds 10 to it.
Global 'x' is now 10.
printf("%d\t",x);
This prints the value of the local 'x', i.e., 12.
x++;
And increment it to 13.
c1(x);
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.
printf("%d\t",x);
Local 'x' is printed again, i.e., 13.
m1();
Global 'x' is incremented by 10 again.
printf("%d\n",x);
Once more local 'x' is printed, i.e., 13.
return 0;
}
int m1(void){
return (x+=10);
Here 'x' refers to the global object of that name.
}
int c1(int x){
return (x+=1);
}
[code end]
.
- References:
- One question of C
- From: drazet
- One question of C
- Prev by Date: Re: One question of C
- Next by Date: Re: nested structures and initialization
- Previous by thread: Re: One question of C
- Next by thread: Re: One question of C
- Index(es):
Relevant Pages
|