A simple program:
int main()
{
long i = i;
return 0;
}
Compiling as C gives no errors and no warnings.
$ gcc -Wall -Wextra -pedantic 1.c
Compiling as C++ gives a warning:
$ c++ -Wall -Wextra -pedantic 1.c
1.c: In function ‘int main()’:
1.c:3:7: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
long i = i;
In both cases variable i seems to be 0, although in c++ it could be uninitialized. I actually made such a typo in one of my functions and it was quite hard to find it. What can I do to avoid this? I'd expect at least a warning. Moreover, Clang doesn't give any warning in either case (c or c++). Is there a specific part of the standard that says anything about this behavior?
Edit: Having tried something similar:
$ cat 1.c
int main(void)
{
int k = k + 0;
int i = i + 1;
return 0;
}
The warning (in C) is generated only for "i".
$ gcc -Wall -Wextra 1.c
1.c: In function ‘main’:
1.c:4:6: warning: ‘i’ is used uninitialized in this function [-Wuninitialized]
int i = i + 1;