Issue
Sample code to check
#include<stdio.h>
int main(void)
{
const int i = 1;
printf("Variable i is %s\n",
__builtin_constant_p(i) ? "a const variable" : "not a const variable");
return 0;
}
Output :
Variable i is not a const variable
Is __builtin_constant_p()
not the right API to determine whether a variable is of type const
or not?
Solution
You can use Generic selection (since C11):
#include <stdio.h>
#define __is_constant_int(X) _Generic((&X), \
const int *: "a const int", \
int *: "a non-const int")
int main(void)
{
const int i = 1;
printf("Variable i is %s\n", __is_constant_int(i));
return 0;
}
Answered By - nalzok Answer Checked By - David Goodson (WPSolving Volunteer)