Difference between revisions of "Advance-C/C2/Storage-class-specifiers/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Title of script''': '''Storage class specifiers in C''' '''Author: '''Ashwini Patil '''Keywords: '''Video tutorial,''' storage class, auto, static, extern, register.'''...")
 
 
Line 24: Line 24:
 
* '''extern keyword'''
 
* '''extern keyword'''
 
* '''register keyword '''with the help of examples.
 
* '''register keyword '''with the help of examples.
 
 
  
 
|-
 
|-
Line 35: Line 33:
 
* '''Ubuntu Operating system version 11.10''' and  
 
* '''Ubuntu Operating system version 11.10''' and  
 
* '''gcc Compiler version 4.6.1 on Ubuntu'''
 
* '''gcc Compiler version 4.6.1 on Ubuntu'''
 
 
  
 
|-
 
|-
Line 48: Line 44:
  
  
If not, for relevant tutorials please visit''' spoken hyphen tutorial dot org.'''
+
If not, for relevant tutorials please visit our website, which is as shown.
  
 
|-
 
|-
Line 54: Line 50:
  
 
'''Introduction'''
 
'''Introduction'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will start with an introduction to storage class specifiers.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will start with an introduction to '''storage class specifiers'''.
 
+
* Specifiers tell the compiler where to store a variable.
+
* How to store the variable.
+
* What is the initial value of the variable.
+
* Life time of the variable.
+
 
+
  
 +
* '''Specifiers''' tell the compiler where to store a '''variable'''.
 +
* How to store the '''variable'''.
 +
* What is the initial value of the '''variable'''.
 +
* Life time of the '''variable'''.
  
 
|-
 
|-
Line 75: Line 69:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Types of storage class specifiers are:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Types of storage class specifiers are:
  
'''auto '''
+
*'''auto '''
  
'''static '''
+
*'''static '''
  
'''extern '''
+
*'''extern '''
  
'''register '''
+
*'''register '''
  
 
|-
 
|-
Line 89: Line 83:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with '''auto keyword.'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us start with '''auto keyword.'''
  
* Auto keyword declares an automatic variable.
+
* '''Auto keyword''' declares an '''automatic variable'''.
 
* It has a local scope.
 
* It has a local scope.
* keywords are not initialized automatically.
+
* '''Keywords''' are not initialized automatically.
* You should explicitly initialize keywords while declaring
+
* You should explicitly initialize '''keywords''' while declaring
* Storage space of keywords is CPU memory.
+
* Storage space of '''keywords''' is '''CPU memory'''.
 
+
 
+
  
 
|-
 
|-
Line 101: Line 93:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example.
  
I have a code file let us go through it.
+
I have a code file; let us go through it.
  
 
Note that our filename is '''auto.c'''
 
Note that our filename is '''auto.c'''
Line 110: Line 102:
 
void increment(void);  
 
void increment(void);  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have declared a function as “'''increment”.'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We have declared a function as “'''increment”.'''
 
 
 
  
 
|-
 
|-
Line 130: Line 119:
  
 
}  
 
}  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the main function.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the '''main function.'''
  
In the main function, increment function is called '''4''' times.
+
In the '''main function''', '''increment function''' is called '''4''' times.
  
  
Line 155: Line 144:
  
 
It has a local scope.
 
It has a local scope.
 +
  
 
Then we display value of '''i '''using''' printf.'''
 
Then we display value of '''i '''using''' printf.'''
  
Value of '''i''' is incremented.
+
Value of '''i''' is incremented here.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+Alt+T'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Press '''Ctrl+Alt+T'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us open the terminal by pressing '''Ctrl+Alt+T''' keys.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us open the terminal by pressing '''Ctrl+Alt+T''' keys simultaneously on your keyboard.
  
 
|-
 
|-
Line 169: Line 159:
 
'''Type: gcc auto.c -o auto'''
 
'''Type: gcc auto.c -o auto'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type:''' gcc auto.c -o auto'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type:''' gcc auto.c -o auto'''
 +
 +
Press '''Enter'''.
  
 
|-
 
|-
Line 178: Line 170:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is zero.
 
+
'''0'''
+
 
+
'''0'''
+
 
+
'''0'''
+
 
+
'''0'''
+
  
 
|-
 
|-
Line 192: Line 176:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now come back to our program.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now come back to our program.
  
Let us''' initialize''' the '''auto '''variable''' i '''above the''' main function.'''
+
Let us''' initialize''' the '''auto variable i '''above the''' main function.'''
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will cut this declaration and initialization from here.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I will cut this declaration and '''initialization''' from here.
  
 
And paste it over here.
 
And paste it over here.
Line 206: Line 190:
  
 
'''./auto'''
 
'''./auto'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute on the terminal.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute on the '''terminal'''.
  
 
Press the uparrow key twice.
 
Press the uparrow key twice.
  
 
Press '''Enter'''
 
Press '''Enter'''
 
'''gcc auto.c -o auto'''
 
 
 
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We get an error''':'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We get an error:
  
