[go: up one dir, main page]

0% found this document useful (0 votes)
12 views21 pages

Storage Classes

The document provides an overview of C programming, covering fundamental concepts such as data types, storage classes, and number systems. It details the four storage classes in C: auto, register, static, and extern, explaining their characteristics and usage with examples. Additionally, it introduces tools for programming like IDEs and the processes of compilation and debugging.

Uploaded by

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

Storage Classes

The document provides an overview of C programming, covering fundamental concepts such as data types, storage classes, and number systems. It details the four storage classes in C: auto, register, static, and extern, explaining their characteristics and usage with examples. Additionally, it introduces tools for programming like IDEs and the processes of compilation and debugging.

Uploaded by

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

C PROGRAMMING

Mrs. SUMITHRA R. P.
UNIT I

Introduction, structure of C program, data types, storage classes, constants,


enumeration constant, keywords, variables, operators, expressions, input/output
statements, assignment statement conditional statements;

Number system: binary, decimal, hexadecimal, conversion between number system


types;

Introduction to tools – IDE, compilation, linking, debugging.


23ECE105 COMPUTER PROGRAMMING

Prepared by,
Mrs. SUMITHRA R. P.
Storage classes

 A storage class defines the scope (visibility) and life-


time of variables and/or functions within a C Program.
 They precede the type that they modify.
Storage classes

Storage class in C is used to describe the following things:


➢ The variable scope.
➢ The location where the variable will be stored.
➢ The initialized value of a variable.
➢ A lifetime of a variable.
➢ Who can access a variable?
Types
 We have four different storage classes in a C program −
➢ auto
➢ register
➢ static
➢ extern
The auto Storage Class

 This is the default storage class for all the variables


declared inside a function or a block.
 Auto variables can be only accessed within the
block/function they have been declared and not outside
them (which defines their scope).
 They are assigned a garbage value by default whenever
they are declared.
 The auto storage class is the default storage class for all
local variables.
{
int mount;
auto int month;
}
#include <stdio.h>
int main( )
{
auto int j = 1;
{
auto int j= 2;
{
auto int j = 3;
printf ( " %d ", j);
}
printf ( "\t %d ",j);
}
printf( "%d\n", j);}
Output

321
The register Storage Class

 The register storage class is used to define local variables that


should be stored in a register instead of RAM.
 The compiler tries to store these variables in the register of the
microprocessor if a free register is available.
 Register variables are much faster than that of the variables stored
in the memory during the runtime of the program.
 This means that the variable has a maximum size equal to the
register size
 variables which are to be accessed very frequently in a program are
declared with the register keyword which improves the running
time of the program.
{
register int miles;
}
Example
#include <stdio.h>
int main()
{
register int count;
int i;
for (i=0;i<=10;i++)
count++;
printf(“Count=%d”,&count);
//The above statement will throw error
return 0;
}
The static Storage Class

 Static variables have the property of preserving their value


even after they are out of their scope.

 By default, they are assigned the value 0 by the compiler.

 They are initialized only once and exist till the termination of
the program.

 Therefore, making local variables static allows them to


maintain their values between function calls.
Example
#include <stdio.h> int main()
int func() {
{ func();
static int i=0; //static variable func();
int j=0; //local variable func();
i++; return 0;
j++; }
printf("i=%d and j=%d\n",i,j);
}
Output

 i=1 and j=1

 i=2 and j=1

 i=3 and j=1


#include <stdio.h>
int f() {
static int x = 0;
x++;
return x;
}
int main(void) {
int j;
printf("Value of f(): %d\n", f());
printf("Value of f(): %d\n", f());
return 0;
}
The extern Storage Class

 Extern storage class simply tells us that the variable is


defined elsewhere and not within the same block where
it is used.
 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
however, it points the variable name at a storage
location that has been previously defined.
 It can be accessed within any function/block.
 The main purpose of using extern variables is that they
can be accessed between two different files which are
part of a large program.
example

#include <stdio.h> exsample.h


#include "exsample.h" int a=100;
int main() int b=200;
{
extern int a,b; Output
int sum = a + b; 100+ 200 = 300
printf("%d + %d = %d ", a,
b, sum);
return 0;
}
First File: main.c

#include <stdio.h>
#include “support.h"
int count ;
extern void write_extern();

main()
{
count = 5;
write_extern();
}
Second File: support.c

#include <stdio.h>
extern int count;
void write_extern(void)
{
printf("count is %d\n", count);
}
Thank You

You might also like