0% found this document useful (0 votes)
15 views19 pages

Storage

C Programme

Uploaded by

Mir Masrur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views19 pages

Storage

C Programme

Uploaded by

Mir Masrur
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

C Storage

Classes &
Scope

Dr. Md. Al Mamun


Storage Class
• A storage class defines the scope (visibility) and
life-time of variables and/or functions within a C
Program. These specifiers precede the type that
they modify. There are the following storage
classes,
o auto
o register
o static
o extern
auto
The auto storage class is the default storage class
for all local variables.
{
int mount;
auto int month;
}
The example above defines two variables with the
same storage class, auto can only be used within
functions, i.e., local variables.
register
• The register storage class is used to define local variables that
should be stored in a register instead of RAM. This means that the
variable has a maximum size equal to the register size (usually one
word) and can't have the unary '&' operator applied to it (as it does
not have a memory location).

{
register int miles;
}

• The register should only be used for variables that require quick
access such as counters. It should also be noted that defining
'register' does not mean that the variable will be stored in a register.
It means that it MIGHT be stored in a register depending on
hardware and implementation restrictions.
static
• The static storage class instructs the compiler to keep
a local variable in existence during the life-time of the
program instead of creating and destroying it each
time it comes into and goes out of scope.

• Therefore, making local variables static allows them to


maintain their values between function calls.

• The static modifier may also be applied to global


variables. When this is done, it causes that variable's
scope to be restricted to the file in which it is declared.
Example
#include <stdio.h>
/* function declaration */
void func(void);

static int count = 5; /* global variable */

main() {
while(count--)
{
func();
}
return 0;
}
/* function definition */
void func( void )
{
static int i = 5; /* local static variable */
i++;

printf("i is %d and count is %d\n", i, count);


Output
i is 6 and count is 4
i is 7 and count is 3
i is 8 and count is 2
i is 9 and count is 1
i is 10 and count is 0
extern
• The extern storage class is used to give a reference of a
global variable that is visible to ALL the program files. When
you use 'extern', the variable cannot be initialized as all it
does is point the variable name at a storage location that
has been previously defined.

• When you have multiple files and you define a global


variable or function, which will be used in other files also,
then extern will be used in another file to give reference of
defined variable or function. Just for understanding, extern is
used to declare a global variable or function in another file.

• The extern modifier is most commonly used when there are


two or more files sharing the same global variables.
Example
First File: main.c
#include <stdio.h>
int count ;
extern void write_extern();
main() {
write_extern();
}
Second File: write.c
#include <stdio.h>
extern int count;
void write_extern(void)
{
count = 5;
printf("count is %d\n", count);
}
C Scope Rules
A scope in any programming is a region of the
program where a defined variable can have its
existence and beyond that variable cannot be
accessed. There are three places where variables
can be declared in C programming language:

 Inside a function or a block which is called local


variables,
 Outside of all functions which is called global
variables.
 In the definition of function parameters which is
called formal parameters.
Local Variable
• Variables that are declared inside a function or
block are called local variables.

• They can be used only by statements that are


inside that function or block of code.

• Local variables are not known to functions outside


their own.
Example
#include <stdio.h>
int main () {
/* local variable declaration */
int a, b; int c;

/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b, c);
return 0;
}
Global Variable
• Global variables are defined outside of a function,
usually on top of the program.

• The global variables will hold their value


throughout the lifetime of your program and they
can be accessed inside any of the functions
defined for the program.

• A global variable can be accessed by any function.


That is, a global variable is available for use
throughout your entire program after its
declaration.
Example
#include <stdio.h>
/* global variable declaration */
int g;

int main () {
/* local variable declaration */
int a, b;

/* actual initialization */
a = 10;
b = 20;
g = a + b;
printf ("value of a = %d, b = %d and g = %d\n", a, b, g);
return 0;
}
Example
• A program can have same name for local and global
variables but value of local variable inside a function will
take preference.

#include <stdio.h>
/* global variable declaration */
int g = 20;

int main () {
/* local variable declaration */

int g = 10;
printf ("value of g = %d\n", g);
return 0;
Formal Parameter
• Function parameters, so called formal
parameters, are treated as local variables within
that function and they will take preference over
the global variables.
Example
#include <stdio.h>
/* global variable declaration */
int a = 20;

int main ()
{
/* local variable declaration in main function */
int a = 10;
int b = 20;
int c = 0;
printf ("value of a in main() = %d\n", a);
c = sum( a, b);
printf ("value of c in main() = %d\n", c);
return 0;
}

/* function to add two integers */


int sum(int a, int b)
{
printf ("value of a in sum() = %d\n", a);
printf ("value of b in sum() = %d\n", b);
return a + b;
Output
value of a in main() = 10
value of a in sum() = 10
value of a in sum() = 20
value of a in main() = 30
End of Slides 

You might also like