If the num
can be compared in a range like 1 to 5
can simply write
if (num >= 1 && num <= 5)
{
///.....
}
This is mentioned in other comments and answers. But if the numbers you need to compare are not in a range then the code can be complicated. For me, the simpler way is :
if (num == 1 || num == -2 || num == 3 || num == 10 || num == 5)
{
//......
}
But there is another way to write code like SQL(mentioned in the question). Using variable argument can implement a function :
is_equal(total number of arguments with num variable, num , list of numbers need to compare ..))
This can provide similar code to if (num in 1,2,3,4,5)
#include <stdio.h>
#include <stdarg.h>
int is_equal(int count,...) {
va_list valist;
int ret = 0 ;
int num ;
/* initialize valist for num number of arguments */
va_start(valist, count);
/* access all the arguments assigned to valist */
for (int i = 0; i < count; i++) {
if(i == 0) num = va_arg(valist, int) ;
else{
if(num == va_arg(valist, int))
ret = 1 ;
}
}
/* clean memory reserved for valist */
va_end(valist);
return ret;
}
int main() {
int num = 12 ;
printf("%d\n" , is_equal(5 ,num , 1, 2, 3, 4)) ;
printf("%d\n" , is_equal(4 ,num, 1, 2, 3)) ;
printf("%d\n" , is_equal(6 ,num, 1, 2, 3, 4, 12)) ;
printf("%d\n" , is_equal(8 ,num, 1, 2, 3, 4, 7, -14, 12)) ;
printf("%d\n" , is_equal(9 ,num, 1, 2, 3, 4, 7, -14, -12 , 120)) ;
}
Output :
0
0
1
1
0