簡體   English   中英

C++中的表達式求值問題

[英]Expression evaluation problems in C++

請參閱以下代碼:

#include<bits/stdc++.h>
using namespace std;
int read(){
    int a;
    scanf("%d", &a);
    return a;
}
void print(int a, int b){
    printf("%d %d", a, b);
}
int main(){
    print(read(), read());
}

輸出的結果轉換了數字的順序,后面不是C++調用函數操作','嗎?

通常,在 C++ 中,子表達式的求值順序是未指定的 查看鏈接以進行更多討論,以及一些特殊情況。

C++ 標准說:

除非另有說明,否則對單個運算符的操作數和單個表達式的子表達式的評估是無序的。[...]

編譯器可能會根據效率或其他一些考慮因素決定首先評估print哪個參數(在您的示例中)。 永遠不應該依賴訂單。

暫無
暫無

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

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