Advance-C/C2/Storage-class-specifiers/English

From Script | Spoken-Tutorial
Jump to: navigation, search

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