What is wrong with this assignment in a conditional operator?

前端 未结 4 1066
独厮守ぢ
独厮守ぢ 2021-01-15 09:18

There is an error. Is it wrong to assign a value to a[i] in the following code? Or something is wrong with conditional operators?

#include
#in         


        
4条回答
  •  长情又很酷
    2021-01-15 09:42

    a[i]>90 ? a[i]=a[i]-32 : a[i]=a[i]+32;

    is not evaluated as

    a[i]>90 ? (a[i]=a[i]-32) : (a[i]=a[i]+32);

    since = has lower precedence than ?:. In standard C you can't write it as above although some compilers allow it as an extension.

    You could write it as the more readable (and portable)

    a[i] += a[i] > 90 ? -32 : +32;
    

提交回复
热议问题