0% found this document useful (0 votes)
4K views

Cs3271 Programming in C

The document describes a lab manual for a programming in C laboratory course. It lists 10 experiments covering topics like I/O statements, decision making constructs, loops, arrays, strings, functions, pointers, structures, and files. Each experiment is allotted a certain number of periods. In total, the course is allotted 60 periods.

Uploaded by

Priya Mohana
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)
4K views

Cs3271 Programming in C

The document describes a lab manual for a programming in C laboratory course. It lists 10 experiments covering topics like I/O statements, decision making constructs, loops, arrays, strings, functions, pointers, structures, and files. Each experiment is allotted a certain number of periods. In total, the course is allotted 60 periods.

Uploaded by

Priya Mohana
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/ 52

CS3271-C Prog Lab Manual

LAB MANUAL

CS3271 Programming in C Laboratory

Year / Semester: I / 02
lOMoARcPSD|356 459 56

CS3271 PROGRAMMING IN C LABORATORY LTPC


0042

LIST OF EXPERIMENTS

1. I/O statements, operators, expressions

2. Decision-making constructs: if-else, goto, switch-case, break-continue

3. Loops: for, while, do-while

4. Arrays: 1D and 2D, Multi-dimensional arrays, traversal

5. Strings: operations

6. Functions: call, return, passing parameters by (value, reference), passing arrays


to function.

7. Recursion

8. Pointers: Pointers to functions, Arrays, Strings, Pointers to Pointers, Array of


Pointers

9. Structures: Nested Structures, Pointers to Structures, Arrays of Structures and


Unions.

10. Files: reading and writing, File pointers, file operations, random access,
processor directives.

TOTAL: 60 PERIODS
lOMoARcPSD|356 459 56

C Programs to demonstrate I/O Statements, Operators & Expressions


Ex.No.1(a) Date:
Aim:
To write a C program to find area and circumference of a circle.

Algorithm:

Step 1: Start the program.


Step 2: Input the radius of the Circle.
Step 3: Find the area and circumference of the circle using the formula
Area =3.14*r*r
Circum=2*3.14*r
Step 4: Print the area and Circumference
Step 5: Stop the Program

PROGRAM: (AREA AND CIRCUMFERENCE OF THE CIRCLE)

#include<stdio.h>
#include<conio.h>
void main()
{
float r,area,circum;
clrscr();
printf("\n Enter the radius of the Circle");
scanf("%f",&r);
area=3.14*r*r;
circum=2*3.14*r;
printf("\n Area=%f",area);
printf("\n Circumference=%f",circum);
getch();
}

Output:
Enter the radius of the Circle 5
Area = 78.500000
Circumference = 31.400000

RESULT:
Thus the C program to find the area and circumference of the circle has been created successfully
and verified.
lOMoARcPSD|356 459 56

Ex.No.1(b) Date:
To swap two numbers without using temporary variable

Aim: To write a C program to swap two numbers using temporary variable.

Algorithm:

Step 1: Start the program.


Step 2: Input the first number and second number.
Step 3: Swap the numbers without using temporary variable
num1=num1+num2
num2=num1-num2
num1=num1-num2
Step 4: Print the first number and second number.
Step 5: Stop the Program

PROGRAM (SWAP TWO NUMBERS WITHOUT USING TEMPORARY VARIABLE)

#include<stdio.h>
#include<conio.h>
void main()\
{
int num1,num2;
clrscr();
printf(“\n Enter the first number:”)
scanf(“%d”,&num1);
printf(“\n Enter the second number:”)
scanf(“%d”,&num2);
num1=num1+num2;
num2=num1-num2;
num1=num1-num2;
printf(“\n The first number is %d”,num1);
printf(“\n The second number is %d”,num2);
}

Output:
Enter the first number: 50
Enter the Second number: 75

The first number is 75


The second number is 50

RESULT:
Thus the C program to swap two numbers without using temporary variable has been created
successfully and verified.
lOMoARcPSD|356 459 56

Ex.No.1(c) Date:
To Convert Fahrenheit into degree Celsius

Aim: To write a C program to convert Fahrenheit into degree Celsius.

Algorithm:

Step 1: Start the program.


Step 2: Input the temperature in fahrenheit.
Step 3: Calculate the temperature in Celsius using below formula
celsius=(0.56)*(Fahrenheit-32);
Step 4: Print the temperature in degree celsius.
Step 5: Stop the Program

PROGRAM (CONVERT FAHRENHEIT INTO DEGREE CELSIUS)

#include<stdio.h>
#include<conio.h>
void main()\
{
float fahrenheit,celsius;
clrscr();
printf(“\n Enter the temperature in Fahrenheit: “)
scanf(“%f”,&fahrenheit);
celsius=(0.56)*(Fahrenheit-32);
printf(“\n Temperature in Degree Celsius is %f “,celsius);
getch();
}

Output:
Enter the temperature in Fahrenheit: 32

Temperature in Degree Celsius is 0

RESULT:
Thus the C program to convert Fahrenheit into Degree Celsius has been created successfully and
verified.
lOMoARcPSD|356 459 56

C Programs to demonstrate Decision Making Constructs Date:

Ex.No.2(a) Demonstration on if.else contruct


Aim:
To write a C program to find largest of three numbers.

Algorithm:
Step 1: Start the program
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b & If a>c
Display a is the largest number.
Else
Display c is the largest number.
Else If b>c
Display b is the largest number.
Else
Display c is the greatest number.
Step 5: Stop the program.

PROGRAM (LARGEST OF THREE NUMBERS)

#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter the values of A,B and C: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c) {
printf("A is Greater than B and C");
}
else if (b > a && b > c) {
printf("B is Greater than A and C");
}
else if (c > a && c > b) {
printf("C is Greater than A and B");
}
else {
printf("all are equal or any two values are equal");
}
return 0;
}

Output:
Enter the values of A,B and C: 3 5 8

C is Greater than A and B

RESULT:

Thus the C program to find largest of three numbers has been created successfully and verified.
lOMoARcPSD|356 459 56

Ex.No.2(b) Demonstration on “goto” statement Date:

AIM:

To write a C Program to check for age eligibility on voting.

ALGORITHM:
Step 1: Start the program
Step 2: Read age.
Step 3: Check the condition, if age>18
Step 4: if true, then go to the label, “yes” and print the statement.
Step 5: Else, go to the label, “no” and print the statement.
Step 6: Stop the program.

