Assignment no.
1)Malloc():
#include<stdio.h>
#include<stdlib.h>
int main()
int i,n;
printf("Enter the number of integers:");
scanf("%d",&n);
int*ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
printf("Memory not available");
exit(0);
for(i=0;i<n;i++)
{
printf("Enter an integer:");
scanf("%d",ptr+i);
for(i=0;i<n;i++)
printf("Integer is:%d\n",*(ptr+i));
return 0;
Input:
Enter the number of integers:4
Enter an integer:23
Enter an integer:45
Enter an integer:18
Enter an integer:89
Output:
Integer is:23
Integer is:45
Integer is:18
Integer is:89
2)Calloc():
#include<stdio.h>
#include<stdlib.h>
int main()
int i,n;
printf("Enter the number of integers:");
scanf("%d",&n);
int*ptr=(int*)calloc(n,sizeof(int));
if(ptr==NULL)
printf("Memory not available");
exit(0);
for(i=0;i<n;i++)
printf("Enter an integer:");
scanf("%d",ptr+i);
for(i=0;i<n;i++)
printf("Integer is:%d\n",*(ptr+i));
return 0;
}
Input:
Enter the number of integers:3
Enter an integer:23
Enter an integer:67
Enter an integer:89
Output:
Integer is:23
Integer is:67
Integer is:89
3)Realloc():
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
char*str;
str=(char*)malloc(3);
strcpy(str,"Puspa");
printf("String=%s,Address=%u\n",str,str);
str=(char*)realloc(str,25);
strcat(str,"raj");
printf("String=%s,Address=%u\n",str,str);
free(str);
printf("String=%s,Address=%u\n",str,str);
return (0);
Output:
String=Puspa,Address=791520
String=Pusparaj,Address=791520
String=,Address=791520
4)Free():
#include<stdio.h>
#include<stdlib.h>
int main()
int i,n;
printf("Enter the number of integers:");
scanf("%d",&n);
int*ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
printf("Memory not available");
exit(0);
for(i=0;i<n;i++)
printf("Enter an integer:");
scanf("%d",ptr+i);
for(i=0;i<n;i++)
printf("Integer is:%d\n",*(ptr+i));
printf("\n");
free(ptr);
for(i=0;i<n;i++)
printf("Integer is: %d\n",*ptr+i);
return 0;
Input:
Enter the number of integers:6
Enter an integer:12
Enter an integer:56
Enter an integer:78
Enter an integer:23
Enter an integer:98
Enter an integer:27
Output:
Integer is:12
Integer is:56
Integer is:78
Integer is:23
Integer is:98
Integer is:27
Integer is: 136160
Integer is: 136161
Integer is: 136162
Integer is: 136163
Integer is: 136164
Integer is: 136165
#include<stdio.h>
union distance
float feet1;
float inch1;
float feet2;
float inch2;
};
int main()
int i;
float sumFeet,sumInch;
union distance d[2];
for( i=0;i<2;i++){
printf("Enter the distance 1 in feet:");
scanf("%f",&d[i].feet1);
printf("Enter the distance 1 in inches:");
scanf("%f",&d[i].inch1);
printf("Enter the distance 2 in feet:");
scanf("%f",&d[i].feet2);
printf("Enter the distance 2 in inches:");
scanf("%f",&d[i].inch2);
}
sum(d,&sumFeet,&sumInch);
printf("\nSum of distance in feet is:%f\n",sumFeet);
printf("Sum of distance in inches is:%f\n",sumInch);
return 0;
void sum(union distance d[],float *sum_feet,float *sum_inch)
int i;
for(i=0;i<2;i++){
*sum_feet=d[i].feet1+d[i].feet2;
*sum_inch=d[i].inch1+d[i].inch2;
Input:
Enter the distance 1 in feet:4
Enter the distance 1 in inches:5
Enter the distance 2 in feet:4
Enter the distance 2 in inches:7
Enter the distance 1 in feet:5
Enter the distance 1 in inches:7
Enter the distance 2 in feet:3
Enter the distance 2 in inches:9
Output:
Sum of distance in feet is:18.000000
Sum of distance in inches is:18.000000
#include<stdio.h>
enum year {Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec};
int main()
int i;
enum year month;
for(i=Jan;i<=Nov;i++)
printf("%d",month=May);
return 0;
Output:4