Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Programming in C_6.

1_Storage Classes

Hello and welcome to the session as the concept of variable or declaration of variable being the
strongest feature of C programming language. The storage classes will tell us the persistence of the
variable meaning the life of the variable and its scope within the program and also it tells us in which
part of the program these variables can be recognized. So in this session we will be seen the concept
of storage classes and its visibility and the visibility of variables within the program and also we will
know the difference between the auto variables and the global variables. As we know that the
variable can be declared by its data type telling that which kind of value can be held within the
variable.

So we categorize these variables as data type one is the type of information that is represented by
the variable. And also it can be categorized as storage class, which tells what to which to how, how
much time to how much duration this variable can be stored within the memory and the scope of
the variable where it is declared and also it's visibility within the program. That is where the entire
variable can be seen and where on which and in which parts of the program this variable can be
accessed can be specified by the storage classes.

This, storage class specifiers can be categorized into four types. We have following types of storage
specifications and C. One is automatic variables we also call them as local variables. We have global
variables, which are defined usually outside the functions, and these global variables are also called
us external variables sometimes, and we have static variables whose values are fixed, and we also
have external variables with which probably we can access the variables which are stored in some
other files. So these are the storage specifiers that we have in C programming language, automatic,
global, static and extern variables. Automatic variables are the variables are also called as local
variables. Yeah. Now these local variables are the ones which are declared within the function only.
That means to say that when the control comes out of the function the variable will lose its value.
For example, if int i is declared in function one then int i scope of int i or the scope of variable i will
be within the function only.

Now let us say another variable int i of same name is declared within another function two then this
I will be local to function two only that means variable i of function one and variable i of function
two though the names are same will be considered separately and i of function one is local to
function one only, and i of function two will be local to function two only. So this is how the
automatic or the scope of automatic variables work. So they're declared inside the block and it is
visible or the variables are visible only within the function and the scope of the variable are the
access of the variable is within the function only. The advantage of variables which are declared as
auto are the first advantage is it conserves memory in the sense now these auto variables exist only
when they are needed that means to say they're declared inside the function only when we want to
use them inside that particular function and these variables are created only when the control
enters into the particular function, or function definition.

Now when I say this, it means that variable will not be created when they're just declared a variable
will be created when it is initialized with any particular value only then the memory will be allocated
to the variable. Otherwise, there is no memory which is allocated to the variable, and also because
of this the memory would be conserved and also the memory will be released once the control
comes out of the function again only, when the function call is made or when the control gets into
the function definition the memory will be allocated to the variable again when it comes out of the
function, that memory will be destroyed. So this keeps happening, in which case the memory will be
conserved or the memory will be saved, which can be utilized for any other variables or any other
functions and also it has it follows a principal of least privilege. That means data can be accessed
only when it is needed. Otherwise, we need not when the data is not needed we don't access it
when we don't access it, then it is not stored. So this follows or automatic storage variables. Storage
specifiers will maintain least privilege because it is accessed only when it is needed, and also the
memory is created only when the data is being used here.

Now, in this program we will see the implementation of auto variables. Now here, the program
consists of two functions. One is the main function and another is test function. Now here in main
function, we can observe that there are two variables which are declared they are m and n, which
have been initialized with 22 and 44 values respectively and those values are being printed or
displayed here and there is a function call which is made to test function.

Once the control sees that test function, the function is invoked on the control will jump on to
control test function. Now here inside the test function, there are two more variables which are a
and b now this and we are initialized with values 50 and 80. Now they're also being displayed here in
by test function. Now the thing here is test function will not know what values have been declared in
main function and main function will not know what values have been declared in test function.
They're unaware of each other's variables that have been declared locally inside the functions, inside
the respective functions.