'''auto.c:3:10: error: file-scope declaration of i specifies auto '''
+
'''file-scope declaration of i specifies auto '''
  
This is because an '''auto variable''' is local to the function.
+
This is because an '''auto variable''' is '''local''' to the '''function'''.
  
We cannot initialize it globally.
+
We cannot '''initialize''' it globally.
  
 
|-
 
|-
Line 237: Line 216:
 
Come back to our program.
 
Come back to our program.
  
Delete this paste it over here.
+
Delete this; paste it over here.
  
Click on '''Save '''and execute on the terminal.
+
Click on '''Save '''and execute on the '''terminal'''.
  
 
|-
 
|-
Line 245: Line 224:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Press the up arrow key.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Press the up arrow key.
  
Recall the previous command:
+
Recall the previous command.
  
'''gcc auto.c -o auto'''
+
Press '''Enter'''.
  
'''press Enter'''
 
  
 
Type:  
 
Type:  
Line 255: Line 233:
 
'''./auto'''
 
'''./auto'''
  
'''press Enter'''
+
Press '''Enter'''.
  
Yes it is working.
 
  
The output is 0.
+
Yes it is working!
  
This is because we have initialized the value of i as 0.
+
The output is zero.
 +
 
 +
 
 +
This is because we have '''initialized''' the value of '''i''' as zero.
  
 
|-
 
|-
Line 270: Line 250:
  
 
I will explain it here briefly.
 
I will explain it here briefly.
 
Come back to the slides.
 
  
 
|-
 
|-
Line 280: Line 258:
  
  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| ''''static'''' variables are '''initialized''' to''' 0.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| ''''static'''' variables are '''initialized''' to''' zero.'''
  
'They are not destroyed even after program control exits from the block.  
+
They are not destroyed even after program control exits from the '''block'''.  
  
Value of the variable persists between different function calls.
+
Value of the '''variable''' persists between different '''function calls'''.
  
Storage space is CPU memory.
+
Storage space is '''CPU memory'''.
  
 
|-
 
|-
Line 295: Line 273:
  
 
Come back to our program.
 
Come back to our program.
 
 
 
  
 
|-
 
|-
Line 303: Line 278:
  
 
Save the file as''' static.c'''
 
Save the file as''' static.c'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Press '''ctrl + shft + s keys simultaneously.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Press '''Ctrl + Shft + S keys simultaneously.'''
  
Let me resize the window.
 
  
Now I will just change the filename as '''static'''
+
Now I will just change the filename as '''static'''.
  
 
Click on''' Save.'''
 
Click on''' Save.'''
Line 319: Line 293:
  
 
'''static int i=0;'''
 
'''static int i=0;'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, I will just change the '''initialization''' of the variable '''i''' to '''static int i=0;'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now, I will change the '''initialization''' of  
 +
*the '''variable i''' to '''static
 +
