How to check a variable is const qualified in C?

How to check a variable is const qualified in C

Do you know the const qualifier? If you’re reading this article, I believe your answer will be Yes.

Here I will not discuss how to use the const keyword and what is the const keyword. But I will discuss how to know a variable is const qualified or not.

➤ How to check a variable is const qualified in C?
➤ What is the way of checking if a variable is constant qualified?
➤ How do you know if a variable is constant in C?

If the above-mentioned questions are coming to your mind, then you are at the right place. Here I will explain with programming how to check a variable is const.

 

C program to check a variable is const qualified:

We will use here the _Generic keyword (generic-selection) to differentiate the const and non-const variables.

Below I have written a macro that will check const and non-const for char, int, and float.  If you want to check other data types, then you have to modify the macro according to your use. So let’s see the macro.

#define __is_constant(X) _Generic((&X), \
        const int *: "a const int", \
        int *:       "a non-const int",\
        const char *: "a const char", \
        char *:       "a non-const char",\
        const float *: "a const float", \
        float *:       "a non-const float",\
        default: "unknown")

 

Note: Macro will only work the compiler supported C11 or above because _Generic Keyword introduce in C11.

 

Check int:

You can see in the below code we are differentiating const and non-const using the above macro. This code will

#include <stdio.h>

#define __is_constant(X) _Generic((&X), \
        const int *: "a const int", \
        int *:       "a non-const int",\
        const char *: "a const char", \
        char *:       "a non-const char",\
        const float *: "a const float", \
        float *:       "a non-const float",\
        default: "unknown")

int main()
{
    const int data1 = 1;
    int data2 = 1;

    //check data1
    printf("Variable data1 is %s\n", __is_constant(data1));

    //check data2
    printf("Variable data2 is %s\n", __is_constant(data2));

    return 0;
}

Output:

Variable data1 is a const int
Variable data2 is a non-const int

 

 

Check char:

Now finding const and non-const of char type.

#include <stdio.h>

#define __is_constant(X) _Generic((&X), \
        const int *: "a const int", \
        int *:       "a non-const int",\
        const char *: "a const char", \
        char *:       "a non-const char",\
        const float *: "a const float", \
        float *:       "a non-const float",\
        default: "unknown")

int main()
{
    const char data1 = 1;
    char data2 = 1;

    //check data1
    printf("Variable data1 is %s\n", __is_constant(data1));

    //check data2
    printf("Variable data2 is %s\n", __is_constant(data2));

    return 0;
}

Output:

Variable data1 is a const char
Variable data2 is a non-const char

 

Similarly, above code, you can check for the float.

 

Now you are thinking about what changes are required if want to check only double.

So don’t worry, as I have mentioned above you only need to modify the macro, like the below example.

#define __is_constant(X) _Generic((&X), \
        const double *: "a const double", \
        double *:       "a non-const double",\
        default: "unknown")

 

Now your macro is ready to check double.

 

Recommended Post

Leave a Reply

Your email address will not be published. Required fields are marked *