So does we say are the scope of m and n variables, are pertained to only main function and a and b
variables scope is pertained only to test function to main function cannot access and be and test
function cannot access m and n, so they are called as auto variables. Now here I have not or we have
not mentioned any keyword or something like auto to the variables, the reason being any variables
which are declared locally by default if its storage classifier is not specifically mentioned, then they
are considered to be auto or local variables. But the variables should be inside the main function, not
outside the main function. Any variables which are declared inside the function and if they're not
mentioned with auto keyword are still are will be considered as auto variables only by default.

We don't have to mention exclusively that there are auto variables, so the output of this program
would be values M equal to 22 and 44. First the main function values will be displayed because then
after that the function call has been made here and then the values 50 and 80 will be displayed. Now
let us also see a program which where we use make use of auto keyword. Now, if we see here, we
have two functions. One is factorial function another is main function. Now, if you observe this
factorial function is written before main function that is okay.

We can write functions or defined functions or we can have function definitions in the program
before main function or after main function. It doesn't matter, now here the variable i is local to
factorial function and also variable m is local to factorial function and way have one more variable,
which is temp, which is also local to the factorial function. All these local variables were mentioned
by auto keyword, saying it is an automatic variable or local variable to factorial and once the control
comes out of this factorial function after compilation, this variables m, i, temp will not have any life
or the memory would be released which is allocated to m, i and temp.

Now, when the control goes or gets in to main function, it has one more auto variable, which is n on
because n is initialized here, and the memory will be created to n now this n cannot be or will not be
visible or accessible to factorial function. So this is how we implement automatic storage by using
auto keyword. If we're not using auto keyword, no harm. Every variable that is declared inside the
function will be considered as automatic or a local variable by default, Let us say one more
automatic storage function where auto storage specifier is implemented. But here in a bit different
way we have firstly, in the first line we have a function declaration or a function prototype, which
has increment void this means that there is no return value from the function.

Next we have a register storage specifier. Now these variables can also be declared as a register
storage class are by using register storage specifiers. Now this the keyword register can be placed
just before the automatic variable declaration to make it a register variable or to tell the compiler to
make that variable to facilitate that variable register storage. Now register storage is nothing but
maintaining a particular variable in high speed hardware registers is nothing but a register storage.
We can do that by including a register keyword just before the automatic variable.

So this is the register keyword and this is how any register storage can be specified. Then why do we
use this register variables is that suppose we have a high, intense variables, which keeps count of
several functions or we have counter variables which keeps doing you know which keeps calculating
or keep track keep will keep track of the count of the variables in such cases, we usually used this
register storage. Now here in the program if we see how register storage specifier can be
implemented now, this is where in the first declaration statement we have registered in i.

Now, this declaration can be done any declaration section of the function, after which we are
declaring an array of five on this area is initialized to the different values. Now, here we are using i as
a counter variable in for loop as I said that register storage is used for highly intense counter
variables now, here it is not highly intense but this is just an example to show how register variables
can be implemented. Now this I can be declared as registering such that the variable can be
maintained in the registers by the compiler and now here we have display were displaying the value
of array, which is i an array of i. But if you can observe the working is just like normal interior.

Just that auto into variable will be declared normally in the memory unit. But this i variable i will be
stored in a register. Okay now, these registered declarations are unnecessary because nowadays all
the compilers can optimize and recognize which variables are being frequently used in the program.
So these registered declarations are unnecessary, and also the compilers can decide and place them
in the registers without any need of register keyword or register declaration. You don't have to
declare any variable as register to make sure that that variable is placed in a register. Any compiler,
with its optimization techniques will understand which variable is being frequently used is a counter
variable i being used or is a counter variable a being used frequently and then will take that variable
and place it in a register without any mention of the programmer by declaring the variable as a
register variable.

