$ cat uninitialized.c
#include <stdio.h>
int main()
{
int x;
for (int i = 0; i < 5; i++)
x += 1;
printf("%d\n", x);
}
$ CC='gcc-13' CFLAGS='-std=c99 -Wall -Wextra' make uninitialized
gcc-13 -std=c99 -Wall -Wextra uninitialized.c -o uninitialized
$ # No warning
$ cat uninitialized.c
#include <stdio.h>
int main()
{
int x;
// for (int i = 0; i < 5; i++)
// x += 1;
printf("%d\n", x);
}
$ CC='gcc-13' CFLAGS='-std=c99 -Wall -Wextra' make uninitialized
gcc-13 -std=c99 -Wall -Wextra uninitialized.c -o uninitialized
uninitialized.c: In function 'main':
uninitialized.c:8:3: warning: 'x' is used uninitialized [-Wuninitialized]
8 | printf("%d\n", x);
| ^~~~~~~~~~~~~~~~~
uninitialized.c:5:7: note: 'x' was declared here
5 | int x;
| ^
OK, this is a rabbit hole.... (gcc loop uninitialized value no warning)
why am I not getting an "used uninitialized" warning from gcc in this trivial example? - Stack Overflow
Why is there not any warning on a declaration without initialization in a for loop? - Stack Overflow
GCC does not warn about uninitialized variables UB if you add a single, unrelated if statement. : r/cpp
18501 – [11/12/13/14 Regression] Missing 'used uninitialized' warning (CCP)
Better Uninitialized Warnings
https://gcc.gnu.org/bugzilla/buglist.cgi?bug_id=30542%2C30575%2C30856%2C33327%2C36814%2C37148%2C38945%2C39113%2C40469%2C42724%2C42884%2C45493%2C46684%2C46853%2C47623%2C48414%2C48643%2C49971%2C56972%2C57629%2C58323%2C58890%2C59225%2C60444%2C69578%2C78203%2C80152%2C80787%2C81049%2C81329%2C82203%2C82810%2C87365%2C89202%2C89501%2C96368
this is the closest 82810 – missed uninitialized variable warning in for/while loop