*int i equal to zero'''
  
Click on''' save'''
+
Click on''' Save'''.
  
 
|-
 
|-
Line 331: Line 307:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see what happens.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see what happens.
  
Execute the file on the terminal.
+
Execute the file on the '''terminal'''.
  
 
Type:
 
Type:
Line 353: Line 329:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as:
  
0  
+
0, 1, 2, 3
  
1
 
  
2
+
This is because '''static variables''' are '''global variables'''.
  
3
+
The scope of '''static variable''' is '''local''' to the '''function''' they are defined in.
  
This is because static variables are global variables.
 
  
The scope of static variable is local to the function they are defined in.
+
They do not lose their value between '''function calls'''.
 
+
Sentence is lengthy.
+
 
+
Reframe to the sentence.''Reply to Madhuri (18/07/2014, 11:45): "..."''
+
 
+
Reframed
+
 
+
 
+
 
+
 
+
They do not lose their value between function calls
+
 
+
Now let us learn about '''extern keyword'''
+
 
+
Come back to the slides.
+
  
 
|-
 
|-
Line 384: Line 343:
  
 
'''extern''' variable
 
'''extern''' variable
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| * Scope of '''extern '''variable is throughout the main program.The sentences do not convey any meaning.<br/>
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us learn about '''extern keyword'''.
''Reply to Madhuri (17/07/2014, 12:47): "..."''<br/> It is very clear that the variable scope is throughout the main program.<br/> This is the advance tutorial we have already covered the basic for scope of variables.
+
* Definition for '''extern''' variable might be anywhere in the C program.
+
* extern variables are initialized to 0 by default.
+
* They can be accessed by all functions in the program
+
* These are stored in CPU memory.
+
 
+
  
 +
* Scope of '''extern variable''' is throughout the main program.
 +
* Definition for '''extern variable''' might be anywhere in the '''C''' program.
 +
* '''extern variables''' are initialized to zero, by default.
 +
* They can be accessed by all '''functions''' in the program.
 +
* These are stored in '''CPU memory'''.
  
 
|-
 
|-
Line 397: Line 355:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us see an example.
  
I have a code file let us go through it.
+
I have a code file; let us go through it.
  
 
Note that our filename is '''extern.c'''
 
Note that our filename is '''extern.c'''
Line 405: Line 363:
  
 
int x = 10 ;
 
int x = 10 ;
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I have initialized a variable as integer variable '''x''' to '''10'''.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| I have initialized a '''variable''' as '''integer variable x''' to '''10'''.
 
+
 
+
 
+
  
 
|-
 
|-
Line 416: Line 371:
  
 
extern int y;  
 
extern int y;  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This the main function.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This the '''main function.'''
  
In the main function I have declared an '''extern integer variable y.'''
+
In the '''main function''' I have declared an '''extern integer variable y.'''
  
 
|-
 
|-
Line 436: Line 391:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight int y = 50 ;
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Highlight int y = 50 ;
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will '''initalize y '''to '''50 '''after the '''main''' function close.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| We will '''initalize y '''to '''50 '''after the '''main function''' close.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''On the terminal'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| '''On the terminal'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now switch to the terminal and let us see what will be the output.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now switch to the '''terminal''' and let us see what will be the output.
 
+
Let me clear the prompt.
+
  
 
|-
 
|-
Line 448: Line 401:
  
 
'''Type: gcc extern.c -o ext'''
 
'''Type: gcc extern.c -o ext'''
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| '''Type: gcc extern.c -o ext'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Type:
 +
 
 +
'''gcc extern.c -o ext'''
  
 
Press''' Enter'''
 
Press''' Enter'''
Line 462: Line 417:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Output
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as:
  
 
'''The value of x is 10 '''
 
'''The value of x is 10 '''
Line 470: Line 425:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to the statements.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Point to the statements.
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As we studied the value of the '''extern keyword''' is throught out the main program.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As we studied, the value of the '''extern keyword''' is throughtout the main program.
  
 
We can define it anywhere in the program.
 
We can define it anywhere in the program.
Line 479: Line 434:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us move on to '''register keyword.'''
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Now let us move on to '''register keyword.'''
 
We will go back to the slides.
 
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Display Slide 11
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| Display Slide 11
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Register variables will be accessed faster than normal variables.
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"|  
 
