I tried the following versions of codes with both GCC and Clang/LLVM:
Version 1
#include <stdio.h>
main() {
work();
return 0;
}
work() {
int b;
printf("b: %d \n", b);
}
Version 2
#include <stdio.h>
main() {
work();
return 0;
}
work() {
int a = 1;
int b;
printf("a: %d b: %d \n", a, b);
}
Version 3
#include <stdio.h>
void work() {
int a = 1;
int b;
printf("a: %d b: %d \n", a, b);
}
int main(int argc, char** argv) {
work();
return 0;
}
For GCC,
Version 1 outputs b: 0
.
Version 2 outputs a: 1 b: 4195728
.
Version 3 outputs a: 1 b: 1742650088
.
For Clang
(LLVM),
Version 1 outputs b: 0
.
Version 2 outputs a: 1 b: -1643302816
.
Version 3 outputs a: 1 b: 0
.
I ran the same codes on several machines many times. The ones produced "0"
always produced "0", that is, in these cases, b
was initialized.
Questions:
What makes the GCC and Clang always produce "0" in version 1?
What makes Clang always produce "0" in Version 3?