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?
The const qualifier explicitly declares a data object as something that cannot be changed. Its value is set at initialization. You cannot use const data objects in expressions requiring a modifiable lvalue. For example, a const data object cannot appear on the lefthand side of an assignment statement. C only.
In a function declaration, the keyword const may appear inside the square brackets that are used to declare an array type of a function parameter. It qualifies the pointer type to which the array type is transformed.
The const keyword allows a programmer to tell the compiler that a particular variable should not be modified after the initial assignment in its declaration.
The const makes a variable a constant where its value cannot be changed.
The qualifier const can be applied to the declaration of any variable to specify that its value will not be changed ( Which depends upon where const variables are stored, we may change the value of const variable by using pointer ).
'Const' Qualifier in C. Two type qualifiers available in C are 'const' and 'volatile'. 'Const' qualifier will impose a restriction on the variable, such a way that its value can't be changed or modified. The main use of 'const' is to define constants in your program; so that it's values can't be changed.
In that list, const and volatile are type qualifiers, the rest are type specifiers. Various combinations of type specifiers are permitted: char signed char, unsigned char int, signed int, unsigned int short int, signed short int, unsigned short int long int, signed long int, unsigned long int float A few points should be noted.
Qualifier in C (Const and Volatile) 1 Qualifier in C 2 Introduction. ... 3 Const or Constant (qualifier in c) The const keyword in a declaration establishes a variable whose value cannot be modified by assignment or by incrementing or decrementing. 4 Volatile (qualifier in c) VOLATILE means: – UNSTABLE, UNPREDICTABLE…etc. ... 5 Conclusion. ...
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;
}
RTFineManual:
You can use the built-in function __builtin_constant_p to determine if a value is known to be constant at compile time ..."
(emphasis mine) Note this is a gcc compiler extension, thus not standard compliant.
C does not have symbolic constants other than enum-constants. const
qualified objects are still variables. Thus the test fails. See the manual for a typical application.
Notes:
Assuming the code shown is just an example, your question looks like an XY-problem. C is statically typed, thus at the point you use such a construct the full type is well known. The only way to have something function-like would be a macro. But hiding different behaviour in a macro will cause confusion by the reader of the code. She has to remember this difference for every invokation.
Instead use two functions with the different behaviour and name them accordingly. As the caller know the type he can use the correct function and any reader of the code (including you some weeks later!) will instantly know about the difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With