C pitfall

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

C pitfall

Post by Henk »

Just found a bug. Maybe I posted it before.

Code: Select all

a + b ? c : d + 
f + g 

is not the same as

a + (b ? c : d) + 
f + g 

I think I will always place parenthesis when using ?: in the future for this is certainly not the first time.

Don't ask me what happens if + would be &&.
User avatar
Lasse Hansen
Posts: 27
Joined: Wed May 28, 2008 1:07 pm
Location: Porsgrunn, Norway

Re: C pitfall

Post by Lasse Hansen »

Henk wrote:Just found a bug. Maybe I posted it before.

Code: Select all

a + b ? c : d + 
f + g 

is not the same as

a + (b ? c : d) + 
f + g 

I think I will always place parenthesis when using ?: in the future for this is certainly not the first time.

Don't ask me what happens if + would be &&.
Try using this:

Code: Select all

Operator Preceedence in C (C++ removed)
(3)----------------------------------		(9)----------------------------------
++			increment                       ==			equal to
--                                          !=
+			unary plus                      (10)---------------------------------
-                                           &			bitwise AND
!			logical NOT                     (11)---------------------------------
~			bitwise NOT                     ^			bitwise XOR
(type)		cast                            (12)---------------------------------
*			indirection, not multiply       |
&			address of                      (13)---------------------------------
sizeof                                      &&
new                                         (14)---------------------------------
delete                                      ||
(5)----------------------------------       (15)---------------------------------
*			multiplication                  ?:
/                                           (16)---------------------------------
%			modulo                          =			direct assignment
(6)----------------------------------       +=
+			addition                        -=
-                                           *=
(7)----------------------------------       /=
<<			bitwise left shift              %=
>>                                          <<=
&#40;8&#41;----------------------------------       >>=
<			less than                       &=
<=                                          ^=
>                                           |=
>=                                          &#40;18&#41;---------------------------------
											,			comma
											-------------------------------------
											
&#123;	++	--	+	-	!	~	&#40;type&#41;	*	&	sizeof	new	delete	&#125;
&#123;	*	/	%	&#125;	
&#123;	+	-	&#125;	
&#123;	<<	>>	&#125;
&#123;	<	<=	>	>=	&#125;	
&#123;	==	!=	&#125;	
&#123;	&	&#125;	&#123;	^	&#125;	&#123;	|	&#125;	&#123;	&&	&#125;	&#123;	||	&#125;
&#123;	=	+=	-=	*=	/=	%=	<<=	>>=	&=	^=	|=	&#125;
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: C pitfall

Post by Henk »

Ok this means

Code: Select all

a + b ? c &#58; d + 
 f + g 

 is the same as 

( a + b&#41; ? c &#58; &#40;d + 
 f + g )
Am I right ? And the same holds if + would be && or ||
User avatar
Lasse Hansen
Posts: 27
Joined: Wed May 28, 2008 1:07 pm
Location: Porsgrunn, Norway

Re: C pitfall

Post by Lasse Hansen »

Henk wrote:Ok this means

Code: Select all

a + b ? c &#58; d + 
 f + g 

 is the same as 

( a + b&#41; ? c &#58; &#40;d + 
 f + g )
Am I right ? And the same holds if + would be && or ||
Yes, both + and &&, || has higher precedence than ?:

One mistake I have made often is that

Code: Select all

if &#40;a & b == c&#41;
is different from

Code: Select all

if (&#40;a & b&#41; == c&#41;
Rein Halbersma
Posts: 741
Joined: Tue May 22, 2007 11:13 am

Re: C pitfall

Post by Rein Halbersma »

Decent compilers warn against missing parentheses with bitwise operators.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: C pitfall

Post by bob »

Henk wrote:Just found a bug. Maybe I posted it before.

Code: Select all

a + b ? c &#58; d + 
f + g 

is not the same as

a + &#40;b ? c &#58; d&#41; + 
f + g 

I think I will always place parenthesis when using ?: in the future for this is certainly not the first time.

Don't ask me what happens if + would be &&.
You need to study the operator precedence chart for C, then this ceases to be a trap.
User avatar
lucasart
Posts: 3232
Joined: Mon May 31, 2010 1:29 pm
Full name: lucasart

Re: C pitfall

Post by lucasart »

Operator precedence is simple: either you know the rules or you don't, but NEVER assume or guess!

BTW, this is not just for operator precedence…
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
kbhearn
Posts: 411
Joined: Thu Dec 30, 2010 4:48 am

Re: C pitfall

Post by kbhearn »

Precedence is easy, always use brackets where it's not obvious - makes things more readable anyways.

&& and || are the really fun ones. in order to support forms like:

if (ptr && (*ptr > 0)) { ... }

&& actually guarantees that the right side of the operator will not be evaluated at all if the left side is false. This of course could be confusing as heck if you're doing a long list of function calls and want to check if they all return true and then wonder why the last one never gets called :)
Henk
Posts: 7216
Joined: Mon May 27, 2013 10:31 am

Re: C pitfall

Post by Henk »

kbhearn wrote:Precedence is easy, always use brackets where it's not obvious - makes things more readable anyways.

&& and || are the really fun ones. in order to support forms like:

if (ptr && (*ptr > 0)) { ... }

&& actually guarantees that the right side of the operator will not be evaluated at all if the left side is false. This of course could be confusing as heck if you're doing a long list of function calls and want to check if they all return true and then wonder why the last one never gets called :)
Expressions with many brackets are difficult to edit. Best is to split expressions. But I don't know if compiler will optimize them.

You easily get these bugs when substituting expressions back in their parent using copy paste (in a hurry) but forget to add brackets.