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:
auto static extern register |
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. Then we display value of i using printf. Value of i is incremented. |
Press Ctrl+Alt+T | Let us open the terminal by pressing Ctrl+Alt+T keys. |
Compile
Type: gcc auto.c -o auto |
Type: gcc auto.c -o auto |
Execute
Type: ./auto |
Type: ./auto |
The output is:
0 0 0 0 | |
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 gcc auto.c -o auto
|
We get an error:
auto.c:3:10: 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: gcc auto.c -o auto press Enter Type: ./auto press Enter Yes it is working. The output is 0. This is because we have initialized the value of i as 0. | |
Now let us see static variable.
Although we have studied about static variable in the previous tutorials. I will explain it here briefly. Come back to the slides. | |
Display Slide 9
'static'
|
'static' variables are initialized to 0.
'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.
Let me resize the window. Now I will just change the filename as static Click on Save. |
Change
auto int i =0; to static int i=0; |
Now, I will just change the initialization of the variable i to static int i=0;
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 This is because static variables are global variables. The scope of static variable is local to the function they are defined in. Sentence is lengthy. Reframe to the sentence.Reply to Madhuri (18/07/2014, 11:45): "..." Reframed
Now let us learn about extern keyword Come back to the slides. | |
Display Slide 10
extern variable |
* Scope of extern variable is throughout the main program.The sentences do not convey any meaning. Reply to Madhuri (17/07/2014, 12:47): "..."
|
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.
Let me clear the prompt. |
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:
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 throught out the main program.
We can define it anywhere in the program. Both the statements are justified. |
Now let us move on to register keyword.
We will go back to the slides. | |
Display Slide 11 | Register variables will be accessed faster than normal variables.
They are stored in register memory instead of main memory. Limited number of variaThis sentence does not convey the meaning.Reply to Madhuri (17/07/2014, 13:06): "..." It is understood that we cannot use more than 1000 variables as register variables. Register size is low as mentioned either 16, 32 or 64 bits.bles can be used since register size is very low. (16 bits, 32 bits or 64 bits) |
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.
Let me clear the prompt On the terminal. Type: gcc space register.c space -o space register Type: ./register |
The output is displayed as:
Values stored in register memory 1 Values stored in register memory 2 Values stored in register memory 3 Values stored in register memory 4 Values stored in register memory 5 | |
This brings us to the end of this tutorial.
Let us summarize. | |
Display Slide 12
Summary |
In this tutorial we learnt,
Where is the assignment.
We cannot give assignment as there is no use. Register and extern variables are mostly used in embedded C programming, hardware programming. There these can be understood well.
|
Display Slide 13 | As an assignment,
Write a program, to print the sum of first 5 numbers Declare both the keywords auto and static in the program
|
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. |