'value changes after adding optimization flag in Clang
I have written a little program:
#include <stdio.h>
void tester() {
int x = 69;
}
void x() {
int x;
printf("%d\n", x);
}
int main() {
tester();
x();
}
which I expect to print out 69. It prints out 69 when I compile without optimization, but with O1-O3, it prints out large numbers. Why is this?
Solution 1:[1]
those 2 x variable are totally different things, they just happen to have the same name. If you want it to be the same x then you must create a global variable
int x;
void tester() {
x = 69;
}
void xx() {
printf("%d\n", x);
}
int main() {
tester();
xx();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | pm100 |
