Comparing multiple integer values ​​for equality

If I have two integer values, I can just check for equality:

if (a == b) 

If I have three integer values, I could do something like:

 if ((a == b) && (a == c) && (b == c)) 

I have a situation where I have 6 values ​​for checking equality, this approach will contain detailed

Is their best, and more C-idiomatic (c-onic?) Means to achieve this?

in fact, just by writing this, I see that:

 if ((a == b) && (b == c) && (c == d)... ) 

logically normal, i.e. you don't need all combinations like a == b and b == c means a == c

But in terms of clarity, as far as I can get closer:

if (a == b == c) ?

+7
c
source share
4 answers

You cannot get close to a == b == c .

To call experienced C programmers the least unexpected, I think this is the best

 if(a == b && b == c && c == d && d == e && e == f) 

i.e. basically like what you already have, with smaller brackets. I don’t always mind adding parentheses that have nothing to do with the compiler to help people who don’t know all the priority rules, but I think comparisons separated by the && character (without || ) are simple enough that the brackets were more messy than they cost.

If the variable names are long, then all this on one line will not be beautiful, therefore, perhaps

 if(a == b && b == c && c == d && d == e && e == f) 

or it might be better, since duplicating a in each line will be immediately noticeable:

 if(a == b && a == c && a == d && a == e && a == f) 

If you really want it to be compact,

  The p99 preprocessor offers the following: 

 
 #include "p99_for.h" if(P99_ARE_EQ(a,b,c,d,e,f)) 

which looks like John Zwink's answer, but does not require an auxiliary function.

+7
source share

I can show you how to do this in a variable macro, so we can write this:

 if(EQI(a, b, c, d, e, f)) 

Here is the code:

 #include <stdarg.h> #include <stddef.h> #define NUMARGS(...) (sizeof((int[]){__VA_ARGS__}) / sizeof(int)) #define EQI(...) ( \ alleqi(NUMARGS(__VA_ARGS__), __VA_ARGS__)) /* takes count number of int arguments */ int alleqi(size_t count, ...) { va_list va; va_start(va, count); int v0 = va_arg(va, int); for (; count > 1; count--) { if (v0 != va_arg(va, int)) { break; } } va_end(va); return count == 1; } 

Please note that the above only works for int arguments (and possibly of other types whose size is the same as int ), although I support someone accusing me of promoting undefined behavior if I advertise it!).

Thanks to this previous post for figuring out how to count arguments in a variable macro: fooobar.com/questions/33716 / ...

+7
source share

If you want to check six values, one of them will be storing them in an array

 int a[6]; equals = 1; for (int i=0; i<5; i++) { if (a[i] != a[i+1]) { equals = 0; break; } } 
+6
source share

Array-based approaches require changing the structure of the data in the array.

Approaches using a variable macro require an additional call to a function and function (service data).

With the exception of these two approaches, the only thing I see is to write them. I believe that the best way, or a more understandable way, is to take the first variable and compare it with all the other variables. It is understood that either all variables are equal, or at least two variables are unequal:

 if ((a == b) && (a == c) && (a == d) && (a == e) && (a == f) && (a == g) ...) 
+2
source share

All Articles