Ambiguous if and else branches: Is the behaviour defined?

前端 未结 5 1978
既然无缘
既然无缘 2021-01-18 11:33

I recently came accross some C++ code like the following:

if(test_1)
    if(test_2)
    {
         // Do stuff
    }
    else
       exit(0);
5条回答
  •  忘掉有多难
    2021-01-18 11:59

    Is the behaviour of this code defined according to any standard (C, C++)?

    Yes, it is defined. In C (and all similar languages as I know), the "dangling else" binds to the last free if, therefore this interpretation

    if(test_1)
    {
        if(test_2)
        {
        }
        else
        {
        }
    }
    

    is correct.

提交回复
热议问题