Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a variable is of "const" qualifier type in C?

Tags:

c

gcc

constants

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?

like image 245
Sandeep Avatar asked Apr 13 '16 02:04

Sandeep


People also ask

What is const type qualifier in C?

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.

Where may a const type qualifier appear?

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.

What is a const variable in C?

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.

Is const a variable type?

The const makes a variable a constant where its value cannot be changed.

What is the use of qualifier const in C++?

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 ).

What is the use of const in C?

'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.

What are the different types of type qualifiers in C++?

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.

What is the meaning of volatile qualifier in C?

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. ...


2 Answers

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;
}
like image 54
nalzok Avatar answered Nov 15 '22 04:11

nalzok


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.

like image 27
too honest for this site Avatar answered Nov 15 '22 05:11

too honest for this site