EmbDev.net

Forum: ARM programming with GCC/GNU tools Is this C statement valid?


Author: jrmymllr jrmymllr (jrmymllr)
Posted on:

Rate this post
0 useful
not useful
Is this code valid to test if "x" is equal to either condition?

if(x == (CONDITION1 || CONDITION2))
{

    //stuff
}

The compiler (Codesourcery GCC) seems to be ok with it, but is it 
legal/reliable code?

Author: kbuchegg (Guest)
Posted on:

Rate this post
0 useful
not useful
NO
if( ( x == CONDITION1 ) ||
    ( x == CONDITION2 ) )

Author: kbuchegg (Guest)
Posted on:

Rate this post
0 useful
not useful
Having said that:
It is valid in the sense, that it follows the language rules. But it 
does not what you think it should do.

Author: Martin (Guest)
Posted on:

Rate this post
0 useful
not useful
if ( (x == CONDITION1) || (x == CONDITION2) )
{

    //stuff
}


The 'stuff' section will be processed if one of the conditions is true.

Author: mano (Guest)
Posted on:

Rate this post
0 useful
not useful
this
if(x == (CONDITION1 || CONDITION2)) {

    //stuff
}

is equal to this
if((x == CONDITION1) || (x == CONDITION2)) {

    //stuff
}

(only) when
BOOL x = YES;     // must be YES

BOOL CONDITION1 = ...;
BOOL CONDITION2 = ...;

Author: jrmymllr jrmymllr (jrmymllr)
Posted on:

Rate this post
0 useful
not useful
Thanks to everyone for the lightning quick replies.  I've always used:

if( ( x == CONDITION1 ) || ( x == CONDITION2 ) )

but was hoping to use a #define to turn this:

if(x == CONDITION)

into what I proposed by doing something like:

#define CONDITION       (CONDITION1 || CONDITION2)


I'll just go through the code and modify each instance correctly :)

Author: Bruce Graham (Company: Wadia) (papabravo)
Posted on:

Rate this post
0 useful
not useful
The double vertical bar "||" is a relational operator like "<" or ">". 
The single vertical bar "|" is a bit-wise operator.  You can use the the 
single vertical bar to do what you were trying to do in the original 
post.  The bit-wise combination of constants will yield a constant that 
can be used to test multiple conditions simultaneously.

Author: BrEin (Guest)
Posted on:

Rate this post
0 useful
not useful
So, to answer your question "Is this C statement valid?":
Yes it is.
But it works differently like you expect.
The language C don't understand booleans.
It takes numbers to represent true or false.

false == 0;
true  != 0;  // common 1

Your statement:
if(x == (CONDITION1 || CONDITION2))
{

    //stuff
}

checks first the statement within the parenthesizes.
It returns a 1 when CONDITION1 OR CONDITION2 is not 0.
Otherwise it returns a 0.

Then it checks x with the return value.
When x is 0 and both conditions are also 0 the equation returns a 
1.
When x is 1 and at least one condition is not 0 the equation returns 
a 1, too.

When the expression in the if-header is not 0 the if branch will be 
executed. Otherwise not, of course.

All this does not hurt the rules of C.

____________________________

But you purpose was to check if x has the same value like CONDITION1 
OR CONDITION2.
To do this you have to reorder your question.
Has x the same value like CONDITION1 OR has x the same value like 
CONDITION2?

Therefore is the answer of kbuchegg and the others correct.
if( ( x == CONDITION1 ) ||  ( x == CONDITION2 ) )
{
    //stuff
}

This expression returns a 1 when at least one of the statements within 
the parenthesizes return a 1.

regards
BrEin




*************************************
I hope this answer is not "overdressed" and I seam to be a squirt.
I thought the question was caused by missing some knowledge.
Sorry if not.

Author: Fabian Hoemcke (Company: Technische Universität Berlin) (brein)
Posted on:

Rate this post
0 useful
not useful
What is right?
a 1?
or
an 1?

I serios don't know it? :D

Author: Klaus Wachtler (mfgkw)
Posted on:

Rate this post
0 useful
not useful
The 1

Author: Fabian Hoemcke (Company: Technische Universität Berlin) (brein)
Posted on:

Rate this post
0 useful
not useful
Really?
"...it returns the 1..."????

Author: Klaus Wachtler (mfgkw)
Posted on:

Rate this post
0 useful
not useful
there is only one 1
:-)

Author: Fabian Hoemcke (Company: Technische Universität Berlin) (brein)
Posted on:

Rate this post
0 useful
not useful
OK! Thanks!

Author: yalu (Guest)
Posted on:

Rate this post
0 useful
not useful
Fabian Hoemcke write:
> What is right?
> a 1?
> or
> an 1?
>
> I serios don't know it? :D

Since 1 ("one") doesn't start with a spoken vowel, it's "a 1" and not
"an 1". But in this context

> ... the equation returns a 1.

I think it's better style to omit the article:

  ... the equation returns 1.

Klaus Wachtler write:
> there is only one 1
> :-)

I have at least three of them. Would you like to see them?

        1          1          1    ;-)

SNCR

Author: Klaus Wachtler (mfgkw)
Posted on:

Rate this post
0 useful
not useful
at least two of them are just copies.

Author: moi (Guest)
Posted on:

Rate this post
0 useful
not useful
#define cond(x) (CONDITION1==x || CONDITION2==x)

by chance?

Author: Fabian Hoemcke (Company: Technische Universität Berlin) (brein)
Posted on:

Rate this post
0 useful
not useful
Thank you very much yalu!!!

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.