Skip to main content

Qualifiers and Modifier in C

Qualifiers or Modifier in C

Qualifiers(Modifier) is used in c programming to modify the property of the variable. In C programming Qualifiers(Modifier) there are 5 group of qualifiers(Modifier) 1st group is having 5 types auto, register, static, extern, typedef. 2nd is having 2 typessigned, unsigned. 3rd one is having again 2 types Short, long. 4th one is Constant 5th is volatile.


GroupQualifiers(Modifier)Default Qualifiers(Modifier)
1
auto, register, static, extern, typedef


auto
2signed, unsignedsigned
3Short, longNot Short,not long
4ConstNot Const
5VolatileNot Volatile

Auto

It defines the storage class of a variable. However, since the default for local variables is auto, you don't normally need to manually specify it.

Register

Variables which are most frequently used in a C program can be put in registers using register keyword
Local variable are stored in register instead of RAM.

Static

Static defined local variables do not lose their value between function calls. In other words they are global variables
Static global variables are not visible outside of the C file they are defined in .
Static functions are not visible outside of the C file they are defined in.


typedef

A typedef declaration is a declaration with typedef as the storage class. The declarator becomes a new type. You can use typedef declarations to construct shorter or more meaningful names for types already defined by C or for types that you have declared. Typedef names allow you to encapsulate implementation details that may change.


Extern

Variables of this storage class are “Global variables .Variables of this storage class are “Global variables .Generally , External variables are declared again in the function using keyword extern .In order to Explicit declaration of variable use ‘extern’ keyword .



Const

Constants are also like normal variables. But, only difference is, their values can’t be modified by the program once they are defined.
  • They refer to fixed values. They are also called as literals.
  • They may be belonging to any of the data type.
  • Syntax:
const data_type variable_name; (or)const data_type *variable_name;

Volatile

When a variable is defined as volatile, the program may not change the value of the variable explicitly.
  • But, these variable values might keep on changing without any explicit assignment by the program. These types of qualifiers are called volatile.
  • For example, if global variable’s address is passed to clock routine of the operating system to store the system time, the value in this address keep on changing without any assignment by the program. These variables are named as volatile variable.
  • Syntax:
volatile data_type variable_name; (or)volatile data_type *variable_name;




Volatile:

A variable should be declared volatile whenever its value could change unexpectedly. In practice, only three types of variables could change:
1Memory-mapped peripheral registers
2 Global variables modified by an interrupt service routine
3 Global variables within a multi-threaded application


2) Constant:

The addition of a 'const' qualifier indicates that the (relevant part of the) program may not modify the
variable


In this 5 group of qualifiers(Modifier) having the different type of property. In same group only one qualifiers we can use at a time.
Example :
#include<stdio.h>
int main()
{
auto int a=5;
printf(“%d”,a);
}

Output is 5

#include<stdio.h>
int main()
{
auto static int a=5; // or register static int a=5 at time same qualifiers we can't use
printf(“%d”,a);
}

output is error

But we can use different group qualifiers at a time
example :
#include<stdio.h>
int main(){
short unsigned volatile const int a=5;
printf(“%u”,a)
 return 0;
}



Comments

Popular posts from this blog

CAN INTERVIEW QUESTIONS

RTOS Real time operating system interview questions

CAN INTERVIEW QUESTIONS 2

What is UDS protocol

Memory mapping in c

TOP IOT PLATFORMS