/*
static storage class program example */
#include <stdio.h>
#define MAXNUM 3
void sum_up(void);
int main()
{
int count;
printf("\n*****static storage*****\n");
printf("Key in 3 numbers to be summed ");
for(count
= 0; count < MAXNUM; count++)
sum_up();
printf("\n*****COMPLETED*****\n");
return 0;
}
void sum_up(void)
{
/* at
compile time, sum is initialized to 0 */
static int
sum = 0;
int num;
printf("\nEnter a number: ");
scanf("%d", &num);
sum +=
num;
printf("\nThe current total is: %d\n", sum);
}
/************************Register*********************/
- · Register keyword is used to define local variable.
- · Local variable are stored in register instead of RAM.
- · As variable is stored in register, the Maximum size of variable = Maximum Size of Register
- · unary operator [&] is not associated with it because Value is not stored in RAM instead it is stored in Register.
- · This is generally used for faster access.
- · Common use is “Counter“.
Syntax
{
register int count;
}
Register
storage classes example
#include<stdio.h>
void main()
{
int num1,num2;
register int sum;
printf("\nEnter the Number 1 : ");
scanf("%d",&num1);
printf("\nEnter the Number 2 : ");
scanf("%d",&num2);
sum = num1 + num2;
printf("\nSum of Numbers : %d",sum);
getch();
}
/***********Automatic Storage Classes and Variables**********/
This is default storage class
All variables declared are of type Auto by
default
In order to Explicit declaration of variable use
‘auto’ keyword
auto int num1 ;
// Explicit Declaration
Features
:
Storage
|
Memory
|
Scope
|
Local / Block Scope
|
Life time
|
Exists as long as Control remains in the block
|
Default initial Value
|
Garbage
|
Example
:
#include<stdio.h>
#include<conio.h>
void main()
{
auto mum = 20 ;
{
auto num = 60 ;
printf("nNum : %d",num);
}
printf("nNum : %d",num);
}
void main()
{
auto mum = 20 ;
{
auto num = 60 ;
printf("nNum : %d",num);
}
printf("nNum : %d",num);
}
Output
:
Num : 60
Num : 20
Num : 20
/**********Malloc*********/
*C program to create memory for int, char and
float variable at run time.*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *iVar;
char *cVar;
float *fVar;
/*allocating memory
dynamically*/
iVar=(int*)malloc(1*sizeof(int));
cVar=(char*)malloc(1*sizeof(char));
fVar=(float*)malloc(1*sizeof(float));
printf("Enter integer
value: ");
scanf("%d",iVar);
printf("Enter
character value: ");
scanf("
%c",cVar);
printf("Enter float
value: ");
scanf("%f",fVar);
printf("Inputted
value are: %d, %c, %.2f\n",*iVar,*cVar,*fVar);
/*free allocated memory*/
free(iVar);
free(cVar);
free(fVar);
return 0;
}
|
OUTPUT:-
Enter
integer value: 100
Enter
character value: x
Enter
float value: 123.45
Inputted
value are: 100, x, 123.45
hi sir,
ReplyDeleteplease upload BCS-042-Analysis And Design Of Algorithm &
BCSL-045-Analysis And Design Of Algorithms Lab solved assignments asap...
hi sir
ReplyDeleteplease upload BCS-042-Analysis And Design Of Algorithm &
BCSL-045-Analysis And Design Of Algorithms Lab solved assigments for BCA 4th Semester .
Thanks