×

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

#define in C

#define in C In the C programming language, the preprocessor directive acts an important role within which the #define directive is present that is used to define the constant or the...

3 minutes read.

String literals in C

Constants refer to the fixed values of the program that may not alter during the compiling ad execution. These fixed values are known as literals. In other words, the values...

3 minutes read.

Limitations of Synchronisation and Uses of Static Synchronisation in Multithreading

The multithreading component of java is the element around which the idea rotates as it permits simultaneous execution of at least two program pieces for the most significant usage of...

9 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

Find reverse of an array in C using functions

Introduction: Here, we describe how to find the reverse array in C using functions. Suppose you have an array with n elements. I need to display the elements present in...

3 minutes read.

String Handling functions in C

String :- The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any...

5 minutes read.

Break, continue and goto statement in C

Break statement Break statement is used to break the process of a loop (while, do while and for) and switch case. Syntax: break; Example 1 while(test Expression) { // codes if(condition for break){ break; } // codes } Example 2 For(int it, condition,...

1 minute read.

Difference between If Else and Nested If Else in C

What is the If Else statement in C? In C programs, if else conditions are used to perform some operations based on specific conditions. The operation is only executed if the...

6 minutes read.

Isalnum() function in C

Introduction: The isalnum () is a function used in C programming language. This function checks the passing number or argument is an alphanumeric number or not. The alphanumeric number consists of the...

3 minutes read.

Floor() Function in C

Floor() function is a built-in function in C which is defined in the math.h header file. This function is used to return the nearest integer value which is less than...

4 minutes read.

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.

Reverse a Stack using Recursion in C

In this tutorial we will learn how recursion will be used in this case to reverse a stack. For loops, while loops, do-while loops, and similar constructions are not permitted....

5 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Segmentation Fault in C

Segmentation Fault in C In the C programming language, segmentation fault or segmentation violation or core dump is a condition that the hardware component raises to protect the memory by indicating...

4 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

Positioning of file in C

Positioning() function in c The fseek() function is utilized to change the document position of the stream . The fully extent of value from the source should be one of the...

3 minutes read.

Associativity of Operators in C

What is an Associativity operator? Two operators with equal priority are connected by employing their association when they are present in an expression. There are two different types of associativity: left to rightright...

4 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Loop Questions in C

Question 1: What is For loop syntax? Ans: The syntax that has been used for the ‘for’ loop in C programming language is: for (initialization statement /*for providing a value to variables*/; test...

19 minutes read.