0% found this document useful (0 votes)
8 views62 pages

CP Lab

The document is a practical record notebook for the CS3271 Programming in C Laboratory at Indira Institute of Engineering & Technology, detailing various experiments conducted during the academic year 2023-2024. It includes sample C programs for calculating the area and circumference of a circle, decision-making constructs, loops, arrays, strings, functions, recursion, and pointers. Each experiment contains the aim, algorithm, source code, output, and results for the respective programming tasks.

Uploaded by

yaminishrvani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views62 pages

CP Lab

The document is a practical record notebook for the CS3271 Programming in C Laboratory at Indira Institute of Engineering & Technology, detailing various experiments conducted during the academic year 2023-2024. It includes sample C programs for calculating the area and circumference of a circle, decision-making constructs, loops, arrays, strings, functions, recursion, and pointers. Each experiment contains the aim, algorithm, source code, output, and results for the respective programming tasks.

Uploaded by

yaminishrvani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 62

INDIRA INSTITUTE OF ENGINEERING &TECHNOLOGY

PANDUR, THIRUVALLUR

PRACTICAL RECORD NOTE BOOK

ANNA UNIVERSITY CHENNAI

CS3271 PROGRAMMING IN C LABORATORY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

IIET-CSE-THIRUVALLUR
INDIRA INSTITUTE OF ENGINEERING &TECHNOLOGY
Pandur, Thiruvallur – 631 203

University Register No:

CERTIFICATE
Certified to be bonafide record of work done by _______________________ on 2ND

Semester B.E. (CSE) in the CS3271 PROGRAMMING IN C LABORATORY during the

academic year 2023-2024.

FACULTY-IN-CHARGE HEAD OF THE DEPARTMENT

Submitted for the University Practical Examination held on _________________

Internal Examiner External Examiner

IIET-CSE-THIRUVALLUR
INDEX
PAGE. STAFF
EXP.NO DATE EXPERIMENT NAME
NO SIGNATURE

IIET-CSE-THIRUVALLUR
EXP No. 1
Programs Using I/O Statements And Expressions
Write a C program to find the area and circumference of the circle.

Aim

Algorithm

Source Code

#include<stdio.h>
int main()
{
int rad;
float PI = 3.14, area, ci;
printf("\nEnter radius of circle: ");
scanf("%d", &rad);
area = PI * rad * rad;
printf("\nArea of circle : %f ", area);
ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);
return (0);
}

IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output
Enter radius of circle :6
Area of circle :113.040001
Circumference :37.680000

Result

IIET-CSE-THIRUVALLUR
EXP No.
2(a) Decision making constructs (If-else, goto,switch-case, break-
continue)
Write a program in C using If-else to determine Whether a person is Eligible for Voting
or Not

Aim

Algorithm

Source Code :

#include<stdio.h>
#include<conio.h
int main()
{
int a,b,c;
printf(“Enter the three numbers: “);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b&&a>c)
printf(“The biggest number is: %d”,a);
else if(b>a&&b>c)
printf(“The biggest number is: %d”,b);
else
printf(“The biggest number is: %d”,c);
}

IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output:
Enter the three numbers:
758
The biggest number is: 8

Result:

IIET-CSE-THIRUVALLUR
EXP No.
2(b) Decision making constructs (If-else, goto, switch-case,
break-continue)

Write a C program to find and print the present Elevator position using switch case.

Aim

Algorithm

Source Code :
#include <stdio.h>
int main()
{
int a;
printf("Enter Floor No : ");
scanf("%d",&a);
switch(a)
{
case 1:
printf("\nFirst Floor");
break;
case 2:
printf("\nSecond Floor");

IIET-CSE-THIRUVALLUR
break;

case 3:
printf("\nThird Floor");
break;
default:
printf("Invalid No");
break;
}
return 0;
}

Output :
Enter Floor No : 3
Third Floor

Result.

