session 5
DESCRIPTION
C Language SlidesTRANSCRIPT
![Page 1: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/1.jpg)
Storage classes
• A variable in C can have any one of the four storage classes.
1. Automatic variables.2. External variables.3. Static variables.4. Register variables.
![Page 2: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/2.jpg)
AUTOMATIC VARIABLES (LOCAL/INTERNAL)• Automatic variables are declared inside a function in
which they are to be utilized.• They are created when a function is called and destroyed
automatically when the function is exited.• Eg:main()
{int number;
}• We may also use the keyword auto to declare automatic
variables explicitly.
![Page 3: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/3.jpg)
EXTERNAL VARIABLES• Variables that are both alive and active throughout
the entire program are known as external variables. They are also known as global variables.extern float length = 7.5;void main(){}function1( ){}The keyword extern can be used for explicit declarations of external variables.
![Page 4: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/4.jpg)
STATIC VARIABLES• As the name suggests, the value of a static
variable persists until the end of the program. A variable can be declared static using the keyword static.
• Eg:• 1) static int x;• 2) static int y;
![Page 5: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/5.jpg)
REGISTER VARIABLES• We can tell the compiler that a variable should be
kept in one of the machine’s registers, instead of keeping in the memory.
• Since a register access is much faster than a memory access, keeping the frequently accessed variables in the register will lead to faster execution of programs.
• This is done as follows:register int count;
![Page 6: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/6.jpg)
POINTERS • A pointer is a variable that represents the location of a
data item, such as a variable or an array element.
• A pointer is a memory address.
• In the simplest term pointer is a nearly integer variable which stores a memory address of a computer which may contain other variable or even another pointer.
![Page 7: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/7.jpg)
Concept of Address and Pointers• Memory can be
conceptualized as a linear set of data locations.
• Variables reference the contents of a locations
• Pointers have a value of the address of a given location
Contents1
Contents11
Contents16
ADDR1ADDR2ADDR3ADDR4ADDR5ADDR6
***
ADDR11
**
ADDR16
![Page 8: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/8.jpg)
Pointer Variable• Declaring a pointer variable is quite similar to
declaring an normal variable all you have to do is to insert a star '*' operator before it.
• General form of pointer declaration is -type* name;
For Exampleint * variable1;
![Page 9: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/9.jpg)
• Say you declare a variable named foo.int foo;
• Now let's declare another variable of pointer type
int *foo_ptr = &foo;foo_ptr is declared as a pointer to int. We have initialized it to point to foo.
![Page 10: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/10.jpg)
Use of & and *
• When is & used?• When is * used?
• & -- "address operator" which gives or produces the memory address of a data variable.
• * -- "dereferencing operator" which provides the contents in the memory location specified by a pointer
![Page 11: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/11.jpg)
#include <stdio.h>void main (void){
int count = 10, x;int *int_pointer;int_pointer = &count;x = *int_pointer;printf ("count = %d, x = %d\n", count, x);
}
![Page 12: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/12.jpg)
Using Pointers in Expressions#include <stdio.h>void main (){ int i1, i2;
int *p1;i1 = 5;p1 = &i1;i2 = *p1 + 20;
printf ("i1 = %d, i2 = %d, *p1 = %d ", i1, i2, *p1);}
![Page 13: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/13.jpg)
Pointer arithmetic• Pointers can be added and subtracted.
• However pointer arithmetic is quite meaningless unless performed on arrays.
• Addition and subtraction are mainly for moving forward and backward in an array.
![Page 14: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/14.jpg)
#include <stdio.h>void main(){
int ArrayA[3]={1,2,3};int *ptr;ptr=ArrayA;printf("address: %p - array value:%d “,ptr,*ptr);ptr++;printf("address: %p – array value:%d”,ptr,*ptr);
}
![Page 15: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/15.jpg)
Pointers and Functions
• Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there.
• There are two mechanism to call a function1) Pass by Value2) Pass By Reference
![Page 16: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/16.jpg)
Pass by Value#include <stdio.h>
void main ( )
{
int a = 5, b = 6;
printf("a=%d b=%d\n",a,b) ;
swap (a, b) ;
printf("a=%d b=%d\n",a,b) ;
}
void swap(int a, int b)
{
int temp;
temp= a; a= b; b = temp ;
printf ("a=%d b=%d\n", a, b);
}
Results:a=5 b=6a=6 b=5a=5 b=6
![Page 17: Session 5](https://reader035.vdocuments.mx/reader035/viewer/2022081809/545c56fab0af9f952c8b47c2/html5/thumbnails/17.jpg)
#include <stdio.h>
void main ( )
{
int a = 5, b = 6;
printf("a=%d b=%d\n",a,b) ;
swap (&a, &b) ;
printf("a=%d b=%d\n",a,b) ;
}
void swap(int *a, int *b)
{
int temp;
temp=*a; *a=*b; *b = temp ;
printf ("a=%d b=%d\n", *a, *b);
}
Pass by Reference
Results:a=5 b=6a=6 b=5a=6 b=5