Type Qualifiers in C language

C Language | Type Qualifiers: In this tutorial, we are going to learn about the various type qualifiers in C programming language, declaration, and access of the variables using the type qualifiers.
Submitted by IncludeHelp, on June 26, 2020

Type qualifiers are the keywords that can be prepended to variables to change their accessibility i.e. we can say type qualifiers are used to change the properties of variables.

Types of type qualifiers

There are two Type Qualifiers variable in C programming language,

  1. const qualifier
  2. volatile qualifier

1) const qualifier

The const qualifier is used to declare a variable to be read-only (constant), its value may not be changed, and it can be declared by using const keyword. It helps to prevent accidental change of the values.

To declare a constant, include the const keyword before or after the data type. Syntax to declare a constant is,

const data_type constant_name = value;
or 
data_type const constant_name = value;

Example:

#include <stdio.h>

int main()
{
    // first method
    const float pi = 3.14f;

    // second method
    char const default_string[] = "None";

    // printing the values
    printf("pi = %f\n", pi);
    printf("default_string = %s\n", default_string);

    return 0;
}

Output:

pi = 3.140000
default_string = None

2) volatile qualifier

The volatile qualifier is used to declare a variable that can be changed explicitly i.e. it tells the compiler that the variable's value may change at any time. It is very useful for embedded programming to keep the updated value that can be updated from the various interrupts. To declare a volatile variable, we use the volatile keyword.

To declare a volatile variable, include the volatile keyword before or after the data type. Syntax to declare a volatile variable is,

volatile data_type variable_name;
or 
data_type volatile variable_name;

Example:

#include <stdio.h>

int x = 0;
volatile int y = 0;

int main()
{
    y = 0;

    // Here, the compiler optimizes the code and
    // 'else' part will be optimized because the
    // variable (x) will never be other than 0
    if (x == 0) {
        printf("x is zero\n");
    }
    else {
        printf("x is not zero\n");
    }

    // Here, the compiler never optimize 'else' part
    // because the variable (y) is a volatile variable
    if (y == 0) {
        printf("y is zero\n");
    }
    else {
        printf("y is not zero\n");
    }

    return 0;
}

Output:

x is zero
y is zero





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.