PROGRAM: (FINDING THE ELIGIBILITY FOR VOTING)

#include<stdio.h>
void main()
{
int age;
yes: //label name
printf("you are Eligible\n");
no: //label name
printf("you are not Eligible");

printf("Enter your age:");


scanf("%d", &age);
if(age>=18)
goto yes; //goto label g
else
goto no; //goto label s
}

Output:-1

Enter your age: 32

You are Eligible

Output:-1

Enter your age: 22

You are not Eligible

RESULT:
Thus the C program to demonstrate on goto label has been created successfully and verified.
lOMoARcPSD|356 459 56

Ex.No.2(c) Demonstration on “switch” statement Date:

AIM:
To write a C program for demonstrating arithmetic operations using switch case statement.

ALGORITHM:

Step-1 Start the program.


Step-2 Display menu showing addition, subtraction, multiplication and division operation.
Step-3 Get the values for two variables
Step-4 Obtain the choice from the user and accordingly switch over to particular block.
Step-5 Display the result.
Step-6 If the user wishes to continue repeat steps 2 and 3
Step-7 Stop the program.

PROGRAM:

#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, n;
clrscr();
printf(“1. Addition\n”);
printf(“2. Subtraction\n”);
printf(“3. Multiplication\n”);
printf(“4. Division\n”);
printf(“0. Exit\n”);
printf(“Enter your choice : “);
scanf(“%d”,&n);
printf(“Enter the two numbers :”);
scanf(“%d,%d”,&a,&b);
switch(n)
{

case 1:
c = a + b;
printf(“Addition :%d\n”,c);
break;
case 2:
c = a – b;
printf(“Subtraction :%d\n”,c);
break;
case 3:
c = a * b;
printf(“Multiplication :%d\n”,c);
break;
case 4:
c = a / b;
printf(“Division :%d\n”,c);
break;
case 0:
lOMoARcPSD|356 459 56

exit(0);
break;
}

getch();
}

OUTPUT:

1. Addition
2. Subtraction
3. Multiplication
4. Division
0. Exit

Enter Your Choice : 1


Enter the 2 nos a and b: 2 8
Addition : 10.

Enter Your Choice : 2

Enter the 2 nos a and b: 5 2


Subtraction : 3.

Enter Your Choice : 3


Enter the 2 nos a and b: 2 8
Multiplication : 16.

Enter Your Choice : 4.


Enter the 2 nos a and b: 8 4
Division : 2.

Enter Your Choice : 0.


Exit.

Result: Thus to write a C program for demonstrating arithmetic operations using switch case statement was
compiled and executed successfully.
lOMoARcPSD|356 459 56

Ex.No.2(d) Demonstration on “break - continue” statement Date:

AIM:
To write a C Program to take the inputs until entering zero using break statement.

ALGORITHM:
Step 1: Start the program
Step 2: Using while loop till false, Read the value, a
Step 4: Check the condition, if a==0 then
Step 5: if true, exit from the executions
Step 6: Else, ask for reading the value
Step 7: Stop the program.

PROGRAM: (Taking input from the user until entering zero)

#include <stdio.h>
int main ()
{
int a;
while (1)
{
printf("Enter the number:");
scanf("%d", &a);
if ( a == 0 )
printf(“Bye”);
break;
}
return 0;
}

Output:

Enter the number:2


Enter the number:3
Enter the number:4
Enter the number:5
Enter the number:0
Bye

RESULT:
Thus the C program to take the inputs until entering zero using break statement has been created
successfully and verified.
lOMoARcPSD|356 459 56

Ex.No.2(e) Demonstration on “break - continue” statement Date:

AIM:
To write a C Program to print sum of odd numbers upto 10 using continue statement.

ALGORITHM:
Step 1: Start the program
Step 2: Initialize a=0, sum=0
Step 3: Using for loop with condition a<10
Step 4: check the condition a%2==0 then
Step 5: If true, move to the beginning of the for loop
Step 6: Else, calculate sum=sum + a
Step 7: Repeat the steps 4 to 6, until for loop is satisfied
Step 8: Print the sum
Step 9: Stop the program.

PROGRAM: (Sum of odd numbers upto 10)

#include <stdio.h>
int main ()
{
int a,sum = 0;
for (a = 0; a < 10; a++)
{

if ( a % 2 == 0 )
continue;
sum = sum + a;
}
printf("Sum = %d",sum);
return 0;
}

Output:

Sum = 25

RESULT:
Thus the C program to print sum of odd numbers upto 10 using continue statement has been created
successfully and verified.
lOMoARcPSD|356 459 56

Ex.No. 3 Demonstration on Looping Statements

(a) To print the number series up to the given limit (using for loop)
AIM:
To write a C program to print the number series up to the given limit, n.

ALGORITHM:

Step 1: Start the program


Step 2: Read the limit, n
Step 3: Using for loop, initialize i =0 to i<n
Step 4: Print the value n
Step 5: Repeat the steps 3 & 4 till loop is satisfied.
Step 6: Stop the program

PROGRAM (To Print the number series up to the given limit n)

#include <stdio.h>
int main()
{
int i, n;
printf(“Enter the limit:”);
scanf(“%d”,&n);
for (i=1; i<=n; i++)
{
printf("%d\n", i);
}
return 0;
}

Output:

Enter the limit 10

1
2
3
4
5
6
7
8
9
10

Result: Thus a C program to print the number series up to the given limit n was compiled and executed
successfully.
lOMoARcPSD|356 459 56

Ex.No. 3 Demonstration on Looping Statements

(b) To print the sum of series up to given limit (using while loop)
AIM:
To write a C program to find sum of series up to given limit, n.

ALGORITHM:

Step 1: Start the program


Step 2: Read the limit, n
Step 3: Using while loop, initialize i =0 check i<n
Step 4: if true, sum = sum + i, increment i = i + 1
Step 5: Repeat the steps 3 & 4 till loop is satisfied.
Step 6: Print the value of Sum
Step 7: Stop the program

PROGRAM (To Print the sum of number series up to the given limit n)

