the same id in the same scope refers to different objects
Xi Ruoyao
ryxi@stu.xidian.edu.cn
Sun Sep 3 08:53:00 GMT 2017
On 2017-09-03 11:15 +0300, Andrew Makhorin wrote:
> Hello,
>
> Could anyone tell me if the behavior of the following program is
> correct? Or there is an error not detected by GCC?
>
> mao@corvax:~/Desktop$ cat foo.c
> #include <stdio.h>
>
> int x = 111;
>
> int main(void)
> {Â Â Â Â Â int y = x + 222;
> Â Â Â Â Â Â int x = 555;
> Â Â Â Â Â Â printf("x = %d; y = %d\n", x, y);
> Â Â Â Â Â Â return 0;
> }
> mao@corvax:~/Desktop$ gcc foo.c
> mao@corvax:~/Desktop$ ./a.out
> x = 555; y = 333
> mao@corvax:~/Desktop$ gcc --version
> gcc (Debian 4.7.2-5) 4.7.2
>
What the meaning of "correct" is? Of course this program is
well-formed and the result is standard conforming.
If your "error" means "silly mistake", and you want GCC to help
you to detect silly mistakes, you should use warnings. For this
example:
$ gcc-4.7 shadow.c  -Wshadow
shadow.c: In function 'main':
shadow.c:7:11: warning: declaration of 'x' shadows a global declaration [-Wshadow]
shadow.c:3:5: warning: shadowed declaration is here [-Wshadow]
--
Xi Ruoyao <ryxi@stu.xidian.edu.cn>
School of Aerospace Science and Technology, Xidian University
More information about the Gcc-help
mailing list