goto not working with ?: operator in C

蹲街弑〆低调 提交于 2019-12-11 20:07:25

问题


For learning purposes, I wrote the following code snippet:

for(int i=0;i<10;i++)
{
  for(int j = 0;j<5;j++)        
  {
    //(i==j && i==3)? (goto found) : printf("stya here\n");        
    if(i==j && i==3){goto found;} else {printf("stay here\n");}
  }
}

found:
  printf("yes I am here");

But I wondered when I discovered the omitted statement inside the inner loop not gives error and now I am confused about if-else is not always replaceable with ?: operator. What is the fact here? Why does the commented statement give an error?


回答1:


The ?: operator is not replacement for if. It works only for expressions: condition ? expr1 : expr2 where both sub-expressions expr1 and expr2 are of the same type (and the whole expression then is of the same type).

goto is not expression, it is a statement.




回答2:


I am not well versed enough in C to explain why this doesn't work syntactically, but in the sense of intent the ?: ternary operator form is intended as a conditional expression (yields a result), not as a control flow mechanism. Using the if statement you can choose a value for a variable or change the flow of the application. e.g.

//Change flow

if(x ==0)
{
   //do this
} 
else
{
 //goto some label
} 

or

//Change value
    if(x == 0)
    {
      y = 1;
    }
    else
    {
      y = 2;
    }

The ternary is only intended for the second case, as a conditional expression i.e.

y = (x ==0) ? 1 :2;



回答3:


What would be the result of "goto found" expression? I don't know, neither does the compiler, so the result of ? expression cannot be determined, hence the error.




回答4:


In general, the ?: operator is no replacement for a classic if() ... else() .... It might be used as such if both operators (and the condition) are values or expressions returning a value. You can't use them with statements like goto, break or continue.

The following would be possible:

condition ? dothis() : dothat(); // there's no assignment, but it's still valid
var = condition ? dothis() : othervar;
condition ? (var=4, othervar=3) : (somevar = 1);

But you can't include anything that's not an expression (i.e. nothing not having some value or result):

condition ? continue : break; // statements letting the execution continue somewhere else
condition ? {var = 4; othervar = 3;} : dothat(); // trying to inline scopes/multiple exressions
var = condition ? while(var) {var--;} : 5; // similar, inlining a complete loop

These last examples can be done, but they'd require you to use if() or function bodys to call:

if (condition) continue; else break;
condition ? (var = 4, var = 3) : dothat();
var = condition ? dotheloop(var) : 5; // ok, this could be 'var = condition ? 0 : 5;' but... example code



回答5:


Actually, what you're trying to do with goto and the ternary operator is possible if your compiler support the extension Statement Expressions, as it's name said, this extension allow you to write statements inside an expression or sub-expressions, just like this:

(rand() % 2) ? ({goto lbl1;}) : ({goto lbl2;});

Using these statements can be very useful (mostly to optimize macros) but often lead to very dirty code, just like the example i gave :)

So to complement the other answers i'll say that it's not possible in C99/11 without extension but most of the recent compiler support a bunch of extension that allows you to do non-standard cool things.



来源:https://stackoverflow.com/questions/11272947/goto-not-working-with-operator-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!