×

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

Distance Vector Routing Protocol Program in c

A distance-vector routing protocol is one of the foremost instructions of routing protocols in pc conversation principle for packet-switched networks. The hyperlink-nation protocol is the alternative foremost class.The Bellman-Ford set...

4 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.

Multithreading in C interview questions

Q1. What exactly is a thread? Ans: A thread is a brief sequence of instructions intended to be planned and carried out by the CPU apart from the parent process. Q2. Can...

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

Flexible Array Members in a Structure in C

There is flexibility to declare array from c99 generation onwards that declaration of the collections can be made possible without even mentioning its dimension, which means that the displays are...

4 minutes read.

Variable in C

Variable is an identifier that holds data in memory. It is used to identify input data in a program. The value of the variable can change at the time of...

2 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Ftell() Function in C

Ftell(): In File Handling we have some special functions like Ftell(), Fseek(), rewind() etc.. while you are randomly accessing the file these functions play very important role and these functions...

3 minutes read.

Library Functions in C

What are library functions? Library functions are the built-in functions (functions provided by the system) which are stored in the library. The library is the common location where these functions are...

3 minutes read.

Consumer billing system in C

Introduction : Billing has always been taught to do perfectly, we know today's world is full of products, and We all are using multiple products. We are buying and selling various...

9 minutes read.

For Loop in C Programming Examples

The Syntax of the For Loop for (initialization statement; termination condition; modifying (increment/ decrement) statement) {       /* main body of the For loop */     } In for loop, the initialization command...

6 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

Simple hash() function in C

Introduction In this context, we briefly discuss HASH FUNCTION, HASHING or HASH TABLE in C. It is a function used to map data and mapped arbitrary sizes to the fixed-size values. The...

7 minutes read.

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

DSA Program in C

How is a Data Structure Implemented? The foundational terms of a data structure are the following terms. Data may be arranged using data structures to make it easier to use. Each data...

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

Loop Statement in C

Loop statement is used to execute one or more statement repeatedly multiple times. There are three types of loops in C language. Why use loop? We can use loop because it executes a...

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

Nested if-else statement in C

If we use an if-else statement within another if statement in a C program, it is called a nested if-else statement in C. It helps to check the condition inside...

3 minutes read.