Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple if statement question

Status
Not open for further replies.

Harrington

Programmer
Sep 18, 2002
4
US
This may be trivial, but something I haven't seen before... in some existing code I came across a statement (where temp_calc is a char variable):

if ( !(temp_calc, "MULTI_TEST") )...

What does the "(,)" operator do? Is it legal with two char types as shown in the above example?

Thanks in advance!
 
if ( !(temp_calc, "MULTI_TEST") )...

it is probably a coding bug. It is always true since !"MULTI_TEST" will never be 0. The comma is a statement separator like the semi-colon. In this case it takes the last expression after the comma. It is legal because C is an expression language. You see the use of the comma in the for statement

for (i = 0, j = 99; i > aaa && j < 0; i++, j--) ...

The very common comma coding bug is when people move from Pascal to C. Instead of

x[5][6]

they code

x[5,6]

which is perfectly legal: it just returns x[6].
 
An other possibility is the exclamation sign(!) has been redefined by the preprocessor to a function or a Macro. That makes not to much sense, but some programmers are very creative.

hnd
hasso55@yahoo.com

 
Thanks guys!! I wasn't sure how it would evaluate (and why it compiled), but this makes perfect sense. I'm sure what they meant was:

if ( !strcmp(temp_calc, &quot;MULTI_TEST&quot;) )...

This wouldn't be the first bug I've seen in this code! At least now I can see what the effect of the current expression is on the downstream code.

Thanks again!

 
I don't know about you but I find

if (!strcmp(...)

a bit confusing as I initially read it to mean the strings are not equal (even after 15 years of C). Basically, the ! throws me. I normally prefer to code as

if (strcmp (...) == 0)

 
Agreed 100%! I always err on the side of readability. In fact, some of this code I'm updating was full of &quot;negative logic&quot; which I've replaced. I don't like to have to stop and think about each expression when going through the code... I always use &quot;if (strcmp (...) == 0)&quot;...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top