The result of int c=0; cout<<c++<<c;

后端 未结 5 1907
无人及你
无人及你 2020-12-06 19:29

I think it should be 01 but someone says its \"undefined\", any reason for that?

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-06 20:14

    As I see it, f(c++); is equivalent to: f(c); c += 1;

    And f(c++,c++); is equivalent to: f(c,c); c += 1; c += 1;

    But it may be the case that f(c++,c++); becomes f(c,c+1); c+= 2;

    An experiment with gcc and clang, first in C

    #include 
    
    void f(int a, int b) {
        printf("%d %d\n",a,b);
    }
    
    int main(int argc, char **argv) {
        int c = 0;
        f(c++,c++);
    
        return 0;
    }
    

    and in C++

    #include 
    
    int main(int argc, char **argv) {
        int c = 0;
        std::cout << c++ << " " << c++ << std::endl;
        return 0;
    }
    

    Is interesting, as gcc and g++ compiled results in 1 0 whereas clang compiled results in 0 1

提交回复
热议问题