簡體   English   中英

我不明白我的編譯器如何在C ++中獲得此輸出

[英]I don't understand how my compiler got this output in C++

我的C ++程序如下:

#include<iostream>
#include<conio.h>
using namespace std;

int a;
struct CARE{
    long L1;
    void init()
    {
        L1=100;
    }
    void intake()
    {
        a++;
        L1+=++a;
    }
    void takeout()
    {
        int k=5;
        cout<<a*k<<'#'<<L1-a;
    }
};
int main()
{
    CARE c[3];
    for(int i=0;i<3;i++)
       c[i].init();
    for(int j=0;j<3;j++)
       c[j].intake();
    for(int m=0;m<3;m++)
       c[m].takeout();
    return 0;
    getch();
}

輸出結果是:

30#9630#9830#100

根據我的說法,“ a”將是一個垃圾變量,每個輸出都將與另一個不同,但是這里不是這種情況。 有人可以解釋為什么嗎?

a在全局范圍內,因此初始化為0

(實際上,如果在函數中聲明了它,則不會對其進行初始化,並且您的程序行為將是不確定的。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM