How to create a macro that check if a number is odd or even in C

2 Answers

0 votes
#include <stdio.h>

#define EVEN_OR_ODD(num)               \
            if (num & 1)               \
                printf("odd\n", num);  \
            else                       \
                printf("even\n", num); 


int main() {

    int x = 4;

    EVEN_OR_ODD(x);
    
    return 0;
}




/*
run:

even

*/

 



answered Apr 13, 2022 by avibootz
0 votes
#include <stdio.h>

#define IS_ODD(x) (x & 1)

int main()
{
    int num = 12;

    if (IS_ODD(num))
        printf("Odd");
    else
        printf("Even");

    return 0;
}




/*
run:

Even

*/

 



answered Apr 19, 2022 by avibootz

Related questions

1 answer 195 views
1 answer 190 views
1 answer 161 views
1 answer 200 views
1 answer 181 views
...