在C++中,?:运算符比if()... else语句更快?它们在编译代码中有什么区别吗?
我有一个问题,编译器如何对以下代码进行操作:
#include<stdio.h>
int main(void)
{
int b=12, c=11;
int d = (b == c++) ? (c+1) : (c-1);
printf("d = %i\n", d);
}
Run Code Online (Sandbox Code Playgroud)
我不知道为什么结果是???d = 11.
我在C中有一段代码如下:
main()
{
int a=10, b;
a>=5 ? b=100 : b=200 ;
printf("%d" , b);
}
Run Code Online (Sandbox Code Playgroud)
在unix中运行gcc编译器上的代码会将编译时错误生成为'赋值左值所需的左值',并将错误指向b = 200,而在使用Turbo C编译的窗口中,将200作为输出.
任何人都可以解释一下这种情况究竟发生了什么?
:在 C++中做什么?? :
它与运营商有什么区别吗?例如在下面的代码中:
// Extracting the coefficients and exponents as numbers
int expon[21] = { 0 };
int coeff[21] = { 0 };
for (int i = 0; i < monoms; ++i)
{
int monomSize = monomStr[i].size();
for (int j = 0; j < monomSize; ++j)
{
if (monomStr[i][j] == '^')
{
expon[i] = stoi(monomStr[i].substr(j + 1, monomSize - j));
coeff[i] = stod(monomStr[i].substr(0, j));
break;
}
}
}
// Looking for the max of exponents
int maxExponent …Run Code Online (Sandbox Code Playgroud) 我在网上发现了这篇文章,并认为我会尝试将空字符串样式方法应用于我的excel范围值.有时它有一个值,有时它没有,显然double不喜欢空值.
这是我的小片段.
double valTotal = (rngTotal.Value != null ? 1 : 0);
Run Code Online (Sandbox Code Playgroud)
我的问题是我用上面的代码做了什么?它看起来像一行中的if语句,其中"1"是"then"而"0"是"else".是对的吗?最重要的是,这个语法的名称是什么,所以我可以找到更多相关信息?
我在书中遇到了一个问题,它要求我写出以下程序的输出.
#include<stdio.h>
int main()
{
int j=4;
( !j != 1 ? printf("\nWelcome") : printf("GooD Bye"));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我基本上无法理解在运行程序时如何打印Welcome.任何人都可以在运算符层次结构的帮助下解释,编译器根据表达式计算出什么值?
什么意思是" 返回p?memcpy(p,s,len):NULL; "在下面的代码中?(更一般地说,条件运算符是什么,a ? b : c?)
char * strdup(const char * s)
{
size_t len = 1+strlen(s);
char *p = malloc(len);
return p ? memcpy(p, s, len) : NULL;
}
Run Code Online (Sandbox Code Playgroud) c ×4
c++ ×2
c# ×1
gcc ×1
increment ×1
objective-c ×1
operators ×1
performance ×1
syntax ×1
turbo-c ×1