Re: One question of C



drazet wrote:

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;

A new `x`, nothing to do with the `x` above, initialised to 10.

x++;

x := 11.

c1(x);

`x` is unaffected.

x++;

x := 12.

m1();

`x` is unaffected.

printf("%d\t",x);

Prints `12`.

x++;

x := 13.

c1(x);

`x` is unaffected.

printf("%d\t",x);

Prints `13`.

m1();

`x` is unaffected.

printf("%d\n",x);

Prints `13` again.

return 0;
}
int m1(void){
return (x+=10);

Updates the /global/ `x` (which you never refer to in `main`).

}
int c1(int x){
return (x+=1);

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

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

}

[code end]


--
Hewlett-Packard Limited registered office: Cain Road, Bracknell,
registered no: 690597 England Berks RG12 1HN

.