Advance-C/C2/Storage-class-specifiers/English
Title of script: Storage class specifiers in C
Author: Ashwini Patil
Keywords: Video tutorial, storage class, auto, static, extern, register.
|
|
---|---|
Display Slide 1 | Welcome to the spoken tutorial on Storage class specifiers. |
Display Slide 2 | In this tutorial, We will learn about
|
Display Slide 3
System Requirements |
For this tutorial I am using
|
Display Slide 4
Prerequisites
|
To follow this tutorial you should be familiar with C and C++ tutorials.
|
Display Slide 5
Introduction |
I will start with an introduction to storage class specifiers.
|
Display Slide 6 | Syntax:
storage_specifier data_type variable _name |
Display Slide 7
Types |
Types of storage class specifiers are:
|
Display Slide 8
'auto' |
Let us start with auto keyword.
|
Let us see an example.
I have a code file; let us go through it. Note that our filename is auto.c | |
#include<stdio.h>
void increment(void); |
We have declared a function as “increment”. |
int main()
{ increment(); increment(); increment(); increment(); return 0; } |
This is the main function.
In the main function, increment function is called 4 times.
|
void increment(void)
{ auto int i = 0 ; printf ( "%d ", i ) ; i++; } |
Here we have declared variable i as auto int.
It has a local scope.
Value of i is incremented here. |
Press Ctrl+Alt+T | Let us open the terminal by pressing Ctrl+Alt+T keys simultaneously on your keyboard. |
Compile
Type: gcc auto.c -o auto |
Type: gcc auto.c -o auto
Press Enter. |
Execute
Type: ./auto |
Type: ./auto |
The output is zero. | |
Now come back to our program.
Let us initialize the auto variable i above the main function. | |
I will cut this declaration and initialization from here.
And paste it over here. Click on Save | |
gcc auto.c -o auto
./auto |
Let us execute on the terminal.
Press the uparrow key twice. Press Enter |
We get an error:
file-scope declaration of i specifies auto This is because an auto variable is local to the function. We cannot initialize it globally. | |
Delete
global initialization Highlight the global initialization. |
Let us fix the error.
Come back to our program. Delete this; paste it over here. Click on Save and execute on the terminal. |
Press the up arrow key.
Recall the previous command. Press Enter.
./auto Press Enter.
The output is zero.
| |
Now let us see static variable.
Although we have studied about static variable in the previous tutorials. I will explain it here briefly. | |
Display Slide 9
'static'
|
'static' variables are initialized to zero.
They are not destroyed even after program control exits from the block. Value of the variable persists between different function calls. Storage space is CPU memory. |
Point to the program. | Let us see an example.
I will edit the same code file. Come back to our program. |
press ctrl + shft + s keys.
Save the file as static.c |
Press Ctrl + Shft + S keys simultaneously.
Click on Save. |
Change
auto int i =0; to static int i=0; |
Now, I will change the initialization of
Click on Save. |
On the terminal
Type: gcc static.c -o static -o stat |
Let us see what happens.
Execute the file on the terminal. Type: gcc space static.c space -o space stat Press Enter |
Type:
./stat |
Type:
./stat Press Enter |
The output is displayed as:
0, 1, 2, 3
The scope of static variable is local to the function they are defined in.
| |
Display Slide 10
extern variable |
Now let us learn about extern keyword.
|
Let us see an example.
I have a code file; let us go through it. Note that our filename is extern.c | |
#include<stdio.h>
int x = 10 ; |
I have initialized a variable as integer variable x to 10. |
int main()
{ extern int y; |
This the main function.
In the main function I have declared an extern integer variable y. |
printf ( "The value of x is %d \n", x ) ;
printf ( "The value of y is %d",y ) ; return 0; } int y = 50 ; |
Using the printf statements we will display the values of x and y.
This the return statement. |
Highlight int y = 50 ; | We will initalize y to 50 after the main function close. |
On the terminal | Now switch to the terminal and let us see what will be the output. |
Compile
Type: gcc extern.c -o ext |
Type:
gcc extern.c -o ext Press Enter |
Execute
Type: ./ext |
Type: ./ext
Press Enter |
Output | The output is displayed as:
The value of x is 10 The value of y is 50 |
Point to the statements. | As we studied, the value of the extern keyword is throughtout the main program.
We can define it anywhere in the program. Both the statements are justified. |
Now let us move on to register keyword. | |
Display Slide 11 |
|
Let us see an example now.
I have a code file. Let us go through it. Note that the file name is register.c | |
register int i; | Here we have declared register integer variable.
This variable will be directly stored in the register memory. |
for(i=1; i<=5; i++)
printf("n%d\n",i); return 0; } |
This is the for loop that displays the value of i from 1 to 5.
This will display the value of i. |
Type:
gcc register.c -o register Type: ./register |
Let us execute the program and see.
gcc space register.c space -o space register Press Enter.
Press Enter. |
You can see the output is displayed as:
Values stored in register memory 1 2 3 4 5 | |
This brings us to the end of this tutorial.
Let us summarize. | |
Display Slide 12
Summary |
In this tutorial, we learnt-
|
Display Slide 13 | As an assignment,
|
Display Slide 14
|
Watch the video available at the link shown below
It summarises the Spoken Tutorial project If you do not have good bandwidth, you can download and watch it |
Display Slide 15
Spoken Tutorial Workshops |
The Spoken Tutorial Project Team
Conducts workshops using spoken tutorials Gives certificates to those who pass an online test For more details, please write to contact@spoken-tutorial.org |
Display Slide 15
Acknowledgement |
Spoken Tutorial Project is a part of the Talk to a Teacher project
It is supported by the National Mission on Education through ICT, MHRD, Government of India More information on this Mission is available at: http://spoken-tutorial.org\NMEICT-Intro |
This is Ashwini Patil from IIT Bombay.
Thank you for joining. |