So we have local variables which are declared inside the function and which are which are local only
to the function are visible only to the function. But what if we need variables which can be accessed
to throughout the program. Now any variables which can be accessed even outside the function are
called as global variables. So the scope of this global variables will be throughout the program that
means every section of the program can access this global variable if it wants to and we can see this
global variables are access this global variables throughout the program and these variables should
be defined outside the main function but not within the main function or within the function
because if it is, if any variable is declared inside the function, then it would be local it will no more be
global function. Sorry, global variable that's the reason why the global variables have to be declared
outside the main function or outside the function so that it could be it would be visible to all the
other functions. It's like a common general property which can be used by or you know all the
functions and all the sub functions.
Now let us see the implementation of global variables with the help of the C program. Now we have
two variables which are declared globally here, which are a and b and also there is a function which
is defined outside the main function. Now any function if it is defined outside the main function or
before the main function that way need not declare the function. So we don't need to have any
function prototyping for that particular function. Because the function is already defined the
compiler will not such for its declaration anymore, if any function is defined out outside or before
the main function. Okay now here in this we in this and function we have return a plus b statement
where we have not declared any variables a and b inside our function but this add function is trying
to access the variables which are declared globally here if we observe.

Now after this, we also have a main function where there is one variable int answer, which has been
declared. Now this int answer is a variable which is local to the main function and cannot be
accessed by any other functions. But this a, b are the variables are being accessed by main because
their global variables and also it is initializing some values to it 5 and 7. Now the global variables
which are declared will have will be assigned with values 5 and 7 and upon a function call in the main
function to add the control will jump onto the section add which is where function is defined. Now
here a plus b will be calculated, which is 5 and 12 and that value will be returned back to the calling
function where we're displaying the value of the function through printer statement. So now the
value what you can see is 12. So here global variables which are not at all declared inside the
function are being accessed by both the functions add and main function and is our function is doing
some kind of manipulation, whereas main function is initializing values to do these global variables
and upon a function call after some arithmetic cooperation on this values, the value is being
displayed here.

Here is another program to show the implementation of global variables. Now here we can see that
there is a wide test that is being you know declared global here without any parameters because it
doesn't return any values. No values to the parameters then we have variables which are declared as
m, n, which as 22 to 44 as A and B, which are of 50 and 80 now inside the main function if you can
see that the printed statement is accessing all the variables m, n, a, b and displaying them here is
where it is accessing. Now that is the reason why in the output we have all the values that are
displayed here as m equal to 22, n is equal to 44, a is equal to 15, b is equal to 80 and then also, we
also have void test function where these valuables air being accessed and displayed here. So you can
see that both the functions are accessing a, b, m, n values. We have reached the end of session.

Thank you.

Programming in C_6.2_Static & External Variables

Hello and welcome to the session apart from seeing the advantages and disadvantages of auto and
global variable and having differentiated them, we also have static on extern storage. Specifiers.
Now in this session, you will be able to differentiate between auto, global, static and extern
variables, and also you will be able to implement a program where we can fetch variables which are
declared as extern from one source file to another source file. As auto variables, static variables are
also declared inside the function or within the individual functions, and also this will have the same
scope as automatic variables that means they are visible or the scope of the of these static variables
will be only within the function. But the difference between auto variables and static variables is that
static variables will retain its values as auto variables will not be able to retain its values but will lose
the value once the control comes out of the function definition. So the static variables will have its
values which are changed and will retain its value throughout the program.
So if you have a static variable in a function how many ever times the function is called and the
changes that are being made to static will be retained and those many times the changes that are
made will also be retained in the variables. As said this static variables will retain their previous value
even after the function is exited and re-entered are when there is a re-entry of the control in the
function definition at a later time and also the static variables will be initialized only once and their
initial values can be included at the time of static declaration itself. So, for example these static
variables will be used mostly when we want to note certain kind of or when we want to keep
counter of some functions which are being called again and again. For example, any recursive call
function can be kept track through this variables, which are declared as static by using them as a
counter. The syntax of static variable is with the help of the static keyword. Here we use the static
keyword.

