我真的不知道如何或爲何這樣工作的:C++變量聲明
#include <stdio.h>
#include<stdlib.h>
#include <limits.h>
#include <iostream>
using namespace std;
int main() {
int min = INT_MAX, min_index;
cout << min <<"\n";
cout << INT_MAX << "\n";
cout << min_index <<"\n";
system("pause");
return 0;
}
有實際產量爲min
,INT_MAX
和min_index
我知道INT_MAX
是部分limits.h
等min
和INT_MAX
是相同的,但min_index沒有給出值爲什麼它顯示一個值。
'min_index'是未初始化的變量,包含隨機垃圾。訪問其價值展示未定義的行爲。 「似乎工作」是未定義行爲的一種可能表現形式。 –
@HGI在大多數情況下,您應該使用'endl'來代替'<<「\ n」'。 http://stackoverflow.com/a/213977/3141234 – Alexander
@Alexander我會說相反。大多數時候你不需要衝洗緩衝區並支付相關的成本。 – juanchopanza