How to check whether a variable is int or const int in C

1 Answer

0 votes
#include <stdio.h> 

#define is_int_const_int(n) _Generic((&n),  \
            const int *: "const int",       \
            int *:       "int",             \
            default:     "other type")

int main(void)
{
    const int ci = 99;
    int i = 10;
    float f = 3.14;
    
    printf("ci is: %s\n", is_int_const_int(ci));
    printf("i is: %s\n", is_int_const_int(i));
    printf("f is: %s\n", is_int_const_int(f));
    
    return 0;
}
    
/*
run:
 
ci is: const int
i is: int
f is: other type

*/

 



answered Aug 21, 2017 by avibootz

Related questions

1 answer 125 views
4 answers 241 views
3 answers 299 views
1 answer 195 views
195 views asked Aug 31, 2017 by avibootz
1 answer 223 views
2 answers 216 views
...