Now we should know that the syntax of variable is a usually the same anywhere everywhere but
only the storage specifier will be changing it can either be static or it can be extern or it can be any
storage specifier. So only the specifier keeps changing. If it is an auto variable, then you have. You
don't have to mention any storage specifier. Now, here in the example, we can see we have static in
exercise that equal to 123 which means that the initial value of static variable is 123. Now, the
declaration and the initialization is done in the single statement here we have a program which
shows how static keyword or static variable works which is actually very interesting. So here we have
function, which has been declared and in the main function we have, we're making function calls to
the same function three times.

Now on the first function call the control will jump on to the function here function definition where
we have one variable which is declared as integer, which is a local variable to the function fun the
value that is initialized to this variable is 1 and also there is a static variable which has been declared
by name b which has been given value 10. Now here we are printing or displaying the values of both
the variables a and b. Now, when the variable a is being displayed here through printf function, 1 is
being displayed onto the console and when b is being displayed onto the displayed onto the console,
then the value displayed is 10. Now here we are making changes to a by implementing and also by
incrementing now. Once the function comes to the end, it comes back it goes back to the function
which has called it that is the calling function, which is main function to the next statement after the
first function call.

Now here, the function is being called again in the next statement, so the control will jump back to
the function definition fun now in this again, the variable a will be initialized to 1 as it would lose the
value or the previous value, which it has been given or taken because it's a local variable. But here b
will have retained its value where the value has been implemented last time to 11 and as static
variable can be initialized only once though the value has been changed, the value will be retained
and will not be initialized back with the previous value, so it will retain its new value.

So thus when the statements are printed, which prints a and b. The values would be displayed as 1
and 11 here. Now again, the value will be implemented as a plus plus and B plus plus. a will again
and upon the function call again a will be redeclared as 1 and by the will have or hold its initial value,
though there is a declaration and initialization in fun function here, the value will not be initialized
because the initialization can happen only once to the static variable. Here there is one more
program, which is factorial program which is used to count the number of recursive calls that are
made now because i'm using a static a counter variable count equal to zero here, which i am
declaring it to be static. Now this static will have capability to count the number of recursive calls
because once the count variable is initialized to zero whenever there is a change or increment, you
know, increments that is happening to count variable those variables or those values will be noted
by the count variable but will not get back and get re declared to zero value. So here I am making or
incrementing the value of count to be increased by one.

Now here, when a recursive function call is being made here in the main function the factorial
function will call itself again and again. So count variable as it keeps count of the recursive call when
I am printing it prints how many number of times the recursive function call is being made. So when
n is equal to 6, the function call has been made as one when the n is equal to five the 2nd time the
recursive function call is being made when n is equal to 4 third time the recursive function call is
being made and n equal to 3,4 times the recursive function call is being made so till any when any is
equal to zero seventh time the function call a recursive function call is being made, and then the
value is printed here in the main function for which factorial value has been found. Now we have
extern storage specifier where which is equivalent and to global variable.

We usually make use of global variables in while using extern storage specifier, now the scope of this
extern variable can be anywhere within the program. That means you can initialize the value of
external variable anywhere within the program it need not be declared, or it need not be initialized
only at the party at a particular place. It can be declared anywhere in the program, and it can be
accessed to also anywhere in the program and still the value of variable will be stored inside the
variable now here to the extra on variable. The declaration can be done any number of times, but
the value should be defined only once, and the visibility of this external variable can also be
extended not only to variables but also to functions and not only to functions but also to some other
files.

Here, let us see in the program how the extern variables are being used or we can make use of
extern variables here there is global variable which is declared on initialized to be 10, which is X.
Then we have extern variable, which is declared to be verified with the help of extern key word.
Now, as why is extern variable here, you can see after main function somewhere in the program.
The value has been assigned with assigned as 50 now inside the main function when I'm trying to
print the value of x and y, it would still be printing the value of x and y, though the value of y has
been declared sorry initialized somewhere in the program, it will. Still, our main function will still be
able to access the value of y ,5. Now let us see how extern variables can be put into use when we
have variables in some other file and how to include them in the source file.