#include <stdio.h>
int main()
{
int i=0, n, sum=0;
printf(“Enter the limit:”);
scanf(“%d”,&n);
while (i<=n)
{
Sum = sum + i;
i= i + 1;
}
printf(“Sum of series upto %d is %d”,n,sum);
return 0;
}

Output: - 1

Enter the limit 10

Sum of series upto 5 is 15

Output: -

Enter the limit 10

Sum of series upto 5 is 55

Result: Thus a C program to print the sum of number series up to the given limit n was compiled and
executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 3 Demonstration on Looping Statements

(c) To print the even numbers up to given limit (using do..while loop)
AIM:
To write a C program to print the even numbers up to limit, n.

ALGORITHM:

Step 1: Start the program


Step 2: Read the limit, n, sum=0
Step 3: Using do while loop, initialize i =0 to i<n
Step 4: Print the i value and increment i = i + 2
Step 5: Repeat the steps 3 & 4 till loop is satisfied.
Step 6: Stop the program

PROGRAM (To Print the even numbers up to the given limit n)

#include <stdio.h>
int main()
{
int i=0, n, sum=0;
printf(“Enter the limit:”);
scanf(“%d”,&n);
do
{

i= i + 2;
print( i );
}(while i<=n)
printf(“Sum of series upto %d is %d”,n,sum);
return 0;
}

Output: - 1

Enter the limit 15

2
4
6
8
10
12
14

Result: Thus a C program to print the even numbers up to the given limit n was compiled and executed
successfully.
lOMoARcPSD|356 459 56

Ex.No. 4 Demonstration on Arrays

(a) To print sum of array elements using one dimensional array


AIM:
To write a C program to print sum of array elements using one dimensional array

ALGORITHM:

Step 1: Start the program


Step 2: Read the limit, n
Step 3: Using for loop, initialize i =0 to i<n
Step 4: Print the value n
Step 5: Repeat the steps 3 & 4 till loop is satisfied.
Step 6: Stop the program

PROGRAM (To Print sum of given array elements up to the given limit n)

#include <stdio.h>
int main()
{
int a[5]; i=0, n, sum=0;
printf(“Enter the limit:”);
scanf(“%d”,&n);
for (i=0;i<=n;i++)
{
printf(“Enter the number: ”);
scanf(“%d”,&a[i]);
sum = sum + a[i];
i = i + 1;
}
printf(“Sum of the given array numbers is %d”,sum);
return 0;
}

Output: - 1

Enter the limit 5

Enter the number: 10


Enter the number: 20
Enter the number: 30
Enter the number: 40
Enter the number: 50

Sum of the given array numbers is 150

Result: Thus a C program to print sum of given array elements up to the given limit n was compiled and
executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 4 Demonstration on Arrays

(b) To demonstrate on Matrix Addittion using two dimensional array

Aim:
To write a C program for matrix Addition.
Algorithm:

Step 1: Start the program


Step 2: Enter the row and column of the matrix
Step 3: Enter the element of A matrix
Step 4: Enter the element of B matrix
Step 5: Read matrix values in a[i][j] & b[i]
Step 6: Print the A matrix in matrix form
Step 7: Print the B matrix in matrix form
Step 8: Set a loop up to the row and inner loop upto the column
Step 9: Add the elements of the matrix c[i][j]=a[i][j]+b[i][j]
Step 10: Set a loop to print matrix values in c[i][j]
Step 11: Stop the program

PROGRAM (FOR MATRIX ADDITION)

#include<stdio.h>
void main()
{
int a[25][25],b[25][25],c[25][25],i,j,m,n;
clrscr();
printf("Enter the rows and column of two matrix:\n");
scanf("%d%d",&m,&n);
printf("\n Enter the elements of A matrix:”);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\nEnter the elements of B matrix:");
for(i=0;i<m;i++)
{
for( j=0;j<n;j++)
scanf("%d",&b[i][j]);
}
printf("\nThe elements of A matrix");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",a[i][j]);
}
printf("\nThe elements of B matrix");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
printf("\t%d",b[i][j]);
lOMoARcPSD|356 459 56

}
printf("\nThe addition of two matrices");
for(i=0;i<m;i++)
{
printf("\n");
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("\t%d",c[i][j]);
}
}
getch();
}

OUTPUT:

Enter the rows and column of two matrix: 3 3

Enter the elements of A matrix: 1 2 3 4 5 6 7 8 9

Enter the elements of B matrix: 1 2 3 4 5 6 7 8 9

The elements of A matrix


1 2 3
4 5 6
7 8 9

The elements of B matrix


1 2 3
4 5 6
7 8 9

The addition of two matrices


2 4 6
8 10 12
14 16 18

Result: Thus a C program to implement matrix addition was compiled and executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 4 Demonstration on Arrays

(c) To print the position of elements stored in Multi dimensional array

Aim:
To write a C program to print the position of elements stored in Multi dimensional array.

Algorithm:

Step 1: Start the program


Step 2: Read the table, rows and columns
Step 3: Assign the values for the Employees [2][2][3]
Step 4: Using for loop, with tables, rows and columns
Step 5: print the each element with the position.
Step 6: Stop the program

PROGRAM ( FOR MULTI DIMENSIONAL ARRAY)

#include<stdio.h>
int main()
{
int tables, rows, columns;
int Employees[2][2][3] = { { {9, 99, 999}, {8, 88, 888} },
{ {225, 445, 665}, {333, 555, 777} }
};

for (tables = 0; tables < 2; tables++)


{

for (rows = 0; rows < 2; rows++)


{

for (columns =0; columns < 3; columns++)


{

printf("Employees[%d][%d][%d] = %d\n", tables, rows, columns,


Employees[tables][rows][columns]);
}
}
}
return 0;
}
lOMoARcPSD|356 459 56

OUTPUT:

Employees [0][0][0] = 9
Employees [0][0][1] = 99
Employees [0][0][2] = 999
Employees [0][1][0] = 8
Employees [0][1][1] = 88
Employees [0][1][2] = 888
Employees [0][2][0] = 225
Employees [0][2][1] = 445
Employees [0][2][2] = 665
Employees [0][0][0] = 333
Employees [0][0][0] = 555
Employees [0][0][0] = 777

Result: Thus a C program to print the position of elements stored in Multi dimensional array was compiled
and executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 4 Demonstration on Arrays

(d) To traverse the one dimensional array (Reaching all the elements)

