Final C Lab Manual
Final C Lab Manual
Prepared By
N.Marimbi
WEEK 1
(i) Basic Linux environment and its editors like vi, vim,emac etc.
(ii) Exposure to turbo c, gcc.
(iii) Writing simple programs using printf () and scanf().
(i)Basic Linux environment and its editors like vi, vim, emac etc.
A Linux environment supports multiple text editors. There are two types of editors in Linux.
an area that contains information about the behaviour of programs and applications is called
environment.
Vi
Vi editor is a powerful and widely used text editor in UNIX and Linux operating system. It
allows us to create, edit and manage text files. Vim is the advanced version of vi editor. There
are three modes in vi: Command mode, Last Line Mode and Insert Mode..
Command Mode:
When vi starts up, it is in Command Mode. This mode is where vi interprets any characters we
type as commands and thus does not display them in the window. This mode allows us to move
through a file, and delete, copy, or paste a piece of text. Enter into Command Mode from any
other mode, requires pressing the [Esc] key. If we press [Esc] when we are already in
Command Mode, then vi will beep or flash the screen.
Insert mode:
This mode enables you to insert text into the file. Everything that’s typed in this mode is
interpreted as input and finally, it is put in the file. The vi always starts in command mode. To
enter text, you must be in insert mode. To come in insert mode, you simply type i. To get out
of insert mode, press the Esc key, which will put you back into command mode.
Last Line Mode (Escape Mode):
Line Mode is invoked by typing a colon [:], while vi is in Command Mode. The cursor will
jump to the last line of the screen and vi will wait for a command. This mode enables you to
perform tasks such as saving files and executing commands.
2.Basic linux environment and its editor like vi,vim&emac etc.
The default editor that comes with the UNIX operating system is called vi (visual editor). Using
vi editor, we can edit an existing file or create a new file from scratch. we can also use this editor
to just read a text file. The advanced version of the vi editor is the vim editor.
How to open VI editor?
To open vi editors, we just need to type the command mentioned below.
vi [file_name]
Here, [file_name] = this is the file name we want to create or to open the pre-existing file.
1) Creating a new file with `file_name` = geeksforgeeks
vi geeksforgeeks
2) Opening a preexisted file with `file_name` = jayesh
vi jayesh
Command Mode:
When vi starts up, it is in Command Mode. This mode is where vi interprets any characters we
type as commands and thus does not display them in the window. This mode allows us to move
through a file, and delete, copy, or paste a piece of text. Enter into Command Mode from any
other mode, requires pressing the [Esc] key. If we press [Esc] when we are already in
Command Mode, then vi will beep or flash the screen.
Insert mode:
This mode enables you to insert text into the file. Everything that’s typed in this mode is
interpreted as input and finally, it is put in the file. The vi always starts in command mode. To
enter text, you must be in insert mode. To come in insert mode, you simply type i. To get out
of insert mode, press the Esc key, which will put you back into command mode.
Last Line Mode (Escape Mode):
Line Mode is invoked by typing a colon [:], while vi is in Command Mode. The cursor will
jump to the last line of the screen and vi will wait for a command. This mode enables you to
perform tasks such as saving files and executing commands.
Linux vi Commands and Examples
NOTE: vi editor in Linux is a case-sensitive.
To enter in insert mode in vi editor in Linux we just need to press `i` on our keyboard and we
will be in insert mode. we can just start entering our content. (Refer to screenshot mentioned
below).
Commands Description
Positions cursor at
`0` beginning of line.
Position cursor to
previous work
`B`
To edit the file, we need to be in the insert mode. There are many ways to
enter insert mode from the command mode.
Command Description
#include<stdio.h>
int x;
scanf("%d",&x);
return 0;
}
Output:
Alogorithm:
Program Start
Declaring Variables
Input Three Numbers from User
Calculating Sum of Three Numbers (sum = x + y + z)
Displaying the Sum of Three Numbers
Calculating Average of Three Numbers (average = sum/count)
Displaying the Average of Three Numbers
Program End
Program:
#include<stdio.h>
int main()
{
//Declaring Three Variables
int x, y, z, sum;
float avg;//float can be defines numeric values with floating decimal point//
sum = x + y +z;
printf("Sum of Three Numebers is : %d", sum);
avg=sum/3;
printf("\n Average of Three Numebers is : %f", avg);
Program Start
Declaring Variables
Input Fahrenheit from User
Calculating Celsius c=(f-32)*5/9
Displaying the Celsius
Program End
#include <stdio.h>
int main()
float f;
float c;
printf("enter f value");
scanf("%f",&f);
printf("%f",c);
return 0;
Out put:
enter f value98
36.666668
2. B.) Celsius to Fahrenheit
#include <stdio.h>
int main()
float f;
float c;
printf("enter c value");
scanf("%f",&c);
f=(c*9)/5+32; //F=9/5*C+32//
printf("%f",f);
return 0;
Output:
enter c value38
100.400002
3. Aim: To write a c program simple interest.
#include <stdio.h>
int main()
float principle,time,rate,SI;
printf("enter principal(amount):");
scanf("%f", &principle);
printf("enter time:");
scanf("%f", &time);
printf("enter rate:");
scanf("%f", &rate);
return 0;
Output:
enter principal(amount):5000
enter time:2
enter rate:2
#include <math.h>
#include <stdio.h>
int main()
scanf("%lf", &number);
squareRoot = sqrt(number);
return 0;
Output:
Enter a number: 12
Where,
P is principle amount
R is the rate and
T is the time span
/**
*/
#include <stdio.h>
#include <math.h>
int main()
scanf("%f", &principle);
scanf("%f", &time);
scanf("%f", &rate);
return 0;
Output:
Enter time: 2
3. Aim: To write a c program to find the area of a triangle using heron’s formula .
Area = √(s*(s-a)*(s-b)*(s-c))
s = (a + b + c)/2 (Here s = semi perimeter and a, b, c are the three sides of a triangle)
#include<math.h>
int main()
scanf("%f%f%f",&a,&b,&c);
Perimeter = a+b+c;
s = (a+b+c)/2;
Area = sqrt(s*(s-a)*(s-b)*(s-c));
return 0;
Output:
8
Perimeter of Traiangle = 21.00
Program:
#include<stdio.h>
void main()
float u,a,t,v,s;
scanf("%f",&u);
scanf("%f",&a);
scanf("%f",&t);
v=u+a*t;
s=u*t+(1/2)*a*t*t;
Output:
Enter acceleration: 10
WEEK4
1. Aim: write a C program Find the max of three numbers using conditional operator
#include <stdio.h>
int main()
/*
*/
/*
* else
*/
return 0;
2. Aim: Write a C program, Take marks of 5 subjects in integers, and find the total,
average in float
#include <stdio.h>
int main()
return 0;
WEEK5
1. Aim: To Write a C program to find the max and min of four numbers using if-else.
#include<stdio.h>
int main()
{
if(num1>num2&&num1>num3&&num1>num4)
max=num1;
else if(num2>num3&&num2>num4)
max=num2;
else if(num3>num4)
max=num3;
else
max=num4;
printf("the max value is %d",max);
return 0;
Output:
Enter 4 numbers here...
10
50
80
90
the max value is 90
#include <stdio.h>
int main()
{
int unit;
float amt;
printf("Enter total units consumed: ");
scanf("%d", &unit);
return 0;
}
Output:
Enter total units consumed: 200
#include <math.h>
#include <stdio.h>
int main() {
double a, b, c, dis, root1, root2;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
dis = b * b - 4 * a * c;
if (dis > 0)
{
root1 = (-b + sqrt(dis)) / (2 * a);
root2 = (-b - sqrt(dis)) / (2 * a);
printf("real and different roots ,root1 = %f and root2 = %f", root1, root2);
}
else if (dis == 0)
{
root1 = root2 = -b / (2 * a);
printf("real and equal roots,root1 = root2 = %f;", root1);
}
else
{
printf("roots are imaginary");
}
return 0;
}
Output:
#include <stdio.h>
int main() {
char op;
double x, y;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &op);
printf("Enter two operands: ");
scanf("%lf %lf", &x, &y);
switch (op)
{
case '+':
printf("%f + %f = %f", x, y, x + y);
break;
case '-':
printf("%f -%f = %f", x, y, x - y);
break;
case '*':
printf("%f * %f = %f", x, y, x * y);
break;
case '/':
printf("%f /%f = %f", x, y, x / y);
break;
// operator doesn't match any case constant
default:
printf("Error! operator is not correct");
}
return 0;
}
Output:
20
5. Aim: To Write a C program to find the given year is a leap year or not.
#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
return 0;
}
Output:
WEEK 6
#include <stdio.h>
int main() {
int n, i;
unsigned long fact = 1;
printf("Enter an integer: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
fact =fact*i;
}
printf("Factorial of %d = %lu", n, fact);
return 0;
}
Output:
Enter an integer: 5
Factorial of 5 = 120
#include<stdio.h>
int main()
{
int n, i, flag = 0;
printf("Enter a number: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; i++)
{
if (n % i == 0)
{
flag = 1;
break;
}
}
if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);
return 0;
}
Output:
Enter a number: 23
23 s a prime number.
#include<stdio.h>
int main()
{
int i, n;
float x, sum, t;
printf(" Enter the value for x : ");
scanf("%f",&x);
x=x*3.14159/180;
//cos series
sum=1, t=1;
for(i=1;i<=n;i++)
{
t=t*(-1)*x*x/(2*i*(2*i-1));
sum=sum+t;
}
printf(" The value of Cos(%f) is : %.4f", x, sum);
//sin series
t=x;
sum=x;
for(i=1;i<=n;i++)
{
t=(t*(-1)*x*x)/(2*i*(2*i+1));
sum=sum+t;
}
printf(" \n The value of Sin(%f) = %.4f",x,sum);
return(0);
}
Output:
Enter the value for x : 30
Enter the value for n : 5
The value of Cos(0.523598) is : 0.8660
The value of Sin(0.523598) = 0.5000
#include <stdio.h>
int main()
{
int n, reverse = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}
return 0;
}
Output:
#include<stdio.h>
int main()
{
int rows,i,j;
printf("Number of rows: ");
scanf("%d", &rows);
Output:
WEEK 7
1. Aim: To write a c progeam to Find the min and max of a 1-D integer array.
#include <stdio.h>
int main()
{
int a[10], i, n, max,min;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
scanf("%d", &a[i]);
}
max = a[0];
min =a[0];
for(i=1; i<n; ++i)
{
if(a[i]>big)
max = a[i];
if(a[i]< min)
min = a[i];
}
printf("max is %d \n", max);
printf("min is %d", min);
}
Output:
Enter number of elements: 5
12 3 4 5 10
max is 12
min is 3
#include <stdio.h>
int main() {
int a[10], i, n, found=0,search;
printf("Enter number of elements: ");
scanf("%d", &n);
for(i=0; i<n; ++i)
{
scanf("%d", &a[i]);
}
printf("enter search key:");
scanf("%d",&search);
return 0;
}
Output:
Enter number of elements: 5
12 3 4 5 6
enter search key:4
element found
int main()
{
int n, a[10], rev[10], i,j;
printf("Enter the size of the array: ");
scanf("%d", &n);
printf("Enter the elements: ");
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
j=n-1;
for(i = 0; i <n; i++)
{
rev[j] = a[i];
j--;
}
printf("The Reversed array: ");
for(i = 0; i < n; i++)
{
printf("%d ", rev[i]);
}
return(0);
}
Output:
Enter the size of the array: 5
Enter the elements: 3 4 5 6 7
The Reversed array: 7 6 5 4 3
#include <stdio.h>
int main()
{
char binary[20], onesComp[20], twosComp[20];
int i, n,carry=1;
printf("Enter the no of bits :");
scanf("%d",&n);
printf("Enter the binary data ");
scanf("%s",&binary);
return 0;
}
Output:
Program:
#include<stdio.h>
#include<stdlib.h>
int main(){
int a[50],i,j,k, count = 0, dup[50], number;
printf("Enter size of the array");
scanf("%d",&number);
printf("Enter Elements of the array:");
for(i=0;i<number;i++){
scanf("%d",&a[i]);
dup[i] = -1;
}
printf("Entered element are: ");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
for(i=0;i<number;i++){
for(j = i+1; j < number; j++){
if(a[i] == a[j]){
for(k = j; k <number; k++){
a[k] = a[k+1];
}
j--;
number--;
}
}
}
printf("After deleting the duplicate element the Array is:");
for(i=0;i<number;i++){
printf("%d ",a[i]);
}
}
Output:
Entered element are: 2 2 2 2 2 after deleting the duplicate element the Array is
:2
WEEK8
Output:
enter number of rows and columns for matrix a:2 2
enter number of rows and columns for matrix b:2 2
enter matrix a value :
12
34
enter matrix b value:
34
56
addition is:
4 6
8 10
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],c[10][10];
int i,j,k,m1,m2,n1,n2;
printf("enter no.of rows and columns for matrix a:");
scanf("%d %d",&m1,&n1);
printf("enter no.of rows and columns for matrix b:");
scanf("%d %d",&m2,&n2);
if(n1!=m2)
printf("multiplication is not possible");
else
{
printf("enter matrix a value:\n");
for(i=0;i<m1;i++)
for(j=0;j<n1;j++)
scanf("%d",&a[i][j]);
printf("enter matrix b value:\n");
for(i=0;i<m2;i++)
for(j=0;j<n2;j++)
scanf("%d",&b[i][j]);
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
{
c[i][j]=0;
for(k=0;k<n1;k++)
{
c[i][j]=c[i][j]+(a[i][k]*b[j][k]);
}
}
}
printf("multiplication is :\n");
for(i=0;i<m1;i++)
{
for(j=0;j<n2;j++)
printf("%d ",c[i][j]);
printf("\n");
}
}
}
Output:
enter no.of rows and columns for matrix a:2 2
enter no.of rows and columns for matrix b:2 2
enter matrix a value:
12
34
enter matrix b value:
34
56
multiplication is :
11 17
25 39
#include<stdio.h>
#include<string.h>
int main()
{
char x[10][15];
char temp[60];
int i,j,n;
printf("enter n value");
scanf("%d",&n);
fflush(stdin);
printf("Enter the strings\n");
for(i=0;i<n;i++)
gets(x[i]);
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(strcmp(x[i],x[j])>0)
{
strcpy(temp,x[i]);
strcpy(x[i],x[j]);
strcpy(x[j],temp);
}
printf("sorted strings are:\n");
for(i=0;i<n;i++)
puts(x[i]);
return(0);
}
Output:
enter n value2
Enter the strings
rani
latha
sorted strings are:
latha
rani
}
for(j=0;string2[j]!='\0';j++,i++)
{
string1[i]=string2[j];
}
string1[i]='\0';
printf("the concatenate string is %s",string1);
}
Output:
enter string1hello
enter string2srkit
the concatenate string is hellosrkit
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20],rev[20];
int i,j;
printf("enter string:");
gets(str1);
for(i=0;str1[i]!='\0';i++){}
i=i-1;
for(j=0;i>=0;j++,i--)
rev[j]=str1[i];
rev[j]='\0';
printf("reverse without library function %s",rev);
printf(" \nthe reverse string with library function used %s",strrev(str1));
return 0;
}
Output:
enter string:hello
reverse without library function olleh
the reverse string with library function used olleh
WEEK9
#include<stdio.h>
#include<malloc.h>
int main()
{
int i,n,*p;
int sum = 0;
2. Aim: To Write a C program to find the total, average of n students using structures
#include <stdio.h>
#include <stdlib.h>
struct student
{
int rno;
char name[30];
int marks[6];
};
int main()
{
struct student *p;
int n,i,j,total=0;
float avg;
printf("Enter the number of students: ");
scanf("%d", &n);
p = (struct student *)malloc(n * sizeof(struct student));
for (i = 0; i < n; ++i)
{
printf("Enter roll no,name and marks:\n");
scanf("%d %s", &p[i].rno,p[i].name);
for(j=0;j<6;j++)
scanf("%d", &p[i].marks[j]);
}
Output:
3. Aim: To Enter n students data using calloc() and display failed students list
# include <stdio.h>
#include<stdlib.h>
struct student
{
char name[10];
int m[6];
}*p;
void main()
{
int i,j,n;
}
printf("failed student names:\n");
for(i=0;i<n;i++)
{
for(j=0;j<6;j++)
if((p[i].m[j])<35)
{
printf("%s\n",p[i].name );
break;
}
}
}
Output:
4. Aim: To Read student name and marks from the command line and display the student
details along with the total.
#include <stdio.h>
#include<stdlib.h>
return 0;
}
Output:
./a.out ajay 20 30
# include <stdio.h>
#include<stdlib.h>
int main()
{
int *p,i;
p = (int *)malloc(sizeof(int));
p[0] = 1;
//realloc memory size to store 3 integers
p = (int *)realloc(p, 3* sizeof(int));
p[1] = 2;
p[2] = 3;
//printing values
for(i = 0; i < 3; i++)
printf("%d\n",p[i]);
free(p);
return 0;
}
Output:
1
2
3
WEEK10
1. Aim: To create and display a singly linked list using self-referential structure.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int num; //Data of the node
struct node *nextptr; //Address of the next node
}*stnode;
int main()
{
int n;
printf("\n\n Linked List : To create and display Singly Linked List :\n");
printf("-------------------------------------------------------------\n");
if(stnode == NULL) //check whether the fnnode is NULL and if so no memory allocation
{
printf(" Memory can not be allocated.");
}
else
{
// reads data for the node through keyboard
Output:
2. Aim: To Demonstrate the differences between structures and unions using a C program.
Union program:
#include<stdio.h>
union item
{
int x;
int y;
};
int main( )
{
union item it;
it.y = 20;
it.x = 12;
Output:
size of item is 4
12
12
Structure Program:
#include<stdio.h>
struct item
{
int x;
int y;
};
int main( )
{
struct item it;
it.y = 20;
it.x = 12;
Output:
size of item is 8
12
20
3. Aim: To Write a C program to shift/rotate using bitfields.
#include<stdio.h>
int main()
{
int n = 16;
int d = 2;
printf("Left Rotation of %d by %d is ", n, d);
printf("%d", n<<d);
printf("\nRight Rotation of %d by %d is ", n, d);
printf("%d", n>>d);
return (0);
}
Output:
Left Rotation of 16 by 2 is 64
Right Rotation of 16 by 2 is 4
4. Aim: To Write a C program to copy one structure variable to another structure of the
same type.
struct student
{
char name[20];
char country[20];
};
void main()
{
struct student S={"ajay","india"};
struct student X;
X=S;
printf("%s %s",X.name,X.country);
}
Output:
size of item is 40
ajay india
WEEK 11
#include <stdio.h>
long factorial(int n)
{
int i;
long result = 1;
for (i = 1; i <= n; i++)
result = result*i;
return result;
}
int main()
{
int n, r;
long ncr;
printf("Enter the value of n and r:");
scanf("%d %d",&n,&r);
ncr = factorial(n)/(factorial(r)*factorial(n-r));
Output:
Enter the value of n and r :5 2
5C2 = 10
#include<stdio.h>
int length(char str[20])
{
int i;
for(i=0;str[i]!='\0';i++)
{
}
return(i);
}
main()
{
int result;
result=length("hello");
printf("length of string is %d\n",result);
printf("length of string is %d\n",length("hai"));
}
Output:
length of string is 5
length of string is 3
#include<stdio.h>
int m,n;
}
void displaymatrix(int a[10][10])
{
int i,j;
printf("The transposed matrix is:\n");
for(i=0; i<n; i++)
{
for(j=0; j<m; j++)
printf("%d ", a[i][j]);
printf("\n");
}
int main(){
int a[10][10];
printf("Enter the number of row and columns: ");
scanf("%d %d",&m,&n);
readmatrix(a);
transposematrix(a);
displaymatrix(a);
return(0);
Output:
Enter the number of row and columns: 3 3
Enter the elements of the matrix:
123
456
789
The transposed matrix is:
147
258
36 9
#include<stdio.h>
}
Output:
Enter x0,y0,h,xn: 0 1 0.1 1
x y
0.100 1.100
0.200 1.220
0.300 1.362
0.400 1.528
0.500 1.721
0.600 1.943
0.700 2.197
0.800 2.487
0.900 2.816
1.000 3.187
WEEK 12
Fibonacci Series in C: In case of fibonacci series, next number is the sum of previous two
numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series
are 0 and 1.
#include<stdio.h>
void fibonacci(int n)
{
static int n1=0,n2=1,n3;
if(n>2)
{
n3 = n1 + n2;
n1 = n2;
n2 = n3;
printf(" %d",n3);
fibonacci(n-1);
}
}
int main()
{
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d %d",0,1);
if(n>2)
fibonacci(n);
return 0;
}
Output:
#include <stdio.h>
int main()
{
int a, b, result;
Output:
#include <stdio.h>
if (n<1)
return (1);
else
return ( n*factorial(n-1));
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, factorial(n));
return 0;
}
Output:
#include<stdio.h>
int ack(int m, int n)
{
if(m==0)
return n+1;
else if(n==0)
return ack(m-1,1);
else
return ack(m-1,ack(m,n-1));
}
void main()
{
int m,n;
printf("Enter two numbers :: \n");
scanf("%d %d",&m,&n);
printf("\nresult is :: %d\n",ack(m,n));
}
Output:
result :: 1021
#include<stdio.h>
int series (int n)
{
if (n<1)
return (0);
else
return ( n+series(n-1));
}
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("sum of series of %d = %d", n, series(n));
return 0;
}
Output:
WEEK13
#include <stdio.h>
void swap( int *var1, int *var2 )
{
int temp ;
temp = *var1 ;
*var1 = *var2 ;
*var2 = temp;
}
int main()
{
int num1, num2;
printf("\n Enter 2 numbers:");
scanf("%d %d", &num1,&num2);
printf("Before swapping: %d, %d", num1, num2);
/*calling swap function*/
swap(&num1, &num2);
printf("\nafter swapping: %d, %d", num1, num2);
}
Output:
Enter 2 numbers: 12 45
When the variable goes out of the scope then the pointer pointing to the variable becomes
a dangling pointer.
dangling pointer
#include<stdio.h>
int main()
{
char *str;
{
char a = ‘A’;
str = &a;
}
printf("%c", *str);
}
Output:
A
3.Aim: To Write a C program to copy one string into another using pointer.
#include<stdio.h>
void copystrings(char *source, char *dest)
{
while (*source)
{
*dest = *source;
source++;
dest++;
}
*dest = '\0';
}
int main() {
char source[100], dest[100];
return 0;
Output:
#include<stdio.h>
void count(char *str)
{
int lower=0,upper=0,digit=0,other=0;
while (*str)
{
if(*str>=65&&*str<=90)
upper++;
else if(*str>=97&&*str<=122)
lower++;
else if(*str>=48&&*str<=57)
digit++;
else
other++;
str++;
}
printf("lower count=%d,upper count=%d,digit count=%d,other=%d",lower,upper,digit,other);
}
int main() {
char str[100];
Output:
Enter the string:
helloSRK1234@#$ hh
lower count=7,upper count=3,digit count=4,other=4
WEEK14
1. Aim: To Write a C program to write and read text into a file.
#include<stdio.h>
int main()
{
FILE *fp1,*fp2;
char c;
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","w");
while((c=getc(fp1))!=EOF)
{
putc(c,fp2);
printf(“%c”,c);
}
fclose(fp1);
fclose(fp2);
return 0;
}
Output:
2. Aim: To Write a C program to write and read text into a binary file using fread() and
fwrite()
#include<stdio.h>
struct student{
int sno;
char sname [30];
float marks;
};
int main ( )
{
struct student s[60];
int i;
FILE *fp;
fp = fopen ("student.bin", "wb");
for (i=0; i<2; i++)
{
printf ("enter details of student %d", i+1);
printf("student number:");
scanf("%d",&s[i].sno);
printf("student name:");
gets(s[i].sname);
printf("student marks:");
scanf("%f",&s[i].marks);
fwrite(&s[i], sizeof(s[i]),1,fp);
}
fclose (fp);
fp = fopen ("student1.bin", "rb");
for (i=0; i<2; i++)
{
printf ("details of student %d are", i+1);
fread (&s[i], sizeof (s[i]) ,1,fp);
printf("student number = %d", s[i]. sno);
printf("student name = %s", s[i]. sname);
printf("marks = %f", s[i]. marks);
}
fclose(fp);
return(0);
}
Output
enter details of student 1
student number:1
student name:pinky
student marks:56
enter details of student 2
student number:2
student name:rosy
student marks:87
details of student 1 are
student number = 1
student name = pinky
marks = 56.000000
details of student 2 are
student number = 2
student name = rosy
marks = 87.000000
#include<stdio.h>
int main()
{
FILE *fp1,*fp2;
char c;
fp1=fopen("file1.txt","r");
fp2=fopen("file2.txt","w");
while((c=getc(fp1))!=EOF)
{
putc(c,fp2);
printf(“%c”,c);
}
fclose(fp1);
fclose(fp2);
return 0;
}
Output:
We are SRKIT CSM students
4. Aim: To Write a C program to merge two files into the third file using command-line
arguments.
Program:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// Open two files to be merged
FILE *fp1 = fopen(argv[1], "r");
FILE *fp2 = fopen(argv[2], "r");
#include<stdio.h>
int main() {
FILE *fp;
char ch;
int n,length;
printf("Enter the value of n : ");
scanf("%d", &n);
fp = fopen("file1.txt", "r");
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fseek(fp, (length - n), SEEK_SET);
do {
ch = getc(fp);
putchar(ch);
} while (ch != EOF);
fclose(fp);
return(0);
}
Output: