Reputation: 2214
I need some confirmation. I always get correct o/p but someone told me following expression will not work
Thanks in advance.
#define a 11
#define b 12
#define c 13
// I want if array[i] has values of any of these then do something
if( array[i] == (a) ||(b) ||( c))
// some function
else
printf("no match");
Upvotes: 0
Views: 425
Reputation: 3873
What you're doing is or'in the result of the boolean evaluation array[i]==a
directly towards b
, c
.
In other words, ( (array[i] == a) || (b) || (c) )
is effectively what you're doing - probably not what you intended!
You will need to evaluate the boolean expressions separately:
(array[i] == a) || (array[i] == b) ...
Upvotes: 2
Reputation: 225282
"Someone" is correct. You need to use:
if ((array[i] == a) || (array[i] == b) || (array[i] == c))
Your program does not always produce correct output. Try it with any i
not equal to 11, 12, or 13.
Upvotes: 0
Reputation: 2019
Replace your code with this:
if( array[i] == a || array[i] == b || array[i] == c)
Each part of the boolean condition must be a complete expression. While what you wrote is valid C code, it doesn't achieve what you want: you need to make an array element comparison in every part.
Upvotes: 1
Reputation: 7435
if (array[i] == a || array[i] == b || array[i] == c){
...
}
I do wish sometimes that you could say if (array[i] == [a, b, c])
or something to that effect.
Upvotes: 2