C Question

Electronics Computer Programming Q&A
User avatar
Posts: 3007
Joined: Thu Aug 01, 2002 1:01 am
Location: San Jose CA

Post by haklesup » Wed Apr 23, 2008 3:57 pm

That's not a proper Boolean expression.
It is valid C. stupid, confusing, error prone but valid. The thing about C is that the expression in the if statement evaluates to a value. It can have any type. As long as it evaluates to non-zero the "then" clause of the if statement is taken. That's why you will see things like
A better word than proper might have been "Formal". I was merely indicating that it was not a Boolean expression fit for Boolean algebra but that it must be acceptable syntax for whatever command and language was being used. (For Example, swapping the first . and , in your sentence would certainly have changed its syntax to an insult :razz: )

Assuming the variable types were Binary, would it be correct to state the function in English like this?

IF A doesn't equal B OR (any of those other conditions) then....

(the THEN or Else part of the command was never shown) If you contract your logic functions you should also be able to shorten the equivalent sentence

X=(NOT A AND B) OR (C AND D) OR (E AND F) OR (whatever expression says G>H using only logic gates)

The Truth table for G>H is

0 0 0
0 1 0
1 0 1
1 1 1

But I wasn't quickly able to convert that into gates equivalent or that would be the last term. Anyway that's a lot closer to a Boolean expression than the original command.

If the variable type was integer or float or some other real number type then the interpretation in Boolean wouldn't be very valid. the resulting G>H would be a comparator rather than some gates described by the truth table.

While not very good coding style today when working in a group or making code that will be maintained, such contractions were common in the early DOS days when making smaller EXE and source files was required by small memories. Not knowing the context or vintage of the code example only makes such an observation conjecture though. If tested good and adequately commented so as to explain its intent, then It would be acceptable even today. An accompanying comment would have been essential.

User avatar
Posts: 2050
Joined: Tue Nov 30, 2004 1:01 am
Location: Seattle

Post by philba » Wed Apr 23, 2008 4:19 pm

perhaps we were at cross purposes but I was simply making the point that it is valid C - yes, formal in the sense of grammatically correct.

By the way, my example above shows that the expression can be reorganized to be clearer and generate the same size code, if not the exact same code. If ultra code savings are desired then assembly language is probably a better choice than C. I do think that any code that only focuses on generated size is inherently bad code. Readability and maintainability are incredibly important as well. Code that is is hard to understand is hard to get right.

User avatar
Posts: 3862
Joined: Fri Jan 11, 2002 1:01 am
Location: NewJersey

Post by MrAl » Wed Apr 23, 2008 10:06 pm

Hello again,

Bigglez wrote:Greetings ????,
MrAl wrote:But of course we find ourselves again partly talking about the controversial subject of what is collectively usually referred to as
"Readability", and as i have once long ago stated and will repeat here:

"Readability is in the mind of the beholder".
I wasn't here for your earlier missive, perhaps it's just
as well. I'm not following the thread closely, but it
would appear that one side of the issue is making the
end product more useful and easier to repair or
modify through clarity. The other side is more
interested in intellectual weight lifting...

Comments Welcome!
I think that is a good breakdown of what this thread has become.
Aside from the fact that we have at least been able to convey to the
OP what the original author of the code was intending, we have also
been talking about the pro's and con's of writing code like this.

One additional fact however is that the original code is going to be
faster than any two test sequence of code when A really is not
equal to B, unless my expanded form of the code is used. This
is not always moot, but i would bet that in most modern programs
with the speed of processors these days it will be most of the time.
Thus, overall, i am being persuaded by Philba's arguments which
makes me feel that i should change my vote to "Go with one of
the expanded versions" instead of sticking with the original code.
The tiny loss of speed will probably never be felt unless there is
some very critical section of code.

Just for the record though, the Open Watcom compiler does handle
the code as expected, with the expected short circuiting. As Philba
points out however, it would be wise to well document that code if it
was going to be used, both for what the expected outcome should be
and how the interpretation might change.

As mentioned above though, im changing my vote to "Go with one of the
expanded forms" as in most cases i believe it will be better too now.
LEDs vs Bulbs, LEDs are winning.

Posts: 3
Joined: Mon Sep 07, 2009 1:30 am

Re: C Question

Post by cena » Mon Sep 07, 2009 1:49 am

Hi psycho,
In C-programming language compiler first of all check the content from left to right then after if you can have syntax error then it will give you but you can have run time error(not syntax) then compiler can not give you error.In this code if ( A != B || (C = D, E = F, G > H) ) { }.You can have syntax error in this
if ( A != B || (C = D, E = F, G > H) ) { } condition you can not write comma in if condition it does not work better you can use with logical,relational,etc operators.If you can have any problem with that coding then send me via mail.I will solve your problem.

User avatar
Posts: 3862
Joined: Fri Jan 11, 2002 1:01 am
Location: NewJersey

Re: C Question

Post by MrAl » Tue Oct 06, 2009 6:31 am

Hi there,

Long time since we talked in this thread :smile:

Anyway, i see Philba's point now in that code should be written so that it is compiler independent.
I guess though that it depends how the particular individual wants to code in the first place...
If it were you (the reader) coding, do you want to write code that is compiler independent or do you
want to write code that you know for sure works with the compiler that you normally use every day?

In my experience, it's very hard to write code that works for every compiler anyway. There are more
caveats than just this one little thread can mention. For somewhat big projects, there are always
differences and when speed is an issue the coder usually takes advantage of what the compiler
can do in order to get a speed advantage. This leads to switches like:

If compiler=OW then

I know this is a fact of life and can not be avoided especially since i ran into this very problem not more than
about two weeks ago when someone couldnt run my boost converter time domain simulator that was
compiled with a special language compiler, and that language compiler was itself compiled with i think some
Borland compiler, and it wouldnt run because the run directory the user was using had a very long name
that was some 150 characters long and the writer of the language compiler for some reason only allowed
directory names that were 128 characters or less. What i had to do was fix the C code and then recompile,
only i had to use Open Watcom, not Borland, so i had to check into the compiler differences and change
some code that made a fast jump and the size of the code that the two compilers made was different so
a constant in the code had to be changed manually.

So i guess what i am saying is that we have to live with compiler differences so we have to live with
code that must be different for different compliers. I have to say though that I dont like it either and
wish it could be different.
LEDs vs Bulbs, LEDs are winning.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests