Search for notes by fellow students, in your own course and all over the country.
Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.
Title: Storage class of C Programming
Description: Complete notes of Storage class of C Programming
Description: Complete notes of Storage class of C Programming
Document Preview
Extracts from the notes are below, to see the PDF you'll receive please use the links above
1
Storage Class
C has a concept of 'Storage classes' which are used to define the scope (visibility) and life time
of variables and/or functions
...
In C program, there are four storage classes:
automatic, register, external, and static
...
For these reasons, they are also called local variables
...
o
Scope: Local to the block in which variable is declared
...
#include
h>
int main()
{
auto int i=10;
{
auto int i=20;
printf("\n\t %d",i);
}
printf("\n\n\t %d",i);
getch();
return 0;
}
Sumon Ghosh (Mob No: 9836246293 Email Id: sumonghosh
...
com)
2
Output:
20
10
Register Storage Class:
Register is used to define local variables that should be stored in a register instead of RAM
...
It should also be noted
that defining 'register' goes not mean that the variable will be stored in a register
...
o
Scope: Local to the block
...
Calculations are done and the final result is sent back to main memory
...
Register variables occur in CPU and value of that register variable is stored in a register within that CPU
...
There is no waste of time, getting variables from
memory and sending it to back again
...
It cannot not used with static or external storage class
...
Sumon Ghosh (Mob No: 9836246293 Email Id: sumonghosh
...
com)
3
Program :
// Program to demonstrate register storage class
...
h>
#include
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
...
o
Scope: Global to the program
...
87@gmail
...
But few registers are available
for user programs
...
' They are declared outside
the functions and can be invoked at anywhere in a program
...
#include
h>
int main()
{
int i=20;
void show(void);
printf("\n\t %d",i);
show();
getch();
return 0;
}
void show(void)
{
extern int i;
printf("\n\n\t %d",i);
}
int i=10;
Output:
20
10
Static Storage Class:
A variable with the same lifetime as the program, as described above
...
87@gmail
...
o
Life : depends on function calls and the whole application or program
...
Syntax:
static [data_type] [variable_name];
Example:
static int a;
There are two types of static variables as :
a) Local Static Variable
b) Global Static Variable
Static storage class can be used only if we want the value of a variable to persist between different
function calls
...
#include
h>
int main()
{
int i;
void incre(void);
for (i=0; i<3; i++)
incre();
getch();
return 0;
}
void incre(void)
{
Sumon Ghosh (Mob No: 9836246293 Email Id: sumonghosh
...
com)
6
int avar=1;
static int svar=1;
avar++;
svar++;
printf("\n\n Automatic variable value : %d",avar);
printf("\t Static variable value : %d",svar);
}
Static functions in C
Static functions are functions that are only visible to other functions in the same file
In C, functions are global by default
...
For
example, below function fun() is static
...
Therefore, when we want to restrict access to functions, we make them static
...
/* Inside file1
...
c
int main(void)
{
fun1();
getchar();
return 0;
}
*/
Now, if we compile the above code with command “gcc file2
...
c”, we get the error “undefined reference
to `fun1′”
...
c and cannot be used in file2
...
Please write comments if you find anything incorrect in the above article, or want to share more information
about static functions in C
...
87@gmail
Title: Storage class of C Programming
Description: Complete notes of Storage class of C Programming
Description: Complete notes of Storage class of C Programming