TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: Bob Stout
from: Rob Swindell
date: 2003-09-11 15:37:10
subject: Re: Pointers

Re: Re: Pointers
  By: Bob Stout to All on Thu Sep 11 2003 10:51 am

 > > Some compilers will generate a warning, so it's often better to use:
 > >
 > >            if((i=function(parm))==TRUE)  /* no warning */
 > 
 > This seems like a good time to remind folks of one of the oldest C
 > programming "tricks"... When doing comparisons, always put
the constant on
 > the left side of the expression. For inequality and greater/less than
 > comparisons, it makes no difference, but it sure does when testing for
 > equality!
 > 
 > Since using "=" when you mean "==" is one of the
most common mistakes
 > (usually, it's a typo and is even made by experienced C programmers),
 > putting the constant on the left changes the compiler's action from a
 > warning to an error.
 > 
 > if (x == 6)	/* what you mean */
 > 
 > if (x = 6)	/* warning, possible unintended assignment */

Yes, and modern compilers will give you a "possibly incorrect assignment"
warning, bringing this to your attention.

 > if (6 = x)	/* error, assignment to a constant */

So now you have an error instead of a warning. 

I'd rather just watch for warnings and have code that reads logically. :-)

I've worked on large products that use the "constant on the left"
trick all the
time, but it's just not necessary with today's compilers.

                                                  digital man

Snapple "Real Fact" #44:
The bullfrog is the only animal that never sleeps.
--- SBBSecho 2.00-Win32
* Origin: Vertrauen - vert.synchro.net (1:103/705)
SEEN-BY: 633/267 270
@PATH: 103/705 218/903 10/3 106/2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.