Anna university
Computer Practice Lab -II (2) - C programs with explanation
Computer Practice Lab -II (2) - C programs with explanation
ILLUSTRATION OF malloc() FUNCTION
SOURCE CODE: malloc --- memory allocation
Code:
#include
#include
main()
{
int *i,*n,size; i and n----pointer variables
printf("\nEnter the size :");
scanf("%d",&size); // value for variable size is obtained
n=(int *)malloc(size * sizeof(int)); //memory is allocated for variable n.
in the sample output given below the value of size is 3.memory for int is 2 bytes. So 3*2 6 bytes of memory will be allocated for n and n will hold the address of first byte.
printf("\nAddress of First byte is :%u",n); //address of first byte is printed
printf("\nEnter the values :");
for(i=n;i< span="">
scanf("%d",i);//scanf statement is executed repeatedly until the condition in for loop is true
iteration 1: i=n ,so i=164700168
i<164700168+3="" is="" span="" true<="">
scanf statement is executed, first value is obtained and stored in address 164700168
iteration 2: i++ ,so i=1647001689
i<164700168+3="" is="" span="" true<="">
scanf statement is executed, second value is obtained and stored in 164700172 (for pointer variable of any data type the address is incremented by 4 bytes)
iteration 3: i++ ,so i=1647001670
i<164700168+3="" is="" span="" true<="">
scanf statement is executed, third value is obtained and stored in164700176 (for pointer variable of any data type the address is incremented by 4 bytes)
printf("\nPrinting the values :");
for(i=n;i< span="">
printf("\n%d is stored in address %u",*i,i); //printf statement is executed repeatedly until the condition in for loop is true
iteration 1: i=n ,so i=164700168
i<164700168+3="" is="" span="" true<="">
printf statement is executed, first value is printed using *i(value of i) and address is printed by using i (address)
iteration 2: i++ ,so i=1647001689
i<164700168+3="" is="" span="" true<="">
printf statement is executed, second value is printed using *i(value of i) and address is printed by using i (address)
iteration 3: i++ ,so i=1647001670
i<164700168+3="" is="" span="" true<="">
printf statement is executed, third value is printed using *i(value of i) and address is printed by using i (address)
free(0); //allocated memory is released
}
OUTPUT:
Enter the size :3
Address of First byte is :164700168
Enter the values :1
2
3
Printing the values :
1 is stored in address 164700168
2 is stored in address 164700172
3 is stored in address 164700176
ILLUSTRATION OF calloc() FUNCTION
calloc allocate memory for derived datatype like arrays,structure..
Code:
#include
#include
#include
main()
{
char *str=NULL;
str=(char *)calloc(20,sizeof(char)); // 20 bytes memory is allocated to variable str
size of char is 1 byte, so for 20 characters 20 bytes is allocated
strcpy(str,"ENGINEERING COLLEGE"); //the text “ENGINEERING COLLEGE” (18 bytes) is copied to str.the size of str is 20 bytes ,so it can hold the text
printf("RMD %s\n",str); //content of str is printed along with RMD
free(str); //allocated memory is released
return 0;
}
OUTPUT:
[harita@localhost c]$ cc calloc.c
[harita@localhost c]$ ./a.out
RMD ENGINEERING COLLEGE
ILLUSTRATION OF realloc() FUNCTION
Realloc—allocated memory can be reallocated
Code:
#include
#include
#include
main()
{
char *p;
p=(char *)malloc(6); //6 bytes memory is allocated to the variable p.
for a single character 1 byte memory is allocated, so for 6 characters 6 bytes is allocated
strcpy(p,"MADRAS"); // the text MADRAS(6 bytes) is copied to p
printf("\nMemory contains : %s\n",p); //the string MADRAS is printed
p=(char *)realloc(p,7); // memory of variable p is reallocated as 7 bytes
strcpy(p,"CHENNAI"); // the text CHENNAI (7 bytes) is copied to p
printf("Memory now contains : %s\n",p); // string CHENNAI is printed
free(p); //allocated memory is released
}
OUTPUT:
[harita@localhost c]$ cc realloc.c
[harita@localhost c]$ ./a.out
Memory contains: MADRAS
Memory now contains: CHENNAI
CREATION OF A FILE AND READING ITS CONTENTS
A file is created
Text is given to the file
Text is read from file and printed in the output screen
Code:
#include
main()
{
FILE *fp1; // a file is created with name fp1
int age,i,n;
char filename[25],name[25]; //string delaration
float salary;
printf("\nEnter the File Name \n");
scanf("%s",filename); //filename is obtained as input
fp1=fopen(filename,"w"); // file is opened and its mode is changed to ‘write’
according to sample output : ponnuviji file is opened and its mode is write and assigned to fp1. So text can be entered in the file.
printf("\nEnter the Number of Employees :");
scanf("%d",&n);
printf("\nEnter the Name,Salary and Age\n");
for(i=1;i<=n;i++)
{
fscanf(stdin,"%s%f%d",name,&salary,&age);//fscanffile scanf
ponnuviji file will read the input for variables name,salary,age
fprintf(fp1,"%s\t\t%f\t\t%d\n",name,salary,age); //fprintffile printf
the content read is written into file ponnuviji
}
fclose(fp1);//opened file is closed
//fprintf(stdout,"\n\n");
fp1=fopen(filename,"r"); //again the file is opened and its mode is changed to read
printf("\nName\t\tSalary\t\tAge\n\n");
for(i=1;i<=n;i++)
{
fscanf(fp1,"%s%f%d",name,&salary,&age); //values of variables name,salary and age is read from file
fprintf(stdout,"%s\t\t%f\t%d\n",name,salary,age); //data is retrieved from file and printed in the output screen
}
fclose(fp1); //opened file is then closed
}
OUTPUT:
[harita@localhost c]$ ./a.out
Enter the File Name
ponnuviji
Enter the Number of Employees : 2
Enter the Name, Salary and Age
hrithin
75000
28
harsha
50000
25
Name Salary Age
hrithin 75000.000000 28
harsha 50000.000000 25
ILLUSTRATION OF fseek() and ftell() FUNCTIONS
Create a file named Sample.dat cat>Sample.dat ,give some text
Code:
#include
main()
{
long int a=0;
int c;
FILE *fptr; // a file is created with name fptr
fptr=fopen("Sample.dat","r"); // Sample.dat file is opened ,its mode is read and then assigned to fptr
if(fptr!=NULL) // condition is file should have content
{
fseek(fptr,0L,0); // fseek sets the position of file to 0
while((c=getc(fptr))!=EOF) // getc(fptr) get a single character from fptr file
putchar(c); // put or write a single character on screen
upto end of file the while loop is executed
fseek(fptr,0L,2); // changes the position of file
a=ftell(fptr); //ftell gives the position of fptr to a
printf("\nThe Size of the file is : %d",a); //new position is printed
rewind(fptr); // moves to the initial position
a=ftell(fptr);// gives the position of fptr to a
printf("\nThe Size of the file is : %d\n",a);
}
else
printf("\nThe File does not exist\n");
fclose(fptr);
}
OUTPUT:
[harita@localhost c]$ ./a.out
INDIA WINS ICC WORLD CUP 2011
The Size of the file is : 30
The Size of the file is : 0<><>