Aim:
To write a C program to demonstrate on the traversal of one dimensional array.

Algorithm:

Step 1: Start the program


Step 2: Read the number of elements
Step 3: Read one dimensional arrays with values
Step 4: Using for loop with variable i, check the condition i<n
Step 5: Print the element
Step 6: Repeat steps 4 & 5 till the loop ends
Step 7: Stop the program

PROGRAM ( TRAVERSING ONE DIMENSIONAL ARRAY)

#include <stdio.h>
int main()
{
int a[6]={10,20,30,40,50,60};
int i;
for (i=0; i<6; i++)
{
printf(“Element in position[%d] is %d”,i+1,a[i]);
}
}

OUTPUT:

Element in position[1] is 10
Element in position[2] is 20
Element in position[3] is 30
Element in position[4] is 40
Element in position[5] is 50
Element in position[6] is 60

Result:
Thus a C program to traverse the one dimensional array was compiled and executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 5(a) Demonstration on Operation of Strings using Built in Functions

To find the length of the given string

Aim:
To write a C program to find the length of the given string.
Algorithm:

Step 1: Start the program


Step 2: Read the string
Step 3: Using the builtin function strlen(), find the length.
Step 4: Print the length of the given string
Step 5: Stop the program

PROGRAM

#include<stdio.h>
void main()
{
char str1[20];
int len;
printf("Enter the string: ");
scanf("%s",&str1);
len=strlen(str1);
printf("Length of the given string %s is %d",str1,len);
}

OUTPUT:

Enter the string: vimalraj

Length of the given string vimalraj is 8

Result:
Thus a C program to find the length of the given string was compiled and executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 5(b) Demonstration on Operation of Strings using Built in Functions

To copy from one string to another string

Aim:
To write a C program to copy the one string to another string.

Algorithm:

Step 1: Start the program


Step 2: Read the string
Step 3: Using the builtin function, strcpy(), copy to the new string
Step 4: print the new string
Step 5: Stop the program

PROGRAM

#include<stdio.h>
void main()
{
char str1[20],str2[20];
int len;
printf("Enter the string");
scanf("%s",&str1);
strcpy(str2,str1);
printf("Copied New String is %s",str2);
}

OUTPUT:

Enter the string: vimal

Copied New String is vimal

Result:

Thus a C program to copy from one string to another string was compiled and executed successfully.
lOMoARcPSD|356 459 56

C.concatenate two string (join)

Aim:
To write a C program to concatenate two strings.

Algorithm:

Step 1: Start the program


Step 2: Read two strings separately.
Step 3: Using the builtin function, strcat(), join the strings
Step 4: print the concatenated string
Step 5: Stop the program

PROGRAM

#include<stdio.h>
void main()
{
char str1[20],str2[20];
int len;
printf("Enter the string1: ");
scanf("%s",&str1);
printf("Enter the string2: ");
scanf("%s",&str2);
strcat(str1,str2);
printf("Copied New String is %s",str1);
}

OUTPUT:

Enter the string1: Vimalraj

Enter the string2: Raja

Copied New String is VimalrajRaja

Result:

Thus a C program to concatenate two strings was compiled and executed successfully.
lOMoARcPSD|356 459 56

(a) To compare two strings are same

Aim:
To write a C program to compare two strings are same or not.

Algorithm:

Step 1: Start the program


Step 2: Read two strings separately.
Step 3: Using the builtin function, strcmp(), compare the strings
Step 4: if ascii value is 0, print it is same
Step 5: Else, print not same
Step 5: Stop the program

PROGRAM

#include<stdio.h>
void main()
{
char str1[20],str2[20];
int comp;
printf("Enter the string1: ");
scanf("%s",&str1);
printf("Enter the string2: ");
scanf("%s",&str2);
comp=strcmp(str1,str2);
if (comp==0)
{
printf("Two strings are same");
}
else
{
printf("Two strings are not same");
}
}
OUTPUT:-1
Enter the string1: Vimal
Enter the string2: Vimal
Two strings are same
OUTPUT:-2
Enter the string1: Vimal
Enter the string2: Kamal
Two strings are not same

Result:

Thus a C program to compare two strings to check same or not was compiled and executed successfully.
lOMoARcPSD|356 459 56

(b) To reverse the given string

Aim:
To write a C program to reverse the given string.

Algorithm:

Step 1: Start the program


Step 2: Read the string
Step 3: Using the builtin function strrev()
Step 4: Print the reverse of the given string
Step 5: Stop the program

PROGRAM