IIET-CSE-THIRUVALLUR
EXP No.
2(c) Decision making constructs (If-else, goto, switch-case,
break-continue)

Write a C program to read N positive integer numbers.

Note : If any input is Zero, program should take the input again, if any input is negative
the loop should be terminated.

Aim

Algorithm

IIET-CSE-THIRUVALLUR
Source Code :

#include <stdio.h>
int main()
{
int number;
while(1)
{
printf("Enter integer number: ");
scanf("%d",&number);
if(number==0)
{
printf("Invalid input...\n");
continue;

IIET-CSE-THIRUVALLUR
}

else if(number<0)
{
printf("Terminating loop...\n");
break;
}
printf("Number is: %d\n",number);
}
printf("Bye, Bye...\n");

return 0;
}

Output :
Enter integer number : 10
Number is : 10

Enter integer number : 20


Number is : 20

Enter integer number : 0


Invalid input…

Enter integer number : 30


Number is : 30

Enter integer number : -1


Terminating loop…
Bye,Bye…

Result.

IIET-CSE-THIRUVALLUR
EXP No.
3(a)
Loops : for, while, do-while

Write a C program to find a factorial of a number

Aim

Algorithm

Source Code.
#include <stdio.h>
#include <conio.h>
void main()
{
int i, a ,f = 1;
printf("Enter a number:\n");
scanf("%d", &a);
for (i = 1; i <= a; i++)
f=f*i;
printf("Factorial of %d is %d\n", a, f);
getch();
}

IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output.
Enter a number : 5

Factorial of 5 is 120.

Result.

IIET-CSE-THIRUVALLUR
EXP No.
3(b)
Loops : for, while, do-while

Write a C program to find a given number is Armstrong or not.

Aim

Algorithm

IIET-CSE-THIRUVALLUR
Source Code
}

Temp = Number;
while( Temp > 0)
{

Reminder = Temp %10;

Sum = Sum + pow(Reminder, Times);


Temp = Temp /10;

}
if ( Number == Sum )

printf("\n %d is Armstrong Number.\n",


Number); else

printf("\n %d is not a Armstrong Number.\n", Number);


return 0;

Output :
Please Enter number to Check for Armstrong

153
153 is Armstrong Number.

Result.

IIET-CSE-THIRUVALLUR
EXP No.
4(a)
Arrays : 1D and 2D, Multi-dimensional arrays, Traversal

Write a C program to sort one dimensional array elements in ascending order.

Aim
.

Algorithm

Source Code

#include <stdio.h>
#define MAX 100
int main()
{
int arr[MAX], n, i, j;
int temp;
printf("Enter total number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for (i = 0; i < n; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
if (arr[i] > arr[j]) {
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
printf("\nArray elements after sorting:\n");
for (i = 0; i < n; i++) {
printf("%d\n", arr[i]);
}
return 0;
}

Output.
Enter total number of elements : 5

Enter array elements :


Enter array element 1 : 100
Enter array element 2 : 999
Enter array element 3 : 200
Enter array element 4 : 800
Enter array element 5 : 300

Array elements after sorting :


100
200
300
800
999

Result

IIET-CSE-THIRUVALLUR
EXP No.
4(b)
Arrays : 1D and 2D, Multi-dimensional arrays, Traversal

Write a C program for addition of two matrices

Aim:

Algorithm:

Source Code

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3], i, j, k;
clrscr();
printf("\nEnter the elements of A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &a[i][j]);
}
}
printf("\n\nEnter the Elements of Matrix B\n");

IIET-CSE-THIRUVALLUR
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]= a[i][j] + b[i][j];
}
}
printf("\n\nThe sum of the two given matrices is\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
getch();
}

Output:

Enter the elements of A


1 2 3
4 5 6
7 8 9

Enter the Elements of Matrix B


9 8 7
6 5 4
3 2 1

The sum of the two given matrices is


