标签: conditional-operator

左值作为条件运算符中赋值的左操作数

#include <stdio.h>
int main()
{
        int a = 1, b;
        a ? b = 3 : b = 4;
        printf("%d, %d", a, b);
        return 0;
}

[user@localhost programs]$ gcc -Wall vol.c
vol.c: In function ‘main’:
vol.c:5:16: error: lvalue required as left operand of assignment
  a ? b = 3 : b = 4;
                ^
Run Code Online (Sandbox Code Playgroud)

我给了左值,b然后为什么gcc显示错误以及如何解决它?

c c++ gcc compiler-errors conditional-operator

-4
推荐指数
1
解决办法
8007
查看次数

Java 内联 If 语句

全部!我一直在学习如何在Java中使用三元语句,我达到了这个阶段:

变量=(条件)?如果条件为假会发生什么如果条件为真会发生什么

尽管如此,我一直想知道如果我不希望代码在条件为假时执行任何操作,是否可以应用这一点。我知道你可以这样做:

if (条件) {行动}

那么是否可以在不分配错误条件的情况下使用三元语句,如果可以,如何操作?

谢谢你!

java if-statement conditional-operator

-4
推荐指数
1
解决办法
1696
查看次数

如何使用?:运营商

如何将以下代码转换为使用?:运算符..是否可能?

tbtotalamount.Text = string.Format("{0:n2}", dtl.Compute("sum(NetPay)", ""));
if (tbtotalamount.Text.Length == 0)
{
    tbtotalamount.Text = "0";
}
Run Code Online (Sandbox Code Playgroud)

c# string conditional-operator

-5
推荐指数
1
解决办法
151
查看次数

Objective C中的条件运算符

我们可以在目标C中使用条件运算符,就像在C++中一样.我只是尝试像这样(条件)实现?真实陈述:虚假陈述;

 if(page==1)?(buttonPrev.hidden=TRUE):(buttonPrev.hidden=FALSE);
Run Code Online (Sandbox Code Playgroud)

但它导致错误"预期表达"

iphone objective-c conditional-operator conditional-statements

-5
推荐指数
1
解决办法
4940
查看次数

令人困惑的C条件运算符的结果

int a=1,b=2,c=3;
int x=1;
int y=10;
a = x ? b : c;  
cout<< a;      // Outputs 2 (the value of b)
a = y ? b : c; 
cout<< a;      // Outputs 2 (the value of b)
Run Code Online (Sandbox Code Playgroud)

现在,看看以下内容.

a=0;
x=0;
a = x ? b : c;
cout<< a;  //   Outputs 3 (the value of c !!!!) 
Run Code Online (Sandbox Code Playgroud)

为什么这种异常行为?仅当a和x均为0时,表达式的计算结果为false,否则始终为true.请解释.

c c++ ternary-operator conditional-operator

-7
推荐指数
1
解决办法
164
查看次数

没有 else 子句的单行 If 条件

我们如何在运算符中编写没有 else 的单行 If 条件?

例子:

如果(计数== 0){计数= 2; }

我们怎么能像下面这样写:

计数=计数==0?2;

由于三元运算符需要 if else 条件。我想在没有 ternery 运算符的情况下做到这一点。C#中是否有可用的运算符?

谢谢。

c# conditional if-statement conditional-operator assignment-operator

-7
推荐指数
1
解决办法
4415
查看次数

为什么“extern”关键字在同一个文件中不起作用?

请参阅下面的示例代码以了解“extern”的使用。当我在代码中使用 extern 关键字时,出现编译错误。请提出问题的解决方案。

#include<iostream>

  extern int x;
  extern int y;
  extern int z;

int main(){
    
    x = 10;
    y = 15;
    
    z = (x>y ? x: y);
    
    std::cout<<z;
     
    return 0;
}

Run Code Online (Sandbox Code Playgroud)

错误信息:

example8.cpp:(.rdata$.refptr.z[.refptr.z]+0x0): undefined reference to `z';
example8.cpp:(.rdata$.refptr.y[.refptr.y]+0x0): undefined reference to `y';
example8.cpp:(.rdata$.refptr.x[.refptr.x]+0x0): undefined reference to `x';
F:\DEVC_workspace\collect2.exe  [Error] ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

c++ conditional-operator extern

-8
推荐指数
1
解决办法
128
查看次数