Storage Class Variables:
Static storage class:
Storage: main memory & data segment.
Default value: zero.
Scope: local to
the block in which the variable is defined.
Lifetime: till the value of the variable persists between different function
calls.
Example:
#include
<stdio.h>
//program
in file f1.c
void
count(void) {
static
int count1 = 0;
int
count2 = 0;
count1++;
count2++;
printf("\nValue
of count1 is %d, Value of count2 is %d", count1, count2);
}
/*Main
function*/
int
main(){
count();
count();
count();
return
0;
}
Output:
Value of
count1 is 1, Value of count2 is 1
Value of
count1 is 2, Value of count2 is 1
Value of
count1 is 3, Value of count2 is 1
Automatic storage class:
Storage : stack.
Default value: garbage
value.
Scope: local to
the block in which the variable is defined.
Lifetime: till
the control remains within the block in which the variable is defined.
Example: auto
int a = 5; int a = 5;
Register storage class:
Storage: CPU
registers.
Default value: garbage
value.
Scope: local to
the block in which the variable is defined.
Lifetime: till
the control remains within the block in which the variable is defined.
Example: register
int x=5;
Automatic storage class:
Storage: main
memory.
Default value: garbage value.
Scope: local to
the block in which the variable is defined.
Lifetime: till
the control remains within the block in which the variable is defined.
External storage
class:
Storage: main
memory.
Default value: zero.
Scope: global on other file in the same project
Lifetime: as long as the program execution
doesn't come to an end.
Example:
/***************
Index:
f1.c
****************/
#include
<stdio.h>
extern int
x;
int main()
{
printf("value
of x %d", x);
return 0;
}
Index:
f2.c
****************/
int x = 3;
Global variables:
Storage: main
memory.
Default value: zero.
Scope: throughout
the program.
Lifetime: till the
control remains within the block in which the variable is defined.
Note: declared above the main( ) function.
Bitwise operator:
Bitwise AND(&) operator.
ANDing operation :
________________________
10101101 original bit
pattern
00001000 AND mask
----------------------------------------
00001000 resulting bit pattern
Left Shift or Right shift of number:
Left shift:
Eg1: 14<<1;
Consider a number
14-----00001110 (8+4+2)is its binary equivalent
left shift it by
1--------------00011100(16+8+4) which is 28.
Eg2: 1<<1;
consider the number as
1---00000001(0+0+1).
left shift that by
1------------00000010(0+2+0) which is 2.
left shift by 1 bit of
a number=2*number
left shift by 1 bit of
2*number=2*2*number
left shift by n bits of number=(2^n)*number
Right Shift:
Eg1: 14>>1;
Consider a number
14-----00001110 (8+4+2)is its binary equivalent
right shift it by
1-----------------0000111(4+2+1) which is 7.
Eg2: 1<<1;
consider the number as
1---00000001(0+0+1).
right shift that by
1----------------0000000(0+0+0) which is 0.
right shift by 1 bit
of a number=2/number
right shift by 1 bit
of 2*number=2/2/number
right shift by n bits of number=(2^n)/number.
No comments:
Post a Comment