Joined: 05 Sep 2007 Posts: 46 Location: Londrina - Brazil
Conditional Expression Operator ?: error
Posted: Fri Jul 25, 2008 8:50 am
I can't understand why this code generates an error:
Code:
(input(PIN_A1)) ? i = 1 : i = 0;
Error: Line 214(23,24): Expecting :
My compiler version is: 4.017
Can someone help me?
Thanks!!!
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
Posted: Fri Jul 25, 2008 9:24 am
You syntax is wrong, it should be:
Code:
i = (input(PIN_A1)) ? 1 : 0;
Several companies I've worked for don't allow the ?: construction because it often leads to difficult to read code and does not reduce the size of the generated code. The following code fragment does the same and is easier to read:
Code:
if (input(PIN_A1))
i = 1;
else
i = 0;
or in this particular case can even be optimized to:
Code:
i = input(PIN_A1);
As a side note: the compiler versions before v4.030 contain large numbers of bugs and I wouldn't use them.
rhaguiuda
Joined: 05 Sep 2007 Posts: 46 Location: Londrina - Brazil
Posted: Fri Jul 25, 2008 11:04 am
Ckielstra, thanks very much.
The code works great now!
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum