This question already has answers here:
How do I use the conditional (ternary) operator?
(10 answers)
Closed 10 months ago.
Can someone explain this kind of statement(s)? I didn't get it.
data[i] = ((data[i] == div[i]) ? '0' : '1');
It's an if-else statement but the conditional operator takes less space and helps to write the if-else statements in the shortest way possible.
So , your expression can be visualized into if-else statement as:
#include <stdio.h>
int main(){
// Code ...
if(data[i]==div[i])
data[i]='0';
else
data[i]='1';
}
** ‘?:’ takes three operands to work, hence they are also called ternary operators.
Related
This question already has answers here:
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 4 years ago.
#include<stdio.h>
void main()
{
int x=3,y=2,z=0,m;
m=++x || ++y && ++z;
printf("\n %d %d %d %d\n",x,y,z,m); // 4 2 0 1
}
The output of the following code is mentioned as comment in program and I am trying to evaluate how this answer came but I am not able to understand.
I just wanted to know how the program calculates the relative value.
thanks to pmg, i have corrected my original answer (i had an error)
Because the left side of the OR operator (||) is not zero, it doesn't evaluate anything else on that line. This is called a "short circuit operator". In this example you gave, the programmer basically is tricking the compiler. If the argument on the right of the operator does not affect the outcome, it will not execute that code. However in this case, there are increments going on there and they won't be evaluated either.
This will assign "1" to m.
your output should be 3, 2, 0, 1.
This question already has answers here:
Square of a number being defined using #define
(11 answers)
Closed 5 years ago.
I was given a variation of this C code in an interview recently, and asked what would be returned by the function.
#define fun(a) a*a
int main() {
return(fun(4+5));
}
I've run it with a printf("%d"...) in place of the return and it prints 29. No other types than "%d" showed a result. Can someone explain what is going on here?
a is expanded, not evaluated as a. So you get:
4+5*4+5
and with operator priorities; you get 4 + 20 + 5 => 29
better way (with extra outside parenthesis for protecting against outside operators too thanks to Thomas comment):
#define fun(a) ((a)*(a))
but all parenthesis of the world still don't protect against i++, function calls (with side effects, preferably)... so argument reusing in macros is always a problem (there's no syntax to declare & assign a temp variable either). Prefer inlined functions in that case.
This question already has answers here:
What does the comma operator , do?
(8 answers)
Closed 6 years ago.
I tried executing the following c code:
#include <stdio.h>
int main()
{
int i=2000,j=100;
printf("%d",(j,i));
}
I gave different values to i and j and found the output. I always get the output as the value contained in the second variable. Does the expression always give the last variable as a result or does it have any other meaning?
You are using the comma operator.
The output of your code is 2000, because the comma operator evaluates the two expressions and returns the value of the second operand. More details can be found in this SO answer.
This question already has answers here:
Why can't I use a "break" statement inside a ternary conditional statement in C++?
(2 answers)
Closed 7 years ago.
just wondering if, for the following code lines, it is possible to replace using a ternary operator:
if( current->chbits[i] != '\0')
printf("%c\n",current->chbits[i]);
else
break;
If so, how would I parse it correctly?
You can't have break in a ternary operator.
See this for more information.
The ternary operator is used to evaluate an expresson conditionally:
result = condition ? first : second;
In your example is no conditional expression but an conditional statement.
Yes, you can replace the code with ternary operator by making some modifications to eliminate the break statement.
for(int i=0, int flag=1 ;flag!=0; i++){
flag=(current->chbits[i]!='\0') ? printf("%c\n",current->chbits[i]) : 0;
}
Here flag won't be zero since printf() returns the number of characters successfully written on the output. When current->chbits[i] != '\0' becomes false flag is set to 0 and for terminates as per the condition flag!=0.
This question already has answers here:
Why would you use the ternary operator without assigning a value for the "true" condition (x = x ?: 1)
(7 answers)
Closed 10 years ago.
I am attempting to use some code utilising a ternary statement in order to run a specific part, however, when I do this, I get the warning:
expression result unused
And the code in that particular section does not run.
The code in this case is:
i != a ?: printf("|%*s\\\n", i, "");
Why would that be? According to here, this form of the ternary operator, in which there is no alternative for the case, should function, however, it simple skips over it here. Any help is appreciated.
Your code is equivalent to
(i != a) ? (i != a) : printf(...);
Note that you won't end up using the i != a result, hence the warning. Best to write this as an if statement:
if(i==a) printf(...);