10 10 10
10 10 10
10 10 10
Result:

IIET-CSE-THIRUVALLUR
EXP No. 5
Strings : Operations

Write a program in C to count the total number of vowel or consonant in a string

Aim

Algorithm

Source Code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define str_size 100 //Declare the maximum size of the string

void main()
{
char str[str_size];
int i, len, vowel, cons;

IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
printf("\n\nCount total number of vowel or consonant :\n");
printf(" \n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);

vowel = 0;
cons = 0;
len = strlen(str);

for(i=0; i<len; i++)


{

if(str[i] =='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E'


|| str[i]=='I' || str[i]=='O' || str[i]=='U')
{
vowel++;
}
else if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
cons++;
}
}
printf("\nThe total number of vowel in the string is : %d\n", vowel);
printf("The total number of consonant in the string is : %d\n\n", cons);
}

IIET-CSE-THIRUVALLUR
Output
Count total number of vowel and consonant :

Input the string : C Programming Laboratory

The total number of vowel in the string is : 7


The total number of consonant in the string is : 15

Result

IIET-CSE-THIRUVALLUR
EXP No. 6
Functions: Call, Return, Passing Parameters By (Value, Reference),
Passing Arrays To Function.

Write a C program to Swap two numbers using Call by reference

Aim

.
Algorithm

Source Code

#include <stdio.h>
void swap(int*, int*); //Swap function declaration
int main()
{
int x, y;
printf("Enter the value of x and y\n");
scanf("%d%d",&x,&y);

printf("Before Swapping\nx = %d\ny = %d\n", x, y);

swap(&x, &y);

printf("After Swapping\nx = %d\ny = %d\n", x, y);

return 0;
}
//Swap function definition
void swap(int *a, int *b)
{
int t;
t = *b;
*b = *a;
*a = t;
}
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output

Enter the value of x and y


45
90

Before Swapping
x =45
y = 90

After Swapping
x=90
y=45

Result

IIET-CSE-THIRUVALLUR
EXP No. 7
Recursion

Write C Program to find the factorial of the given number

Aim
.
.

Algorithm

Source Code

#include<stdio.h>
#include<conio.h>

void main()

