Hi Guys, Its been a lot of time since my last post. But anyway we are again here for more knowledge sharing. Now we move on to C program Storage Classes.
A storage class defines the scope (visibility) and life-time of variables and/or functions within a C Program.There are four types of storage classes categorized in C:
1. Auto Variables
2. Extern Variables
3. Static Variables
4. Register Variables
Auto Variables
The Auto storage class is the default storage class for all local variables. All the variables declared inside the function which are local to that are Auto Variables.
And Auto Variables are stored in Stack section of C program segment space (Because all functions are stored in Stack section).
void main()
{
int var;
auto int var; //Both are same.
}
It has garbage value as initial/ default value. Scope is local.
Extern Variables
The extern storage class is to give a reference of global variable and it is visible to ALL the program files. The extern keyword is used before a variable to tell compiler that the variable is declared somewhere else.
The extern declaration does not allocate storage for variables, but all it does is point the variable name at a storage location that has been previously defined. You can use extern with a function also to use it from other location.
It has zero value as initial/ default value. Scope is global.
Compile below examples using $ gcc File1.c File2.c and then run ./a.out
Result : 5
Using extern in same file
Result : 10
Static Variables
The static keyword instructs the compiler to persist local variable during the life-time of the program. Instead of creating and destroying a variable each time it comes into and goes out of scope.
Static is initialized only once and remains into existence till the end of the program. The static modifier may also be applied to global variables. When this is done, it causes that variable’s scope to be restricted to the file in which it is declared. Static variables are stored in Data section of C program segment space.
The “static” keyword before a function name makes it static. Access to static functions is restricted to the file where they are declared. Another reason for making functions static can be reuse of the same function name in other files.
Result : i is 4 and counter is 2
i is 5 and counter is 1
i is 6 and counter is 0
Register Variables
register keyword inform the compiler to store the variable in register instead of memory. It has the faster access than normal variable.
It can’t have the unary ‘&’ operator applied to it because it does not have a memory location at all.
register int num;
That’s it for the current Article. I will come back with more topics on Linux Programming. Any Query ? please ask in the comment section.
Libarary functions: usually are just a wrapper function around your system call, they may or may not use system call depanding on the requirement of the function. if library function required something from kernel, then they have to use system call for context switch from user space to kernel space.
C libarary functions are usually buffered I/O functions, they have their own buffer in user space to speed up the read and write requests but when we compare with system call they directly process the request to kernel.
e.g. For system call, one process does write request two times (w.e. write system call) then there are two context switch happens from user space to kernel space consuming more CPU. But in case of library functions (fwrite), when two write request happens by a process, library functions doesn’t call write system call immendiatly, they will buffer the data and once buffer will full from one or more than one system call they will request for write system call and only one context switch will happen…
Additionally library functions check for resource limits, return value and some other tasks also.