#include<stdio.h>
void main()
{
char str1[20],rec[20];
printf("Enter the string: ");
scanf("%s",&str1);
printf("Reverse of the given string is %s”,strrev(str));
}

OUTPUT:

Enter the string: vimalraj

Reverse of the given string is jarlamiv

Result:
Thus a C program to find the reverse of the given string was compiled and executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 6 FUNCTIONS – Function call

(a) To search an element using linear search

Aim:
To write a C program to search an element in a given array using linear search.

Algorithm:

Step 1: Start the program


Step 2: Read no. of elements and enter the values.
Step 3: Read the searching element.
Step 4: Call the function, linear()
Step 5: Stop the program
Function:
Step 4.1: Read i, and flag=0
Step 4.2: Using for loop, check the condition if x==a[i]
Step 4.3: if true, flag=1, break and print element is found
Step 4.4: Else, print element is not found.

PROGRAM

#include<stdio.h>
void linear(int [ ], int, int);
main()
{
int i, n, x, a[20];
printf(“Enter how many elements:”);
scanf(“%d”,&n);
printf(“\n Enter the elements”);
for ( i=0; i<n; i++)
{
scanf(“%d\n”,&a[i]);
}
printf(“\n Enter the element to search:”);
scanf(“%d”,&x);
linear(a.n,x); // Function call
}
void linear(int a[ ], int n, int x)
{
int i, flag=0;
for(i=0; i<n; i++)
{
if ( x == a[i])
{
flag = 1;
break;
}
}

if (flag = = 1)
{
lOMoARcPSD|356 459 56

printf(“\n Element %d is found in the position %d”,a[i], i+1);


else
printf(“\n The element is not found in the list”);
}

OUTPUT-1:

Enter how many elements: 8


Enter the elements: 50
20
60
30
10
80
90
70

Ent er the element to search: 30

Element 30 is found in the position 4

OUTPUT-2:

Enter how many elements: 8


Enter the elements: 50
20
60
30
10
80
90
70

Ent er the element to search: 100

The element is not found

Result:
Thus a C program to search an element using linear search was compiled and executed
successfully.
lOMoARcPSD|356 459 56

Ex.No. 6 FUNCTIONS – Return Value

(b) To find square of a number

Aim:
To write a C program to find square of a given number.

Algorithm:

Step 1: Start the program


Step 2: Read the number.
Step 3: Pass the value to the function
Step 4: Return the value to the main program
Step 5: Print the value
Step 6: Stop the program
Function:
Step 3.1: Read the value
Step 3.2: Return the value

PROGRAM

#include<stdio.h>
int square(int);
void main()
{
int sq, n;
printf(“ Enter the number””);
scanf(“%d”,&n);
sq = square( n ); // Function Call
printf(“Square of %d is %d”,n,sq);
}
int square(int n)
{
return n * n;
}

OUTPUT:
Enter the number: 9

Square of 9 is 81

Result:
Thus a C program to find square of a number was compiled and executed successfully.

Downloaded by Mohana priya KR ([email protected])


lOMoARcPSD|356 459 56

Ex.No. 6 FUNCTIONS – Pass by Value

(c) To find cube of a given number

Aim:
To write a C program to find cube for a given number using pass by value.

Algorithm:

Step 1: Start the program


Step 2: Read the number.
Step 3: Pass the value to the function
Step 4: Return the value to the main program
Step 5: Print the value
Step 6: Stop the program
Function:
Step 3.1: Read the value
Step 3.2: Return the value

PROGRAM

#include<stdio.h>
int cube(int);
void main()
{
int c, n;
printf(“ Enter the number””);
scanf(“%d”,&n);
c = cube( n ); // Function Call
printf(“Cube of %d is %d”,n,c);
}
int cube(int n)
{
return n * n * n;
}

OUTPUT:
Enter the number: 5

Cube of 5 is 125

Result:
Thus a C program to find cube of a number using pass by value was compiled and executed
successfully.

Downloaded by Mohana priya KR ([email protected])


lOMoARcPSD|356 459 56

Ex.No. 6 FUNCTIONS – Pass by reference

(d) To add two numbers

Aim:
To write a C program to add two numbers using pass by reference

Step 1: Start the program


Step 2: Read the two values.
Step 3: Call and Pass the values to the function
Step 4: Return the value to the main program
Step 5: Print the value
Step 6: Stop the program
Function:
Step 3.1: Read the value
Step 3.2: Add the two values
Step 3.3: Return the values

PROGRAM

#include<stdio.h>
int add(int *, int *);
void main()
{
int a,b,c;
printf(“ Enter the two numbers””);
scanf(“%d%d”,&a,&b);
c = add(&a, &b); // Function Call
printf(“Addition is %d”,c);
}
int add(int *x, int *y)
{
int z;
z = *x + *y;
return z;
}

OUTPUT:
Enter the two numbers: 5 10

Addition is 15

Result:
Thus a C program to add two numbers using pass by reference was compiled and executed
successfully.

Downloaded by Mohana priya KR ([email protected])


lOMoARcPSD|356 459 56

Ex.No. 6 FUNCTIONS – Passing Arrays to function

(e) To sort the numbers using bubble sort

Aim:
To write a C program to sort the numbers in ascending order using bubble sort.

Step 1: Start the program


Step 2: Read no. of elements and enter the values.
Step 3: Call the function, bubble() and pass the values
Step 4: Print the elements
Step 5: Stop the program
Function:
Step 4.1: Read the array elements
Step 4.2: Using for loop in i, j, check the condition if a[i] > a[j]
Step 4.3: if true, swap the elements
Step 4.4: return the elements after the loop.

PROGRAM

#include<stdio.h>
void bubble(int a[],int n);
int a[25], i, j, n, temp;
void main()
{
printf("Enter no.of elements: ");
scanf("%d",&n);
printf("Enter the elements: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble(a, n); // function call
printf("The sorted elements are: ");
for(i=0;i<n;i++)
printf("\n%d",a[i]);
}
void bubble(int a[], int n)
{
for(i=0;i<=n-1;i++)
for(j=i+1;j<=n-1;j++)
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
lOMoARcPSD|356 459 56

OUTPUT:

Enter no. of elements: 10

Enter the elements:


50
20
30
10
100
80
90
70
40
60

The sorted elements are:


10
20
30
40
50
60
70
80
90
100

Result:
Thus a C program to sort the numbers in ascending order using bubble sort was compiled and
executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 7 FUNCTIONS – Recursion

To find factorial of a given number using recursion

Aim:
To write a C program to find factorial of a given number using recursion.

Algorithm:

Step 1: Start the program


Step 2: Read the number
Step 3: Call the function, fact() and pass the values
Step 4: Print the elements
Step 5: Stop the program
Function:
Step 4.1: Read the value
Step 4.2: Using recursive function, calculate the value
Step 4.3: return the value after the recursive condition.

PROGRAM

#include<stdio.h>
int fact(int );
main()
{
int n;
int f;
printf("\nEnter a number:");
scanf("%d",&num);
f=fact(num);
printf("\nThe factorial value of %d is %d\n",num,fact1);
}
int fact(int n)
{
if(n==1||n==0)
return(1);
else
return(n*fact(n-1));
}

OUTPUT

Enter a number:6

The factorial value of 6 is 720

Result:
Thus a C program to find factorial of a given number using recursion was compiled and executed
successfully.
lOMoARcPSD|356 459 56

Ex.No. 8 POINTERS – Passing pointers to functions

(a) To swap two numbers with passing pointers to functions

Aim:

To write a C program to swap two numbers with passing pointers to functions

Algorithm:

Step 1: Start the program


Step 2: Read the two values.
Step 3: Call and Pass the values to the function using address
Step 4: Return the value to the main program
Step 5: Print the value
Step 6: Stop the program
Function:
Step 3.1: Read the value
Step 3.2: Swap the values
Step 3.3: Return the values

PROGRAM
#include<stdio.h>
int swap(int *, int *);
void main()
{
int a,b,c;
printf(“ Enter the two numbers”);
scanf(“%d%d”,&a,&b);
swap(&a, &b); // Function Call
printf(“Now A value is %d”,a);
printf(“Now B value is %d”,b);
}
int swap(int *x, int *y)
{
int z;
z = *x;
*x = *y;
*y = z;
}

OUTPUT:
Enter the two numbers: 5 10
Now A value is 10
Now B value is 20

Result:
Thus a C program to swap two numbers with passing pointers to functions was compiled and
executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 8 POINTERS – Passing pointers to arrays

(b) Printing the array values using pointers

Aim:

To write a C program to print the array values using pointers (Address)

Algorithm:

Step 1: Start the program


Step 2: Initialize array values and declare a pointer variable.
Step 3: Assign array variable to the pointer variable.
Step 4: Using for loop, access the elements with help of pointers
Step 5: Print the value
Step 6: Stop the program

PROGRAM
#include <stdio.h>
int main ()

{
/* an array with 5 elements */
double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
double *p;
int i;
p = &balance;
/* output each array element's value */
printf( "Array values using pointer\n");
for ( i = 0; i < 5; i++ ) {
printf("*(p + %d) : %f\n", i, *(p + i) );
}
printf( "Array values using balance as address\n");
for ( i = 0; i < 5; i++ ) {
printf("*(balance + %d) : %f\n", i, *(balance + i) );
}
return 0;
}
lOMoARcPSD|356 459 56

OUTPUT:

Array values using pointer


*(p + 0) : 1000.000000
*(p + 1) : 2.000000
*(p + 2) : 3.400000
*(p + 3) : 17.000000
*(p + 4) : 50.000000
Array values using balance as address
*(balance + 0) : 1000.000000
*(balance + 1) : 2.000000
*(balance + 2) : 3.400000
*(balance + 3) : 17.000000
*(balance + 4) : 50.000000

Result:
Thus a C program to print the array values using pointers was compiled and executed
successfully.
lOMoARcPSD|356 459 56

Ex.No. 8 POINTERS – Passing pointers to strings

(c) To print the string using pointers

Aim:

To write a C program to print the string using pointers.

Algorithm:

Step 1: Start the program


Step 2: Declare and initialize the string.
Step 3: Assign string variable to the pointer variable.
Step 4: Print the normal string variable
Step 5: Print the string with string pointer
Step 6: Stop the program

PROGRAM
#include<stdio.h>
int main()
{
char Name[] = "Dinesh";
char* ptrname = Name;
clrscr();
printf("Name = %s\n",Name);
printf("Name via ptrname = %s\n", ptrname);
return 0;
}

OUTPUT:

Name = Dinesh
Name via ptrname = Dinesh

Result:
Thus a C program to print the string using pointers was compiled and executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 8 POINTERS – Passing pointers to pointers

(d) To print the values using pointers to pointers

Aim:

To write a C program to print the values using pointers to pointers

Algorithm:

Step 1: Start the program


Step 2: Declare pointers with * and **
Step 3: Intialize a value with a variable
Step 4: Assign pointer address for the variable.
Step 5: Print the values with pointer with pointers
Step 6: Stop the program

PROGRAM
#include <stdio.h>
int main () {
int var;
int *ptr;
int **pptr;
var = 3000;
/* take the address of var */
ptr = &var;
/* take the address of ptr using address of operator & */
pptr = &ptr;
/* take the value using pptr */
printf("Value of var = %d\n", var );
printf("Value available at *ptr = %d\n", *ptr );
printf("Value available at **pptr = %d\n", **pptr);
return 0;
}

OUTPUT:
Value of var = 3000
Value available at *ptr = 3000
Value available at **pptr = 3000

Result:
Thus a C program to print the values using pointers to pointers was compiled and executed
successfully.
lOMoARcPSD|356 459 56

Ex.No. 8 POINTERS – Array of Pointers

(e) To print the values using array of pointers

Aim:

To write a C program to print the values using array of pointers

Algorithm:

Step 1: Start the program


Step 2: Declare pointer variable and variable with one dimensional values
Step 3: Initialize a value for one dimensional array
Step 4: Using loop assign the address for the array values
Step 5: Using loop print the values of the array.
Step 6: Stop the program

PROGRAM
#include <stdio.h>
const int MAX = 3;
int main () {
int var[] = {10, 100, 200};
int i, *ptr[MAX];
for ( i = 0; i < MAX; i++) {
ptr[i] = &var[i]; /* assign the address of integer. */
}
for ( i = 0; i < MAX; i++) {
printf("Value of var[%d] = %d\n", i, *ptr[i] );
}
return 0;
}

OUTPUT:

Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200

Result:
Thus a C program to print the values using array of pointers was compiled and executed
successfully.
lOMoARcPSD|356 459 56

Ex.No. 9 STRUCTURE – Nested Structure

(a) To print student details using nested structure

Aim:
To write a C program to print the student details with help of nested structure.

Algorithm:

Step 1: Start the program


Step 2: Declare a structure with members
Step 3: Read the values for the members using structure variable.
Step 4: Declare another structure with members joined with old structure
Step 5. Read the values for new structure
Step 6: Print the values using respective structure variables.
Step 5: Stop the program

PROGRAM
#include <stdio.h>
struct student
{
int rno;
char name[25];
char dept[10];
};
struct address
{
int door;
char area[25];
double pincode;
struct student s; //Nested Structure
}a={10,"Tiruttani",631209,1001,"Vimal","CSE"};
int main()
{
printf("Name=%s",a.s.name);
printf("\nRoll number=%d",a.s.rno);
printf("\nDepartment=%s",a.s.dept);
printf("\nDoor No=%d",a.door);
printf("\nArea=%s",a.area);
printf("\nPincode=%2.lf",a.pincode);
return 0;
}

OUTPUT-1:
Name=Vimal
Roll number=1001
Department=CSE
Door No=10
Area=Tiruttani
Pincode=631209

Result:
Thus a C program to print student details using nested structure was compiled and executed
successfully.
lOMoARcPSD|356 459 56

Ex.No. 9 STRUCTURE – Pointer to Structure

(b) To print book details using nested structure

Aim:
To write a C program to print the book details with help of pointers to structure.

Algorithm:

Step 1: Start the program


Step 2: Declare a structure with members
Step 3: Read the values for the members using structure variable.
Step 4: Declare structure pointer and assign the address of structure variable.
Step 5. Print the values using member selection operator 
Step 6: Stop the program

PROGRAM
#include <stdio.h>
struct book
{
char bname[20];
char auname[20];
float price;
int pages;
char publisher[20];
int pubyear;

}b={"Pro C","Kanetkar",590.50,696,"Mcgraw Hill",2008};


int main()
{
struct book *p;
p=&b;
printf("Book Name=%s",p->bname);
printf("\nAuthor Nanme=%s",p->auname);
printf("\nBook Price=%f",p->price);
printf("\nBook Pages=%d",p->pages);
printf("\nBook Publisher=%s",p->publisher);
printf("\nPublished Year=%d",p->pubyear);
return 0;

}
OUTPUT :
Book Name=Pro C
Author Nanme=Kanetkar
Book Price=590.500000
Book Pages=696
Book Publisher=Mcgraw Hill
Published Year=2008

Result:
Thus a C program to print book details using pointer to structure was compiled and executed
successfully.
lOMoARcPSD|356 459 56

Ex.No. 9 STRUCTURE – Arrays with Structure

(c) To generate EB bill using Arrays with structures

Aim:
To write a C program to generate EB bill using arrays with structure.
Algorithm:

Step 1: Start the program


Step 2: Declare a structure with members
Step 3: Declare structure variable with array with size.
Step 4: Read for no. of consumers, n
Step 5: Read the values for the members using structure variable using loop.
Step 6: Print the values using member selection operator 
Step 7: Stop the program

PROGRAM

#include <stdio.h>
#include<stdlib.h>
struct eb
{
char consumer[20];
int consumerid;
int curread;
int prevread;
int totread;
float price;
}b[10];

int main()
{
int i,n;
printf("Enter how many consumers:");
scanf("%d",&n);
printf("\nEnter the details of the consumers");
for(i=0;i<n;i++)
{
printf("\nEnter the consumer name:");
scanf("%s",b[i].consumer);
printf("\nEnter the consumer id:");
scanf("%d",&b[i].consumerid);
printf("\nEnter the current reading:");
scanf("%d",&b[i].curread);
printf("\nEnter the previous reading:");
scanf("%d",&b[i].prevread);
b[i].totread = b[i].curread - b[i].prevread;
b[i].price = b[i].totread * 6 + 50;
}
printf("\nElectricity Bill Details are:");
for(i=0;i<n;i++)
{
printf("\nCounumer name=%s",b[i].consumer);
lOMoARcPSD|356 459 56

printf("\nCosumer ID = %d",b[i].consumerid);
printf("\nCurrent Reading=%d",b[i].curread);
printf("\nPrevious Reading=%d",b[i].prevread);
printf("\n Consumed Units = %d",b[i].totread);
printf("\n Total Bill = %f",b[i].price);
printf("\n");
}

OUTPUT :
Enter how many consumers:2

Enter the details of the consumers

Enter the consumer name:vimal


Enter the consumer id:1001
Enter the current reading:5234
Enter the previous reading:3254

Enter the consumer name:Kamal


Enter the consumer id:1002
Enter the current reading:6541
Enter the previous reading:4521

Electricity Bill Details are:

Consumer name=vimal
Consumer ID = 1001
Current Reading=5234
Previous Reading=3254
Consumed Units = 1980
Total Bill = 11930.000000

Consumer name=Kamal
Consumer ID = 1002
Current Reading=6541
Previous Reading=4521
Consumed Units = 2020
Total Bill = 12170.000000

Result:
Thus a C program to generate eb bill using arrays of structure was compiled and executed
successfully.
lOMoARcPSD|356 459 56

Ex.No. 9 UNIONS

(d) Demonstration on Union

Aim:
To write a C program to print the mark sheet of a student using union.

Algorithm:

Step 1: Start the program


Step 2: Declare a structure with members
Step 3: Read the values for the members using structure variable.
Step 4: Declare structure pointer and assign the address of structure variable.
Step 5. Print the values using member selection operator 
Step 6: Stop the program

PROGRAM
#include <stdio.h>
union book
{
char bname[20];
char auname[20];
float price;
}b;
int main()
{
printf(“\nEnter the Book Name:”);
scanf(“%s”,b.bname);
printf("Book Name=%s",b.bname);
printf(“\nEnter the Author Name:”);
scanf(“%s”,&b.auname);
printf("\nAuthor Nanme=%s",b.auname);
printf(“\nEnter the Book Price:”);
scanf(“%f”,b.price);
printf("\nBook Price=%f",b.price);
return 0;

}
OUTPUT :

Enter the Book Name: Pro C


Book Name=Pro C
Enter the Author Name: Kanetkar
Author Nanme=Kanetkar
Enter the Book Price:590.50
Book Price=590.500000

Result:
Thus a C program to print book details using union was compiled and executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 10 FILES

(a) Reading from a file

Aim:
To write a C program to read the content from the existing file.

Algorithm:
Step 1: Start the program
Step 2: Declare a file pointer
Step 3: Assign the file pointer with file name and mode with “r”.
Step 4: Using fgetc(), read the each character from a file upto EOF.
Step 5: Print the content of the file
Step 6: Stop the program

PROGRAM
#include <stdio.h>
int main()
{ sample.txt
char ch;
FILE *fp; CS3271 PROGRAMMING IN C LABORATORY
fp=fopen(“sample.txt”. “r”);
if (fp = = NULL)
{
printf(“File does not exist”);
}
while (fp !=EOF)
{
ch= getc(fp);
printf(“%c”,ch);
}
fclose(fp);
}

OUTPUT :

CS3271 PROGRAMMING IN C LABORATORY

Result:
Thus a C program to read the content of the existing file was compiled and executed
Successfully.
lOMoARcPSD|356 459 56

Ex.No. 10 FILES

(b) Writing into a file

Aim:
To write a C program to write the content into a file.
Algorithm:
Step 1: Start the program
Step 2: Declare a file pointer
Step 3: Assign the file pointer with file name and mode with “w”.
Step 4: Using fputc(), write the each character and press ctrl z to stop.
Step 5: Print the content of the text into the file.
Step 6: Stop the program

PROGRAM
#include <stdio.h>
int main()
{
char ch;
FILE *fp;
fp=fopen(“sample.txt”. “w”);
printf(“Enter the text & press ctrl Z to stop”);
while ((ch==getchar())!=EOF)
{
fputc(ch,fp);
}
fclose(fp);
}

OUTPUT :
Enter the text & press ctrl Z to stop
CS3271 PROGRAMMING IN C LABORATORY

sample.txt

CS3271 PROGRAMMING IN C LABORATORY

Result:
Thus a C program to write the content in to the file was compiled and executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 10 FILES

(c) Demonstration on File Pointer

Aim:
To write a C program to demonstrate on usage of file pointers.

Algorithm:

Step 1: Start the program


Step 2: Declare a file pointer
Step 3: Assign the file pointer with file name and mode with “w”.
Step 4: Using fprintf(), write the data in to the file.
Step 5: Close the file pointer
Step 6: Stop the program

PROGRAM
#include <stdio.h>
int main()
{
FILE *fp;
char name[20];
int regno;
char place[20];
fp = fopen("example.txt","w");
printf("Enter your Name, Reg No & Place:");
scanf("%s%d%s",&name,&regno,&place);
fprintf(fp,"%s %d %s",name,regno,place);
printf("\nData written successfully");
fclose(fp);
}

OUTPUT :
Enter your Name, Reg No & Place:
Vimal 1001 Tiruttani

Data written successfully

example.txt

Vimal 1001 Tiruttani

Result:
Thus a C program to demonstration on file pointer was compiled and executed successfully.
lOMoARcPSD|356 459 56

Ex.No. 10 FILES

(d) Demonstration on File Operations

Aim:
To write a C program to demonstrate on usage of file operations (read, write & append)

Algorithm:
Step 1: Start the program
Step 2: Declare a file pointer
Step 3: Display menu for different
file operations.Step 4: Based on
the option, open the file in
specified mode using file pointer.
Step 5: Perform the operation on
file pointer.Step 6: Close the
file pointer Step 7: Stop the
program.

PROGRAM

#include <stdio.h>
int main()
{
FILE *fp; int
regno,n;
char fname[20],name[40];
printf("FILE OPERATIONS");
printf("\nRead Opertion"); printf("\nWrite
Operation"); printf("\nAppend operation");
printf("\nChoose any one of the option:");
scanf("%d",&n);

switch(n)
{
case 1:
printf("\nEnter the file name to read:");
scanf("%s",fname); fp=fopen(fname,"r");
fscanf(fp,"%s%d",&name,&regno);
printf("%s %d",name,regno); fclose(fp);
break;

case 2:
printf("\nEnter the file name to write:");
scanf("%s",fname); fp=fopen(fname,"w");
fprintf(fp,"Programming in C");
printf("\nContent written successfully");
fclose(fp);
break;
lOMoARcPSD|356 459 56

case 3:
printf("\nEnter the file name to write:");
scanf("%s",fname); fp=fopen(fname,"a");
fprintf(fp,"\nProblem solving and Python programming");
printf("\nContent written successfully");
fclose(fp);
break;
default:
printf("Enter correct choice");
break;
}
}
OUTPUT :-1
FILE OPERATIONS
Read Opertion
Write Operation
Append operation
Choose any one of the option:1

Enter the file name to read:example.txt


Vimalraj 1001

OUTPUT :-2
FILE OPERATIONS kamal.txt (New file)
Read Opertion Programming in C
Write Operation
Append operation
Choose any one of the option:2

Enter the file name to write:kamal.txt

Content written successfully

OUTPUT :-3
FILE OPERATIONS kamal.txt (Existing file)
Read Opertion Programming in C
Write Operation Problem Solving and Python Programming
Append operation
Choose any one of the option:3

Enter the file name to write:kamal.txt

Content written successfully

Result:
Thus a C program to demonstration on file operations was compiled and executed
successfully.
lOMoARcPSD|356 459 56

Ex.No. 9 FILES

(e) Demonstration on Random Access

Aim:
To write a C program to demonstrate on random access in a file.

Algorithm:

Step 1: Start the program


Step 2: Declare a file pointer
Step 3: Assign the file pointer with file name and mode with “w+”.
Step 4: Using random access functions, fseek(), ftell(), rewind(), perform the operations
Step 5: Move the file pointer using Postion
Step 6: Print the position of the pointer.
Step 7: Close the file pointer.
Step 8: Stop the program.

PROGRAM
#include <stdio.h>
int main () {
FILE *fp;
int c;
fp = fopen("file.txt","w+");

fputs("This is study.com", fp);

// We are using fseek() to shift the file pointer to the 7th position.
fseek( fp, 7, SEEK_SET );

//Now we overwrite C programming in the 7th position


fputs(" C Programming", fp);

//Now we print the current position of the file pointer using ftell()
printf("The current position of the file pointer is: %ld\n", ftell(fp));

//We take the file pointer to the beginning of the file.


rewind(fp);

//Now we verify if rewind() worked using ftell().


printf("The current position of the file pointer is: %ld\n", ftell(fp));

while(1) {
c = fgetc(fp);
if( feof(fp) ) {
break;
}
printf("%c", c);
}
fclose(fp);
return(0);
}
lOMoARcPSD|356 459 56

OUTPUT

The current position of the file pointer is: 21


The current position of the file pointer is: 0
This is C Programming

Explanation:

Functions can be used to handle file operations only when you send the file pointer as a parameter to the
function. You can also send the file name as a parameter and handle the operations inside the function. The
common practice is to send the file pointer to a function for a specific purpose. This example has been
modified to use a function for displaying the contents of the file by passing the file pointer.

Result:

Thus to write a C program to perform the demonstration on Random file access was compiled and
executed successfully.
lOMoARcPSD|356 459 56

Ex.No.10 FILES

(f) Preprocessor Directives

Aim:
To write a C program to demonstrate on preprocessor directives

Algorithm:

Step 1: Start the program


Step 2: Define a macro
Step 3: Define a macro in another macro
Step 4: Use the macros inside the program
Step 5: Perform the manipulations
Step 6: Print the result
Step 7: Stop the program

PROGRAM
#include <stdio.h>
#define A 10
#define B A+30
#define C A+B
void main()
{
int result;
result = A + B + C;
printf(“Result = %d”,result);

OUTPUT :

Result = 90

Result:
Thus a C program to demonstration on preprocessor directives was compiled and executed
successfully.

You might also like