for (minus == false ? i = 0 : i = 1; string[i] >= '0' && string[i] <= '9'; ++i)
{
intValue = string[i] - '0';
minus == false ? result = result * 10 + intValue :
result = result * 10 - intValue;
}
error: expression is not assignable
screenshot - http://share.pho.to/AarcJ
https://codeshare.io/5Pdd7X
minus == false ? i = 0 : i = 1 will be parsed as (minus == false ? i = 0 : i) = 1 because of operator precedence rule. After evaluation of minus == false ? i = 0 : i, left side of operator = will become an rvalue, but assignment operator must have an lvalue as its left operand.
Change it to minus == false ? (i = 0) : (i = 1)
Use (for example)
for (i = minus? 1:0; string[i].... etc
And...
result = result * 10 + minus? (-lastvalue) : lastvalue;
Related
I wonder if I can reduce the number of lines of the following code to a single one?
if (a > (b+10))
{
value = 1;
}
else
{
value = 0;
}
For booleans (true, false) just do:
bool value = a > b + 10;
You could use the ternary operator:
value = (a > (b+10)) ? 1 : 0;
For this case there are two options:
ternary operator
using the boolean value
Ternary operator
The ternary operator can substitute very simple if-else conditions such as:
value = (a > (b+10)) ? 1 : 0;
Boolean value
Since your condition is attributing the value of 1 or 0 you can simply use the condition evalution. In c when you evaluate a condition it returns an integer between 1 and 0. Which is exactly your objective, making the above expression even simpler.
value = (a > (b+10))
Yes, using ternary operator.
Syntax:
var = (condition) ? (expression1 if condition is true) : (expression2 if condition is false);
Your example:
value = (a > (b+10)) ? 1 : 0;
Reducing the number of lines is easy: you can use a different brace style:
if (a > (b+10)) {
value = 1;
} else {
value = 0;
}
You can use single statements instead of blocks:
if (a > (b+10))
value = 1;
else
value = 0;
You can cram the if statement on a single line (with or without curly braces):
if (a > (b+10)) { value = 1; } else { value = 0; }
if (a > (b+10)) value = 1; else value = 0;
You can further simplify using the ternary operator:
value = (a > (b+10)) ? 1 : 0;
You can also remove redundant parentheses:
value = a > b + 10 ? 1 : 0;
Finally, since comparisons in C evaluate to int values 1 or 0, the above expression is still fully redundant, so you could simply write:
value = a > b + 10;
Note that value does not need to have type bool for its value to be 1 or 0 in this case, but typing it as bool may help the reader better understand the code.
I have this expression with ?: operator:
(adc.Voltage1[Counter.ADC_ConversionCount] = ADC_readResult(Handler.myAdc, ADC_ResultNumber_1)) > 10 ? Counter.WriteOut = 1 : Counter.WriteOut = 0;
And the same expression with if-else:
if((adc.Voltage1[Counter.ADC_ConversionCount] = ADC_readResult(Handler.myAdc, ADC_ResultNumber_1)) > 10 ){
Counter.WriteOut = 1;
}else{
Counter.WriteOut = 0;
}
Why am I getting "expression must be a modifiable lvalue" error in the first case?
The ADC_readResult function return type is uint_least16_t. Here is the Counter struct definition and the ADC struct definition:
typedef struct __COUNTERS__ {
uint16_t WriteOut;
uint16_t ADC_ConversionCount;
uint16_t ADC_CycleCount;
uint8_t LimitADCReached1;
uint8_t LimitADCReached2;
uint8_t LimitADCReached3;
uint8_t LimitADCReached4;
uint8_t LimitADCReached5;
} COUNTERS;
typedef struct __ADC_VOLTAGES__ {
uint16_t Voltage1[ADC_VAL];
uint16_t Voltage2[ADC_VAL];
uint16_t Voltage3[ADC_VAL];
uint16_t Voltage4[ADC_VAL];
uint16_t Voltage5[ADC_VAL];
} ADC;
The error you're getting has to do with the way the expression is parsed.
Your expression (simplified) looks like this:
(a = b) < 10 ? c = 1 : c = 0
The ternary operator ?: has higher precedence than the assignment operator =. While the inner = is seen as part of the ternary, the rightmost one is not. So the expression parses like this:
((a = b) < 10 ? c = 1 : c) = 0;
The result is that you're trying to assign the value 0 to an expression that is not an lvalue, i.e. a variable name or a dereferenced pointer. You would need parenthesis for it to parse the way you want:
((a = b) < 10) ? (c = 1) : (c = 0);
Since what you're doing is assigning a value to c based on an expression, it can be simplified as follows:
c = ((a = b) < 10) ? 1 : 0;
Or even:
c = ((a = b) < 10);
Translating back to your code:
Counter.WriteOut = (adc.Voltage1[Counter.ADC_ConversionCount] = ADC_readResult(Handler.myAdc, ADC_ResultNumber_1) > 10);
And made more readable by splitting the operations:
adc.Voltage1[Counter.ADC_ConversionCount] = ADC_readResult(Handler.myAdc, ADC_ResultNumber_1);
Counter.WriteOut = (adc.Voltage1[Counter.ADC_ConversionCount] > 10);
I think it should be:
Counter.WriteOut = (adc.Voltage1[Counter.ADC_ConversionCount] = ADC_readResult(Handler.myAdc, ADC_ResultNumber_1)) > 10 ? 1 : 0;
The ?: operator has higher precedence than = operator, so the first expression is interpreted as
(
(adc.Voltage1[Counter.ADC_ConversionCount] = ADC_readResult(Handler.myAdc, ADC_ResultNumber_1)) > 10 ?
Counter.WriteOut = 1 : Counter.WriteOut
) = 0
Therefore, the lefthand expression of = is not modifiable.
Use parenthesis to avoid this:
(adc.Voltage1[Counter.ADC_ConversionCount] = ADC_readResult(Handler.myAdc, ADC_ResultNumber_1)) > 10 ? Counter.WriteOut = 1 : (Counter.WriteOut = 0);
(no parenthesis are needed for Counter.WriteOut = 1 because it is middle of ?: operator and no ambiguity is there)
Because what is assigned are common Counter.WriteOut, I prefer
Coumter.WriteOut = ((adc.Voltage1[Counter.ADC_ConversionCount] = ADC_readResult(Handler.myAdc, ADC_ResultNumber_1)) > 10 ? 1 : 0);
or, using definition of comparision operators of C (it returns 1 for true and 0 for false),
Coumter.WriteOut = ((adc.Voltage1[Counter.ADC_ConversionCount] = ADC_readResult(Handler.myAdc, ADC_ResultNumber_1)) > 10);
Can someone explain why the output of this program is false??
x && y gives 1. Still the output is false.
#include <stdio.h>
int main()
{
int x = 1, y = 2;
if(x && y == 1)
{
printf("true.");
}
else
{
printf("false.");
}
return 0;
}
Because == has a higher precedence than && So first this get's evaluated:
x && (y == 1)
y == 1 // 2 == 1
//Result: false
Which is false and then second:
x && false //1 && false
//Result: false
So the if statement will be false
For more information about operator precedence see here: http://en.cppreference.com/w/cpp/language/operator_precedence
if(x && y == 1)
Is the same as
if( ( x != 0 ) && ( y == 1 ) )
Here,x != 0 is true, but y == 1 is false. And since at least one of the operands of && is false, the condition evaluates to false and the else part executes.
It clearly stated X = 1 & Y = 2;
Now with your expression
X && Y == 1
The expression is evaluated as
Y == 1 (Precedence Rule, Also output is False)
X != 0 (Its True)
Now && is Logical And Operator, so it evaluates to True only if both the parts in expression evaluates to True!!!
It's okay to false, then 2 and 2 and it is different from one.
What you're asking is whether both x and y both are worth 1. If this happens say true but false
Can anyone explain me why this line of code is wrong?
int n = 0, y = 1;
y == 1 ? n = 0 : n = 1;
The error is "Lvalue required as left operand of assignment " for "n=1"
The statement
(y == 1 ? n = 0 : n) = 1;
is interpreted as because n binds with ?: operator due to its higher precedence.
= needs l-value as its left operand while ?: returns an r-value.
Try this instead
y == 1 ? n = 0 : (n = 1);
or
n = y == 1 ? 0 : 1;
You should use :
n = y==1 ? 0 :1
According to the C standard, the behaviour is undefined if an attempt is made to use the result of the conditional operator as an Lvalue.
You can use ternary operator for assigning for example:
int n = 0, y = 1;
n = y == 1 ? 0 : 1;
could some one help. getting error with these 2 lines of code. num_red - count_red = red_pot;// all defined as 0
and
while (count_red = 0 && count_yellow = 0 && count_green = 0 && count_brown = 0 && count_blue = 0 && count_pink = 0)
{
if (count_black = 0)
{
score = score + 7;
printf("Score: %d\n", score);
num_balls = num_balls - 1;
}
}
If that's a C-like language, you need to use == for equality checks, not =. The single = is for assignment so that:
int seven = 7;
int five = 5;
if (seven - five == 2) ...
is okay, but:
int seven = 7;
int five = 5;
if (seven - five = 2) ...
will, even if it compiles, not do what you expect.
You have a classic example in your code. The segment:
if (count_black = 0) blah;
will not execute blah when count_black is zero. It will set count_black to zero and steadfastly refuse to ever execute blah, since the result of count_blah = 0 is 0 (false).
If you want the equality:
num_red - count_red == red_pot
to be true, you need to assign one of those variables (the "unknown" one) based on the other two "known" ones. For example, if num_red and count_red are known, set red_pot with:
red_pot = num_red - count_red;
Alternatively, if red_pot and count_red are known, set num_red with:
num_red = count_red + red_pot;