有沒有人知道我的程序爲什麼在下面的情況下打印-69?我期望它用C語言打印未初始化原始數據類型的默認/垃圾值。謝謝。我可以重新初始化全局變量以覆蓋C中的值嗎?
#include<stdio.h>
int a = 0; //global I get it
void doesSomething(){
int a ; //I override global declaration to test.
printf("I am int a : %d\n", a); //but I am aware this is not.
a = -69; //why the function call on 2nd time prints -69?
}
int main(){
a = 99; //I re-assign a from 0 -> 99
doesSomething(); // I expect it to print random value of int a
doesSomething(); // expect it to print random value, but prints -69 , why??
int uninitialized_variable;
printf("The uninitialized integer value is %d\n", uninitialized_variable);
}
:這裏是流「A」與2次呼叫doesSomethnig的:讓圖像並作爲堆棧> A =垃圾,(打印垃圾),a =(-69),退出循環,我假設自動變量被釋放並且棧被清除,但是你提到的'a'位置的參數不會改變,在第二次調用doesSomething:a =垃圾,然後打印一個=>它應該打印垃圾。爲什麼要打印(-69),垃圾值現在在堆棧頂部,不是嗎? –