Now, many a time it may so happen that user would define can have his own library functions and
include them in the header files and to make use of those functions are variables we have to include
them in the source file. So here we have two files which arefile1.h which is a header file and file1.c,
which file1.c. Now, in file1.c, we have a equal to 10, b equal to 20 in C variables which are declared
and initialized. We're not initializing the value of c and in Sorry. In another file1.c, we have declared
all the variables a, b, c which we are accessing from file1.c by including that file1.h header file inside
the source file here and we are declaring them declaring a,b,c as extern variables here in the main
function. So whenever we're using those variables of that header file, those variables can be
declared as extern on, then we can make use of them. See, here we're simply manipulating be by a
and storing it in see value.

We can also have external functions which can be defined in some other files. Now, here we have
two files file1.h and file1.c in file1.h we have a they find a function output here, and we have
performed some of function here which displays a statement. Hi welcome now, if we have to use
that particular function in our source file, then we have to include that header file, which is file1.h
and also we have to declare that function as extern function such that we can make use of it. So
inside the main function, this output function is being called now because this output function
though it is defined in some other function. Some other header file the functionality or operation
would be performed and hi welcome will be displayed on to the output. So there are a few tips are
tricks that we have to follow while for a for a storage class one is when defining a variable as global
rather than local what we have to keep in mind is it leads to many side effects declaration of global
variables will lead to many side effects because these variables can be accessed to throughout the
program, if we accidentally modify them where it is not required, then it would become a problem.

So it is better to avoid to declare these global variables I mean to avoid global declaration of
variables rather declare all the variables within the function as local variables until it is not very
necessary. So these are some rules which specifies the file score. Now the file can be the identifiers
of file can be defined outside are known and also the identifiers. Files defined outside the function
will be known to all the functions and the global variables, function definitions and all the function
prototypes can be accessed. The scope of the function can be only referenced inside a function
body, nowhere else you can have none of the scope of variables and nextly function prototypes
scope would be used for identify us in parameter list.

So the scope of variables in function prototype will be known or known only to the function. So the
scope of the block would be would begin at the definition of the block and answered the end right
brace. Now all identifies can be declared inside the block, and basically this block scope is used for
variables, function parameters that we will be using and also some outer blocks of the outer blocks,
which are hidden from the inner blocks. Uh, can also come under the block scope.

So here we have a broad picture of which storage specifier is used for which uses which storage and
which initial value what scope it has on what is its life. So if we see auto storage specifier where all
the auto variables will be stored in stack something called as stack where the memory or the data
would be placed one above the other sequentially and which follows a last in first out technique. So
that's the storage space that is occupied by all the local variables. Now, the initial value of any auto
variable or local variable will be a garbage value until and unless it is initialized with the proper value
and all the auto variables scope will be within the block, and it ends at the end of the block. Now, we
also have an extern storage specifier whose storage is in the is in a particular data segment and its
initial value is zero. It's not garbage value. It is zero and scope of this extern variables is global and
also within multiple files. That means throughout the program and also can be accessed outside the
file.

And till the end of the program the life of the extern variable exists. Then we have a static variable
specifications. The storage of this static storage specify it is within the data segment. The initial value
is zero that is given to the static variables that is initialized and also thestatic variables will be scope
of the static variables will be within the block, and the life of the static variable will be till the end of
the program. Only when the program terminates, then the life of the static variable will also be
ended. Then we have register. Now this register will store all its values in CPU register. It is
registered variables will be storing all its values in CPU register. The default value of this will be a
garbage value. The life on the scope of the register variable will be only within block, and the life will
end at the end of the block. Once the can function comes out or control comes out of the function,
the register variable will lose its life. Okay, we have come to the end of decision.

Thank you very much.

You might also like