Professional Documents
Culture Documents
GEC 225 Module3 - Slide
GEC 225 Module3 - Slide
Programming II
int main( ) {
setup();
return 0;
}
void setup() {
int x = 5;
serial.begin(9600);
if (x <MAXVAL) {
int temp;
temp = x*100*k;
}
printf(“ The value of temp is:” );
printf (temp);
}
6 Friday, April 22, 2022
3.1.2 Function Block Scope
Example 3.3
If (x <MAXVAL) { //start of if statement block
int temp; //temp’s scope starts after this line
temp = x *100; //temp goes out of scope here
}
9 Friday, April 22, 2022
3.2.0 Storage Classes
Storage class in C language is a specifier
which tells the compiler about data storage, it
describes:
i. where and how to store variables,
ii. its initial value and
iii. scope of the variables in a program.
Syntax:
register int number;
15 Friday, April 22, 2022
The features of register storage class are:
✓ Storage: CPU register.
✓ Default initial value: garbage value
✓ Scope: local to the function or block in
which it is defined.
✓ Life time: controls remains within
function or blocks in which it is defined.
Storing all variable in the CPU register is not
possible because of the limitation of the register
pair.
When a variable is used at many places (like loop
counter), then it is better to declare it as a register
storage class.
16 Friday, April 22, 2022
Example 3.5 – Use of register storage class
#include<stdio.h>
main( )
{
register int i;
for(i=1;i<=12;i++)
{
printf(“%d”,i);
}
17 Friday, April 22, 2022
3.2.3 Static Storage Class
A static variable tells the compiler to persist
the variable until the end of a program.
void test()
{
static int a = 0; //Static variable
a = a+1;
printf("%d\t",a);
}
21 Friday, April 22, 2022
3.2.4 External Storage Class
External storage class is used to declare variable
outside any function.
main()
{
extern int x; //Tells compiler that it is defined
//somewhere else
x = 10;
printf("%d",x);
}
int x; //Global variable x