有条件运算符中的此分配有什么问题? [英] What is wrong with this assignment in a conditional operator?
本文介绍了有条件运算符中的此分配有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有错误。
在以下代码中为a [i]分配值是否错误?
或条件运算符有问题吗?
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<stdio.h>
#include<string.h>
int main(){
char a[12]="sumit tyagi";
int i=0;
while(a[i]!='\0'){
a[i]>90 ? a[i]=a[i]-32 : a[i]=a[i]+32; //error in this line
i++;
}
printf("\n %s",a);
推荐答案
a [i]> 90? a [i] = a [i] -32:a [i] = a [i] +32;
是不是评估为
a [i]> 90吗? (a [i] = a [i] -32):(a [i] = a [i] +32);
因为 =
的优先级低于?:
。在标准C语言中,尽管某些编译器允许将其作为扩展,但是您无法如上所述编写。
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;
这篇关于有条件运算符中的此分配有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文