int n;
long int fact();
clrscr();
printf("enter the number whose factorial is
to be found"); scanf("%d",&n);
printf("the factorial of %d is: %d
\n",n,fact(n)); getch();
}

IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
long int fact(n)
{
int n;
if(n==0)
return(1);
else
return(n*fact(n-1);
}

Output:
enter the number whose factorial is to
be found:6 The factorial of 6 is: 720

Result:

IIET-CSE-THIRUVALLUR
EXP No.
8(a) Pointers : Pointers to functions, Arrays, Strings, Pointers to Pointers,
Arrays of Pointers

Write a C program to compute the sum of all elements stored in an array using Pointers

Aim

Algorithm

Source Code

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10];
int i,sum=0;
int*p;
printf("Enter 10 elements\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
p=a;
for(i=0;i<10;i++)
{
sum=sum+*p;
p++;
}
printf("The sum of array elements is %d",sum);
}

IIET-CSE-THIRUVALLUR
Output
Enter 10 elements :
1
2
3
4
5
6
7
8
9
12
The sum of array elements is 57

Result

IIET-CSE-THIRUVALLUR
EXP No.
8(b) Pointers : Pointers to functions, Arrays, Strings, Pointers to
Pointers, Arrays of Pointers

Write a C program using pointer to determine the length of a character string

Aim

Algorithm

Source Code
#include<stdio.h>
#include<conio.h>
int string_ln(char*);

void main() {
char str[20];
int length;
clrscr();

printf("\nEnter any string : ");


gets(str);

length = string_ln(str);
printf("The length of the given string %s is : %d", str,
length); getch();
}

int string_ln(char*p) /* p=&str[0] */


{
int count = 0;
while (*p != '\0') {
count++;
p++;
}
return count;
}

IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output.
Enter any string:
Technology
The length of the given string Technology is : 10

Result.

IIET-CSE-THIRUVALLUR
EXP No.
9(a) Structures : Nested Structures, Pointers to Structures, Arrays of
Structures and Unions
Write a C program that gets and display the report of the student details using Nested
Structures

Aim

Algorithm

Source Code.

IIET-CSE-THIRUVALLUR
{
struct student stud;

printf("Enter name and roll number of student:\n");


scanf("%s%d",stud.name, &stud.roll);
printf("Enter street name, house number and state number:\n");
scanf("%s%d%d",stud.adrs.street, &stud.adrs.houseno, &stud.adrs.stateno);
printf("Student detail is:\n");
printf("Name: %s\tRoll: %d\n", stud.name, stud.roll);
printf("Address:%s, House no. -%d, state: %d",stud.adrs.street, stud.adrs.houseno,
stud.adrs.stateno);

return 0;
}

Output.
Enter name and roll number of student:
Ramesh
17
Enter street name, house number and state number:
Jawagarnagar
23
45

Name :Ramesh Roll: 17


Address : Jawagarnagar, House no.-23, State:45

Result

IIET-CSE-THIRUVALLUR
EXP No.
9(b) Structures : Nested Structures, Pointers to Structures, Arrays of
Structures and Unions

Write a 'C' program to display the student Name, Marks and Percentage using Union

Aim..

Algorithm.

Source Code.
#include <stdio.h>
#include <string.h>

union student
{
char name[20];
char subject[20];
float percentage;
};

int main()
{
union student record1;
union student record2;
// assigning values to record1 union variable
printf("Union record1 values example\n");
strcpy(record1.name, "Raju");
printf(" Name : %s \n", record1.name);

IIET-CSE-THIRUVALLUR
strcpy(record1.subject, "Maths");
printf(" Subject : %s \n", record1.subject);
record1.percentage = 86.50;
printf(" Percentage : %f \n\n", record1.percentage);

// assigning values to record2 union variable


printf("Union record2 values example\n");
strcpy(record2.name, "Mani");
printf(" Name : %s \n", record2.name);
strcpy(record2.subject, "Physics");
printf(" Subject : %s \n", record2.subject);
record2.percentage = 99.50;
printf(" Percentage : %f \n", record2.percentage);
return 0;
}

Output.
Union record1 values example
Name : Raju
Subject : Maths
Percentage : 86.500000

Union record2 values example


Name : Mani
Subject : Physics
Percentage : 99.500000

Result.

IIET-CSE-THIRUVALLUR
EXP No.
10(a) Files : reading and writing, File pointers, file operations,
random access, processor directives.

Write a C program to create, write and read text in/from file

Aim.

Algorithm

Source Code.

#include< stdio.h >


int main()
{
FILE *fp; /* file pointer*/
char fName[20];

printf("\nEnter file name to create :");


scanf("%s",fName);

/*creating (open) a file*/

IIET-CSE-THIRUVALLUR
fp=fopen(fName,"w");

/*check file created or not*/


if(fp==NULL)
{

printf("File does not created!!!");

exit(0); /*exit from program*/


}
printf("File created successfully.");

/*writting into file*/


putc('A',fp);

putc('B',fp);
putc('C',fp);

printf("\nData written successfully.");

fclose(fp);
/*again open file to read data*/
fp=fopen(fName,"r");

if(fp==NULL)
{
printf("\nCan't open file!!!");
exit(0);

}
printf("Contents of file is :\n");
printf("%c",getc(fp));

printf("%c",getc(fp));

printf("%c",getc(fp));
fclose(fp);

return 0;
}

IIET-CSE-THIRUVALLUR
Output.
Enter file name to create : program.txt

File created successfully.

Data written successfully.

Content of file is.

ABC

Result.

IIET-CSE-THIRUVALLUR
EXP No.
10(b) Files : reading and writing, File pointers, file operations,
random access, processor directives.

Telephone Directory using Random Access File.

Aim.

Algorithm.

IIET-CSE-THIRUVALLUR
Source Code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Phonebook_Contacts
{
char FirstName[20];
char LastName[20];
char PhoneNumber[20];
} phone;
void AddEntry(phone * );
void DeleteEntry(phone * );
void PrintEntry(phone * );
void SearchForNumber(phone * );
int counter = 0;
char FileName[256];

IIET-CSE-THIRUVALLUR
FILE *pRead;
FILE *pWrite;
void main (void)
{
phone *phonebook;
int iSelection=0;
phonebook = (phone*)
malloc(sizeof(phone)*100); //int iSelection = 0;
if (phonebook == NULL)
{
printf("Out of Memory. The program will now exit");
}
else {}
do
{
printf("\n\t\t\tPhonebook Menu");
printf("\n\n\t(1)\tAdd Friend");
printf("\n\t(2)\tDelete Friend");
printf("\n\t(3)\tDisplay Phonebook Entries");
printf("\n\t(4)\tSearch for Phone Number");
printf("\n\t(5)\tExit Phonebook");
printf("\n\nWhat would you like to do? ");
scanf("%d", &iSelection);
if (iSelection == 1)
{
AddEntry(phonebook);
}
if (iSelection == 2)
{
DeleteEntry(phonebook);

IIET-CSE-THIRUVALLUR
}
if (iSelection == 3)
{
PrintEntry(phonebook);
}
if (iSelection == 4)
{
SearchForNumber(phonebook);
}
if (iSelection == 5)
{
printf("\nYou have chosen to exit the Phonebook.\n");
getch();
}
} while (iSelection<= 4);
}
void AddEntry (phone * phonebook)
{
pWrite = fopen("phonebook_contacts.dat", "a");
if ( pWrite == NULL )
{
perror("The following error occurred
"); exit(EXIT_FAILURE);
}
else
{
counter++;
realloc(phonebook, sizeof(phone));
printf("\nFirst Name: ");

scanf("%s", phonebook[counter-1].FirstName);

IIET-CSE-THIRUVALLUR
printf("Last Name: ");
scanf("%s", phonebook[counter-1].LastName);
printf("Phone Number (XXX-XXX-XXXX): ");
scanf("%s", phonebook[counter-1].PhoneNumber);
printf("\n\tFriend successfully added to Phonebook\n");
fprintf(pWrite, "%s\t%s\t%s\n", phonebook[counter-1].FirstName, phonebook[counter-
1].LastName, phonebook[counter-1].PhoneNumber);
fclose(pWrite);
}
}
void DeleteEntry (phone * phonebook)
{
int x = 0;
int i = 0;
char deleteFirstName[20]; //
char deleteLastName[20];
printf("\nFirst name: ");
scanf("%s", deleteFirstName);
printf("Last name: ");
scanf("%s", deleteLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(deleteFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(deleteLastName, phonebook[x].LastName) == 0)
{
for ( i = x; i < counter - 1; i++ )
{
strcpy(phonebook[i].FirstName, phonebook[i+1].FirstName);
strcpy(phonebook[i].LastName, phonebook[i+1].LastName);
strcpy(phonebook[i].PhoneNumber, phonebook[i+1].PhoneNumber);

IIET-CSE-THIRUVALLUR
}
printf("Record deleted from the phonebook.\n\n");
--counter;
return;
}
}
}
printf("That contact was not found, please try again.");
}
void PrintEntry (phone * phonebook)
{
int x = 0;
printf("\nPhonebook Entries:\n\n ");
pRead = fopen("phonebook_contacts.dat", "r");
if (pRead == NULL)
{
perror("The following error occurred: ");
exit(EXIT_FAILURE);
}
else
{
for( x = 0; x < counter; x++)
{
printf("\n(%d)\n", x+1);
printf("Name: %s %s\n", phonebook[x].FirstName, phonebook[x].LastName);
printf("Number: %s\n", phonebook[x].PhoneNumber);
}
}

IIET-CSE-THIRUVALLUR
void SearchForNumber (phone * phonebook)
{
int x = 0;
char TempFirstName[20];
char TempLastName[20];
printf("\nPlease type the name of the friend you wish to find a number for.");
printf("\n\nFirst Name: ");
scanf("%s", TempFirstName);
printf("Last Name: ");
scanf("%s", TempLastName);
for (x = 0; x < counter; x++)
{
if (strcmp(TempFirstName, phonebook[x].FirstName) == 0)
{
if (strcmp(TempLastName, phonebook[x].LastName) == 0)
{
printf("\n%s %s's phone number is %s\n", phonebook[x].FirstName,
phonebook[x].LastName, phonebook[x].PhoneNumber);
}
}
}
}

IIET-CSE-THIRUVALLUR
Output .
Phonebook Menu

1. Add Friend
2. Delete Friend
3. Display Phonebook Entries
4. Search for Phone Number
5. Exit Phonebook
What would you like to do ? 1
First Name : Arun

Last Name : Kumar


Phone Number (xxx-xxx-xxx) : 568-547-254
Friend successfully added to Phonebook
Phonebook Menu

1. Add Friend
2. Delete Friend
3. Display Phonebook Entries
4. Search for Phone Number
5. Exit Phonebook

What would you like to do ? 5

You have chosen to exit the Phonebook

Result.

IIET-CSE-THIRUVALLUR
CONTENT BEYOND SYLLABUS

IIET-CSE-THIRUVALLUR
EXP No.
11(a)
PRODUCT OF TWO MATRICES

Aim.

Algorithm:

Source Code

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][3], b[3][3], c[3][3], i, j, k;
printf("\nEnter the elements of A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &a[i][j]);

IIET-CSE-THIRUVALLUR
}
}
printf("\n\nEnter the Elements of Matrix B\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)

{
scanf("%d", &b[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
printf("\nThe Product of the Two Matrices are\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t", c[i][j]);
}
printf("\n");
}
getch();
}

Output:
Enter the elements of A
1 2 34 5 67 8 9
Enter the Elements of Matrix B
9 8 76 5 43 2 1
The Product of the Two Matrices are
30 24 18

84 69 54
138 114 90

IIET-CSE-THIRUVALLUR
Result:

IIET-CSE-THIRUVALLUR
EXP No. DATA MANIPULATION FILE HANDLING SEQUENTIAL ACCESS
11(b)

Aim

Algorithm.

Source Code.
Use fprintf() to write a text file:

#include <stdio.h>
#include <stdlib.h>

int main()
{

printf("Welcome “!\n\n");

char character;
FILE *fpointer;
fpointer = fopen("C:\\program.txt","w"); // Here "w" refers to write on file
if(fpointer == NULL)
{
printf("Error! The file does not exist.");
exit(0);
}
printf("Enter a character: ");
scanf("%c",&character);

fprintf(fpointer,"%c",character); // Use of fprintf()


function fclose(fpointer);

return 0;
}
IIET-CSE-THIRUVALLUR
IIET-CSE-THIRUVALLUR
Output.

Use fscanf() to read a text file:

#include <stdio.h>
#include <stdlib.h>
int main()
{

printf("Welcome to DataFlair tutorials!\n\n");

char character;
FILE *fpointer;

if ((fpointer = fopen("C:\\program.txt","r")) == NULL)


{
printf("Error! The file does not exist.");
exit(0);
}
fscanf(fpointer,"%c", &character);
printf("The character is: %c", character);
fclose(fpointer);
return 0;
}

IIET-CSE-THIRUVALLUR
Result.

IIET-CSE-THIRUVALLUR

You might also like