+
*'''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): "..."''
+
*They are stored in '''register memory''' rather than '''main memory'''.
  
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.  
+
*Limited number of variables can be used since register size is very low.  
  
(16 bits, 32 bits or 64 bits)
+
*16 bits, 32 bits or 64 bits.
  
 
|-
 
|-
Line 506: Line 458:
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| register int i;  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"| register int i;  
 +
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared '''register integer variable'''.
  
 
+
This '''variable''' will be directly stored in the '''register memory.'''
 
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Here we have declared register integer variable.
+
 
+
This variable will be directly stored in the '''register memory.'''
+
  
 
|-
 
|-
Line 521: Line 470:
  
 
}
 
}
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the '''for''' loop that displays the value of '''i''' from '''1''' to '''5.'''
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| This is the '''for loop''' that displays the value of '''i''' from '''1''' to '''5.'''
  
 
This will display the value of '''i.'''
 
This will display the value of '''i.'''
Line 533: Line 482:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program and see.
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| Let us execute the program and see.
  
Let me clear the prompt
 
  
On the terminal.
+
On the '''terminal''', type:
 
+
Type:
+
  
 
'''gcc space register.c space -o space register'''
 
'''gcc space register.c space -o space register'''
 +
 +
Press '''Enter'''.
 +
  
 
Type: '''./register'''
 
Type: '''./register'''
 +
 +
Press '''Enter'''.
  
 
|-
 
|-
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:none;padding:0.097cm;"|  
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| The output is displayed as:
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| You can see 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'''
+
'''Values stored in register memory 1 2 3 4 5'''
  
 
|-
 
|-
Line 567: Line 510:
  
 
Summary
 
Summary
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial we learnt,
+
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| In this tutorial, we learnt-
  
 
* '''Storage class specifiers'''
 
* '''Storage class specifiers'''
Line 574: Line 517:
 
* '''extern keyword'''
 
* '''extern keyword'''
 
* '''register keyword'''
 
* '''register keyword'''
 
Where is the assignment.
 
 
 
''Reply to Madhuri (17/07/2014, 12:55): "..."''
 
 
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.
 
 
 
 
  
 
|-
 
|-
Line 589: Line 522:
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment,
 
| style="border-top:none;border-bottom:1pt solid #000000;border-left:1pt solid #000000;border-right:1pt solid #000000;padding:0.097cm;"| As an assignment,
  
Write a program, to print the sum of first  
+
*Write a program to print the sum of first 5 numbers  
 
+
5 numbers  
+
 
+
Declare both the '''keywords auto '''and
+
 
+
'''static''' in the program
+
 
+
 
+
  
 +
*Declare both the '''keywords auto '''and '''static''' in the program
  
 
|-
 
|-

Latest revision as of 18:05, 22 September 2014

Title of script: Storage class specifiers in C

Author: Ashwini Patil

Keywords: Video tutorial, storage class, auto, static, extern, register.


Visual Cue
Narration
Display Slide 1 Welcome to the spoken tutorial on Storage class specifiers.
Display Slide 2 In this tutorial, We will learn about
  • Storage class specifiers
  • auto keyword
  • static keyword
  • extern keyword
  • register keyword with the help of examples.
Display Slide 3

System Requirements

For this tutorial I am using
  • Ubuntu Operating system version 11.10 and
  • gcc Compiler version 4.6.1 on Ubuntu
Display Slide 4

Prerequisites


spoken-tutorial.org

To follow this tutorial you should be familiar with C and C++ tutorials.


If not, for relevant tutorials please visit our website, which is as shown.

Display Slide 5

Introduction

I will start with an introduction to storage class specifiers.
  • Specifiers tell the compiler where to store a variable.
  • How to store the variable.
  • What is the initial value of the variable.
  • Life time of the variable.
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.
  • Auto keyword declares an automatic variable.
  • It has a local scope.
  • Keywords are not initialized automatically.
  • You should explicitly initialize keywords while declaring
  • Storage space of keywords is CPU memory.
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.


Then we have return 0 statment.


Let us see the function definition

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 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.


Type:

./auto

Press Enter.


Yes it is working!

The output is zero.


This is because we have initialized the value of i as 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.


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 change the initialization of
  • the variable i to static
  • int i equal to zero

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.


They do not lose their value between function calls.

Display Slide 10

extern variable

Now let us learn about extern keyword.
  • Scope of extern variable is throughout the main program.
  • Definition for extern variable might be anywhere in the C program.
  • extern variables are initialized to zero, by default.
  • They can be accessed by all functions in the program.
  • These are stored in CPU memory.
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
  • Register variables will be accessed faster than normal variables.
  • They are stored in register memory rather than main memory.
  • Limited number of variables 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.


On the terminal, type:

gcc space register.c space -o space register

Press Enter.


Type: ./register

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-
  • Storage class specifiers
  • auto keyword
  • static keyword
  • extern keyword
  • register keyword
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.

Contributors and Content Editors

Ashwini, Nancyvarkey