×

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type qualifiers are used to change the properties of variables. In other words, the qualifiers are the keywords that are applied to the data types or the type modifiers in C.

Types of type qualifiers

There are three types of type qualifier variables in the C programming language.

  • const
  • volatile
  • restrict

Type qualifiers are basically the keywords that are used to modify the properties of the existing variables.

1. Const qualifier

  • The const qualifier is used to declare a variable to be read only (constant), the value may not be changed, and it can be declared using the keyword const.
  • This qualifier will help to prevent accidental changes in the values.
  • In order to declare a const qualifier, include the keyword const before or after any data type.

Syntax

The syntax for declaring a const qualifier is:

 const data-type constant-name = value;
 Or
 data-type const constant-name = value; 

Use the const type qualifier to qualify an object whose value cannot be changed at any given time.

The objects qualified by the ‘const’ keyword cannot be modified, i.e., an object declared as a ‘const’ cannot serve as an operand in an operation that changes its value in further operations.

 int const *p_ci;      /* Pointer to constant int*/
 int const (*p_ci);   /* Pointer to constant int*/
 int *const cp_i;     /* Constant pointer to int*/
 int (*const cp_i);   /*Constant pointer to int*/
 int volatile vint;     /* Volatile integer*/ 

E.g.: 

 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 int main()
 {
 const float pi = 3.14;
 printf (“The value of pi is = %f \n”, pi);
 return 0;
 } 

Output

Type qualifiers in C
  • The const qualifier can be used to qualify any data type, including the single member of a structure or a union.
  • If const is specified when declaring an aggregate type, all the aggregate type members are treated as objects qualified with const. When the keyword const is used to qualify a member of any aggregate type, only that member will be qualified.

2. Volatile qualifier

  • A volatile qualifier is used to declare a variable that can be changed at any point of time and explicitly, i.e., it always tells the compiler that the variable’s value may change at any time.
    • It will be useful for embedding programming in order to keep the updated value that can be changed from various interrupts.
    • To declare a volatile variable, the keyword ‘volatile’ is used.
    • In order to declare a volatile variable, include the keyword volatile before or after the data type.

Syntax

The syntax to declare ta volatile variable is as follows:

 volatile data-type variable-name;
 Or
  data-type volatile variable-name; 

An object without the volatile specifier will not tell the compiler to perform the optimizations. It indicates that the compiler can apply any optimizations depending on the program context and compiler optimization levels.

E.g.:

 #include <stdio.h>
 #include <conio.h>
 #include <string.h>
 int x = 0;
 volatile int y = 0;
 int main()
 {
 y = 0; /* the compiler optimizes the code and else will be optimized
 because the variable(x) will never be other than 0*/
 if (x == 0)
 {
 printf (“The value of the variable x is zero \n”);
 }
 else
 {
 printf (“The value of the variable x is not zero \n”);
 }
 if (x == 0)
 {
 printf (“The value of the variable y is zero \n”);
 }
 else
 {
 printf (“The value of the variable y is not zero \n”);
 }
 return 0;
 } 

Output

Type qualifiers in C

Optimizations that are defeated by using the volatile qualifier and can be categorized as follows:

  • Optimizations alter an object's duration, such as cases where referencing to an object is shifted or moved to another part of the program.
  • Optimizations alter an object's locality, such as cases where a variable serving as a loop counter can be stored in a register to save the cost of doing any memory reference.
  • Optimizations alter an object's existence, such as loop induction to eliminate the variable reference.

3. Restrict qualifier

  • The new type of qualifier introduced to C99 can be applied to pointer declarations.
  • It qualifies the pointer, not what it points at.
  • It is an optimization hint that no other pointer in the present scope refers to the exact location.
  • It acts as a contract between a developer and a compiler. If you do alias a pointer marked with restrict, then the result is undefined.

E.g.:

 #include <stdio.h>
 void my_function(int* x, int* y, int* restrict z) {
    *x += *z;
    *y += *z;
 }
 main(void) {
    int x = 10, y = 20, z = 30;
    my_function(&x, &y, &z);
    printf("%d %d %d", x, y, z);
 } 

Output

Type qualifiers in C

Marking union members as restrict will tell the compiler that only some of the options can be accessed in scope, allowing the compiler to optimize the access to members.


Related Topics

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

For loop in C Programming

In the C programming language, the for loop is used to repeatedly iterate over a set of instructions or a section of code. It is frequently used to traverse array-based...

3 minutes read.

Structure in C

Why Structure came into the picture? In the actual world, we deal with entities that are collections of objects rather than tiny bits of data like integers, characters, floats, etc. So,...

4 minutes read.

Find a subarray with a given sum.

Find a subarray with a given sum. The simple solution is to recognize all subarrays one by one and to check each subarray's sum. The quick solution follows the following program. Algorithm: From...

4 minutes read.

Queue implementation in C

In the C programming language, the queue is the abstract concept that is similar to stacks. However, it is the part of the data structures that work opposite to that...

4 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

First Fit Program in C

This is one of the easiest ways to allocate memory. The main goal is to divide the memory into several fixed sizes. In C, there is only one process for...

3 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Const vs Volatile in C

Introduction Qualifiers are nothing but keywords which are used to modify the properties of a variable. Const and Volatile keywords are qualifiers in C. The Const qualifier is applied to the...

4 minutes read.

Length of an Array Function in C

In C, there is no built-in function to get the length of an array. However, there are a few ways you can determine the length of an array. It is necessary...

15 minutes read.

Passing Array to Function in C

Need to pass Arrays? The need to pass arrays to a function arises when we need to pass a list of values to a given function. During the course of our programming...

4 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

How to use atoi() function in C

Introduction: The atoi() is a function used in C programming language. This function converts string characters to the integer value. The atoi() function converts the input string to the return type of...

4 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

Armstrong program in C using function

In this article we will learn how to find Armstrong of a number using function in C. An Armstrong number is a three-digit number that is the sum of its separate...

1 minute read.

#error #pragma in C

#error, #pragma in C #error, also known as the error directive in the C language. It will not allow you to make any compilation fail and immediately issues a statement which...

4 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Bigint (BIG INTEGERS) in C with Example

The maximum number of digits that a long, long int can have in C/C++ is 20. The issue is how to store the 22-digit number, which is difficult to do...

9 minutes read.