Javatpoint Logo
Javatpoint Logo

Type Qualifiers in C

Type qualifiers are keywords that can be used to change a data type's behavior in the C programming language. These qualifiers can be used to describe a variable or pointer's constancy, volatility, restrictions, and others.

These type qualifiers are as follows:

1. Const: If any variable is declared as a const in the code and is initialized with a value, its value cannot be furtherly modified in any part of the code. It results in a compile-time error if the programmer attempts to modify the value of such variables, which are declared const.

An example code snippet:

Output:

The code results in a compile time error since the user is trying to modify the value of const variable 'y' after its initialization.

2. Volatile: The keyword "volatile" can be used to describe a variable whose value is subject to sudden change. It is typically used for variables that can be accessed by hardware, updated by several threads or processes, or both. The volatile qualifier informs the compiler that any reads or writes to the variable should not be optimized away since the variable's value might change without the program's permission.

Syntax:

3. Restrict: Use the restrict qualifier to specify that a pointer is an exclusive method of accessing a specific object or area of memory. Assuming that no other pointer may access the same memory region, enables the compiler to generate code that is more effective. In performance-sensitive code, such as numerical algorithms, the restrict qualifier is often used.

An example code that declares a restrict-qualified pointer:

4. _Atomic: When a variable may be read or written atomically, meaning without interference from other threads or processes, it is indicated by this type of qualifier. It is frequently used in multithreaded programming to provide synchronized and reliable data access.

Syntax to declare a variable:

5. _Thread_local: When a variable has thread-local storage duration, which means that it lives separately for each thread in a program, it is indicated by this type of qualifier. Having a private copy of the variable for each thread in this way enables the storage of thread-specific information.

6. _Noreturn: When a function enters an infinite loop or ends the program in some other way (such as by calling the exit() function), it is indicated by this type of qualifier that the function never returns. Writing functions that carry out some clean-up or error handling before ending the program can make use of this.

7. _Alignas: The alignment of a variable, which establishes its memory address boundary, is defined by this type of qualification. It may help to improve memory access and lower cache The desired alignment in bytes is specified as an input to the "_Alignas" qualifier.

Code:

Output:

x=10
y=20
z=30
thread_x=40
Segmentation fault

Explanation:

This program shows how to utilize variables and declares them with all of the appropriate type qualifiers. It should be noted that changing variables with the 'const' and volatile qualifiers may result in warnings or errors, but attempting so with a pointer still works. If not utilized properly, the restricted qualifier can also produce warnings. Finally, the qualifiers '_Noreturn' and '_Thread_local' have an impact on how a function or variable behaves and, if improperly used, can result in warnings or errors.


Next TopicUnsigned int in C





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA