Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
45 views36 pages

C Lab Manual

The document provides details about a C programming laboratory course at Gnanamani College of Technology, including faculty information, course objectives, and a list of experiments. It outlines various programming concepts such as I/O statements, decision-making constructs, loops, and arrays, along with sample programs and their outputs. The course aims to equip students with practical skills in C programming through hands-on experiments.

Uploaded by

mgkavin40
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)
45 views36 pages

C Lab Manual

The document provides details about a C programming laboratory course at Gnanamani College of Technology, including faculty information, course objectives, and a list of experiments. It outlines various programming concepts such as I/O statements, decision-making constructs, loops, and arrays, along with sample programs and their outputs. The course aims to equip students with practical skills in C programming through hands-on experiments.

Uploaded by

mgkavin40
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/ 36

GNANAMANI COLLEGE OF TECHNOLOGY

(An Autonomous Institution)


Affiliated to Anna University - Chennai, Approved by AICTE - New Delhi.
(Accredited by NBA & NAAC with "A" Grade)
NH-7, A.K.SAMUTHIRAM, PACHAL (PO), NAMAKKAL - 637 018.

FACULTY DETAILS
Name of the Faculty : Mrs.P.Saranya., MCA.,
Designation : Assistant Professor
Department : Artificial Intelligence and Data Science

SUBJECT DETAILS
Subject Code & Name : 23GE202 & Programming in C
Course Code : 116
Course Category : General Engineering
Year / Semester : I / II
Program & Department : B.E – Agricultural Engineering
: B.E – Mechanical Engineering
Regulation : 2023
Academic Year : 2024 – 2025 (Even Sem)

23GE221- PROGRAMMING IN C | LABORATORY


L T P C
23GE221 PROGRAMMING IN C LABORATORY
0 0 3 1.5

COURSE OBJECTIVES
• To familiarize with C programming constructs.
• To develop programs in C using basic constructs.
• To develop programs in C using arrays.
• To develop applications in C using strings, pointers, functions.
• To develop applications in C using structures.
• To develop applications in C using file processing.

LIST OF EXPERIMENTS
1. I/O statements, operators, expression

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 - length, compare, concatenate, copy

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

7. Recursion - Factorial and Fibonacci of the number

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: 45 PERIODS
COURSE OUTCOMES
At the end of this course, the students will be able to:

CO1: To demonstrate knowledge on C programming constructs and develop programs in C using basic
constructs.

CO2: To develop programs in C using arrays.

CO3: To develop applications in C using strings, pointers, functions.

CO4: To develop applications in C using structures.

CO5: To develop applications in C using file processing.

23GE221- PROGRAMMING IN C | LABORATORY


1

1 I/O STATEMENTS, OPERATORS, EXPRESSIONS DATE:

AIM:
To write a C program to implement the I/O statements, Operators, Expressions.

ALGORITHM:
Step 1: All valid C programs must contain the main() function. The code execution begins from the start of
the main() function.
Step 2: The printf() is a library function to send formatted output to the screen. The function prints the
string inside quotations.
Step 3: To use printf() in our program, we need to include stdio.h header file using the #include <stdio.h>
statement.
Step 4: The return 0; statement inside the main() function is the "Exit status" of the program. It's optional.

FLOW CHART:

I/O STATEMENTS
PROGRAM:
#include<stdio.h>
int main()
{
int a,b,c;
printf("Please enter any two numbers: \n");
scanf("%d %d", &a, &b);
c = a + b;
printf("The addition of two number is: %d", c);
}

OUTPUT:
Please enter any two numbers: 67
33
The addition of two number is: 100

ADDITION OPERATORS
PROGRAM:
#include <stdio.h>
int main()
{
int x=100, y=10, z;
z=x+y;
printf("sum of two numbers is %d\n", z);
}

23GE221- PROGRAMMING IN C | LABORATORY


2

OUTPUT:
Sum of two numbers is 110

EXPRESSIONS
PROGRAM:
#include <stdio.h>
int main()
{
int a,b,result;
printf("Enter 2 numbers for Arithmetic operation \n");
scanf("%d\n%d",&a,&b);
result = a+b;
printf("Addition of %d and %d is = %d \n",a,b,result);
result = a-b;
printf("Subtraction of %d and %d is = %d \n",a,b,result);
result = a*b;
printf("Multiplication of %d and %d is = %d \n",a,b,result);
result = a/b;
printf("Division of %d and %d is = %d \n",a,b,result);
result = a%b;
printf("Modulus(Remainder) when %d divided by %d = %d \n",a,b,result);
int c=a;
result = a++;
printf("Post Increment of %d is = %d \n",c,result);
result = ++a;
printf("Pre Increment of %d is = %d \n",c,result);
result=a--;
printf("Post decrement of %d is = %d \n",c,result);
result=--a;
printf("Pre decrement of %d is = %d \n",c,result);
return 0;
}

OUTPUT:
Enter 2 numbers for Arithmetic operation 10
2
Addition of 10 and 2 is = 12
Subtraction of 10 and 2 is = 8
Multiplication of 10 and 2 is = 20
Division of 10 and 2 is = 5
Modulus (Remainder) when 10 divided by 2 = 0
Post Increment of 10 is = 10
Pre Increment of 10 is = 12
Post decrement of 10 is = 12
Pre decrement of 10 is = 10

RESULT:
Thus the implementation of I/O Statements, Operators & Expressions in C Programming has been
successfully executed and Output was verified.

23GE221- PROGRAMMING IN C | LABORATORY


3

DECISION-MAKING CONSTRUCTS: IF-ELSE, GOTO, SWITCH-CASE,


2 DATE:
BREAK-CONTINUE

AIM:
To write a C program to implement the Decision-making constructs: if-else, goto, switch-case, break-
continue.

ALGORITHM:
Step 1: If the test expression is evaluated to true, statements inside the body of if are executed.
Step 2: If the test expression is evaluated to false, statements inside the body of if are not executed.
Step 3: if statement is the simplest decision-making statement. It is used to decide whether a certain
statement or block of statements will be executed or not i.e. if a certain condition is true then a block
of statement is executed otherwise not.
Step 4: Here, the condition after evaluation will be either true or false. C if statement accepts Boolean values
– if the value is true then it will execute the block of statements below it otherwise not. If we do not
provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if statement will consider the
first immediately below statement to be inside its block.

FLOWCHART:

DECISION-MAKING CONSTRUCTS: IF-ELSE


PROGRAM:
#include<stdio.h>
int main()
{
int a, b;
printf("Please enter the value for a:");
scanf("%d", &a);
printf("\nPlease the value for b:");
scanf("%d", &b);
if (a > b)
{
printf("\n a is greater");
}
else
{
printf("\n b is greater");
}
}

23GE221- PROGRAMMING IN C | LABORATORY


4

OUTPUT:
Please enter the value for a:67
Please the value for b:105
b is greater

DECISION-MAKING CONSTRUCTS: SWITCH CASE


PROGRAM:
#include <stdio.h>
int main()
{
char operation;
int n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operation);
printf("Enter two operands: ");
scanf("%d %d",&n1, &n2);
switch(operation)
{
case '+':
printf("%d+ %d = %d",n1, n2, n1+n2);
break;
case '-':
printf(“%d-%d = %d",n1, n2, n1-n2);
break;
case '*':
printf("%d*%d = %d",n1, n2, n1*n2);
break;
case '/':
printf("%d/%d = %d ",n1, n2, n1/n2);
break;
default:
printf("Error! Operator is not correct");
}
return 0;
}

OUTPUT:
Enter an operator (+, -, *, /): *
Enter two operands:
10
2
10*2 = 20

DECISION-MAKING CONSTRUCTS: GOTO JUMP


PROGRAM:
#include <stdio.h>
int main()
{
const int maxInput = 3;
int i, number, average, sum = 0;
for (i = 1; i <= maxInput; ++i)
{
printf("%d. Enter a number: ", i);
scanf("%d", &number);

23GE221- PROGRAMMING IN C | LABORATORY


5

if (number < 0)
{
goto jump;
}
sum += number;
}
jump:
average = sum / (i - 1);
printf("Sum = %d\n", sum);
printf("Average = %d", average);
return 0;
}

OUTPUT:
1.Enter a number: 23
2.Enter a number: 18
3.Enter a number: -5
Sum = 41
Average = 13

DECISION-MAKING CONSTRUCTS: BREAK-CONTINUE


PROGRAM:
#include <stdio.h>
int main()
{
int i, number, sum = 0;
for (i = 1; i <= 5; ++i)
{
printf("Enter n%d: ", i);
scanf("%d", &number);
if (number < 0)
{
break;
}
sum += number; // sum = sum + number;
}
printf("Sum = %d", sum);
}

OUTPUT:
Enter n1: 4
Enter n2: 3
Enter n3: 8
Enter n4: -5
Sum = 15

RESULT:
Thus the implementation of Decision-making constructs (if-else, goto, switch-case, break-continue) in C
Programming has been successfully executed and Output was verified.

23GE221- PROGRAMMING IN C | LABORATORY


6

3 LOOPS: FOR, WHILE, DO-WHILE DATE:

AIM:
To write a C program to implement the Loops: for, while, do-while.

ALGORITHM:
Step 1: In a for loop, the initial value is performed only once, then the condition tests and compares the
counter to a fixed value after each iteration, stopping the for loop when false is returned.
Step 2: In while loop, a condition is evaluated before processing a body of the loop. If a condition is true
then and only then the body of a loop is executed.
Step 3: In a do…while loop, the condition is always executed after the body of a loop. It is also called an exit-
controlled loop.

FLOWCHART:

LOOP: FOR LOOP


PROGRAM:
#include <stdio.h>
int main()
{
int i;
for (i = 1; i < =5; ++i)
{
printf("%d \n", i);
}
}

OUTPUT:
1
2
3
4
5

LOOP: WHILE LOOP


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

23GE221- PROGRAMMING IN C | LABORATORY


7

{
int i = 1;
while (i < 5)
{
printf("%d\n", i);
++i;
}
}

OUTPUT:
1
2
3
4

LOOP: DO WHILE LOOP


PROGRAM:
#include <stdio.h>
int main()
{
int number, sum = 0;
do
{
printf("Enter a number: ");
scanf("%d", &number);
sum += number;
}while(number != 0);
printf("Sum = %d",sum);
return 0;
}

OUTPUT:
Enter a number: 3
Enter a number: 2
Enter a number: 5
Enter a number: 0
Sum = 10

RESULT:
Thus the implementation of Loop (for, while, do while) in C Programming has been successfully executed
and Output was verified.

23GE221- PROGRAMMING IN C | LABORATORY


8

4 ARRAYS: 1D AND 2D, MULTI-DIMENSIONAL ARRAYS, TRAVERSAL DATE:

AIM:
To write a C program to implement the Arrays: 1D and 2D, Multi-dimensional arrays, Traversal.

ALGORITHM:
Step 1: Arrays have 0 as the first index, not 1. In this example, mark[0] is the first element.
Step 2: If the size of an array is n, to access the last element, the n-1 index is used. In this example, mark[4]
Step 3: Suppose the starting address of mark[0] is 2120d. Then, the address of the mark[1] will be 2124d.
Similarly, the address of mark[2] will be 2128d and so on. This is because the size of a float is 4 bytes.

FLOWCHART:

ONE-DIMENSIONAL ARRAY
PROGRAM:
#include <stdio.h>
int main()
{
int marks[5], i, n, sum = 0, average;
printf("Enter number of subjects:");
scanf("%d", &n);
for(i=0; i < n; ++i)
{
printf("Enter marks%d: ",i+1);
scanf("%d", &marks[i]);
sum += marks[i];
}
average = sum / n;
printf("Average = %d", average);
return 0;
}

OUTPUT:
Enter number of subjects: 4
Enter marks1: 90

23GE221- PROGRAMMING IN C | LABORATORY


9

Enter marks2: 89
Enter marks3: 70
Enter marks4: 78
Average = 81

TWO-DIMENSIONAL ARRAY
PROGRAM:
#include <stdio.h>
int main()
{
int a[2][2], b[2][2], result[2][2];
printf("Enter elements of 1st matrix\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("Enter a%d%d: ", i + 1, j + 1);
scanf("%d", &a[i][j]);
}
printf("Enter elements of 2nd matrix\n");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("Enter b%d%d: ", i + 1, j + 1);
scanf("%d", &b[i][j]);
}
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
result[i][j] = a[i][j] + b[i][j];
}
printf("\nSum Of Matrix:");
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j)
{
printf("%d\t", result[i][j]);
if (j == 1)
printf("\n");
}
}

OUTPUT:
Enter elements of 1st matrix
Enter a11: 6
Enter a12: 7
Enter a21: 4
Enter a22: 5
Enter elements of 2nd matrix
Enter b11: 8
Enter b12: 7
Enter b21: 6
Enter b22: 4
Sum of Matrix: 14 14
10 9

23GE221- PROGRAMMING IN C | LABORATORY


10

MULTI-DIMENSIONAL ARRAY
PROGRAM:
#include <stdio.h>
int main ()
{
int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};
int i, j;
for ( i = 0; i < 5; i++ )
{
for ( j = 0; j < 2; j++ )
{
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}

OUTPUT:
a[0][0] = 0
a[0][1] = 0
a[1][0] = 1
a[1][1] = 2
a[2][0] = 2
a[2][1] = 4
a[3][0] = 3
a[3][1] = 6
a[4][0] = 4
a[4][1] = 8

BINARY TREE TRAVERSAL


PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct node
{
int item;
struct node* left;
struct node* right;
};
void inorderTraversal(struct node* root)
{
if (root == NULL) return;
inorderTraversal(root->left);
printf("%d ->", root->item);
inorderTraversal(root->right);
}
void preorderTraversal(struct node* root)
{
if (root == NULL) return;
printf("%d ->", root->item);
preorderTraversal(root->left);
preorderTraversal(root->right);
}
void postorderTraversal(struct node* root)

23GE221- PROGRAMMING IN C | LABORATORY


11

{
if (root == NULL) return;
postorderTraversal(root->left);
postorderTraversal(root->right);
printf("%d ->", root->item);
}
struct node* createNode(value)
{
struct node* newNode = malloc(sizeof(struct node));
newNode->item = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct node* insertLeft(struct node* root, int value)
{
root->left = createNode(value);
return root->left;
}
struct node* insertRight(struct node* root, int value)
{
root->right = createNode(value);
return root->right;
}
int main()
{
struct node* root = createNode(1);
insertLeft(root, 12);
insertRight(root, 9);
insertLeft(root->left, 5);
insertRight(root->left, 6);
printf("Inorder traversal \n");
inorderTraversal(root);
printf("\nPreorder traversal \n");
preorderTraversal(root);
printf("\nPostorder traversal \n");
postorderTraversal(root);
}

OUTPUT:
Inorder traversal
5 ->12 ->6 ->1 ->9 ->
Preorder traversal
1 ->12 ->5 ->6 ->9 ->
Postorder traversal
5 ->6 ->12 ->9 ->1 ->

RESULT:
Thus the implementation of Arrays (1D and 2D, Multi-dimensional arrays, Traversal) in C Programming
has been successfully executed and Output was verified.

23GE221- PROGRAMMING IN C | LABORATORY


12

5 STRINGS: OPERATIONS - LENGTH, COMPARE, CONCATENATE, COPY DATE:

AIM:
To write a C program to implement the Strings: operations - length, compare, concatenate, copy.

ALGORITHM:
Step 1: String is an array of characters. How to work with strings in C programming and how to use the pre-
defined string handling functions.
Step 2: We will see how to compare two strings, concatenate strings, copy one string to another & perform
various string manipulation operations.
Step 3: We can perform such operations using the pre-defined functions of “string.h” header file. In order
to use these string functions you must include string.h file in your C program.

FLOWCHART:

STRINGS: OPERATIONS
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
char s1[25] = "Siva";
char s2[25] = "Shiva";
char s3[25] = "Hello";
printf("Length of string s1: %d", strlen(s1));
if (strcmp(s1, s2) ==0)
{
printf("String 1 and string 2 are equal");
}

23GE221- PROGRAMMING IN C | LABORATORY


13

else
{
printf("String 1 and 2 are different");
}
strcat(s3,s1);
printf("Output string after concatenation: %s", s3);
strcpy(s1,s2);
printf("String s1 is: %s", s1);
return 0;
}

OUTPUT:
Length of string s1: 4
String 1 and 2 are different
Output string after concatenation: HelloSiva
String s1 is: Shiva

RESULT:
Thus the implementation of String operations (length, compare, concatenate, copy) in C Programming has
been successfully executed and Output was verified.

23GE221- PROGRAMMING IN C | LABORATORY


14

FUNCTIONS: CALL, RETURN, PASSING PARAMETERS BY (VALUE,


6 DATE:
REFERENCE), PASSING ARRAYS TO FUNCTION.

AIM:
To write a C program to implement the Functions: call, return, passing parameters by (value, reference),
passing arrays to function.

ALGORITHM:
Step 1: The call by value method of passing arguments to a function copies the actual value of an argument
into the formal parameter of the function. In this case, changes made to the parameter inside the
function have no effect on the argument. By default, C programming uses call by value to pass
arguments.
Step 2: The call by reference method of passing arguments to a function copies the address of an argument
into the formal parameter. Inside the function, the address is used to access the actual argument
used in the call. It means the changes made to the parameter affect the passed argument.
Step 3: To pass a value by reference, argument pointers are passed to the functions just like any other value.
So accordingly you need to declare the function parameters as pointer types as in the following
function swap(), which exchanges the values of the two integer variables pointed to, by their
arguments.

FLOWCHART:

CALL BY VALUE – SWAP FUNCTIONS


PROGRAM:
#include <stdio.h>
void swap(int x, int y);
int main ()
{
int a = 100, b = 200;

23GE221- PROGRAMMING IN C | LABORATORY


15

printf("Before swap, value of a : %d\n", a );


printf("Before swap, value of b : %d\n", b );
swap(a, b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b ); return 0;
}
void swap(int x, int y)
{
int temp;
temp = x;
x = y;
y = temp;
return;
}

OUTPUT:
Before swap, value of a : 100
Before swap, value of b : 200
After swap, value of a : 100
After swap, value of b : 200

CALL BY REFERENCE - SWAP FUNCTIONS


PROGRAM:
#include <stdio.h>
int main ()
{
int a = 100, b = 200;
printf("Before swap, value of a : %d\n", a );
printf("Before swap, value of b : %d\n", b );
swap(&a, &b);
printf("After swap, value of a : %d\n", a );
printf("After swap, value of b : %d\n", b );
return 0;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
return;
}

OUTPUT:
Before swap, value of a : 100
Before swap, value of b : 200
After swap, value of a : 200
After swap, value of b : 100

PASSING ARRAYS TO FUNCTION


PROGRAM:
#include <stdio.h>
void display(int age1, int age2)
{

23GE221- PROGRAMMING IN C | LABORATORY


16

printf("%d\n", age1);
printf("%d\n", age2);
}
int main()
{
int ageArray[] = {2, 8, 4, 12};
display(ageArray[1], ageArray[2]);
return 0;
}

OUTPUT:
8
4

CALLING A FUNCTION
PROGRAM:
#include <stdio.h>
int max(int num1, int num2);
int main ()
{
int a = 100, b = 200, ret;
ret = max(a, b);
printf( "Max value is : %d\n", ret );
return 0;
}
int max(int num1, int num2)
{
int result;
if (num1 > num2)
{
result = num1;
}
else
{
result = num2;
}
return result;
}

OUTPUT:
Max value is: 200

FUNCTION WITH NO ARGUMENT AND NO RETURN VALUE


PROGRAM:
#include <stdio.h>
void value(void);
int main()
{
value();
}
void value(void)
{
int year = 1, period = 5, amount = 5000, inrate = 0.12;
float sum;

23GE221- PROGRAMMING IN C | LABORATORY


17

sum = amount;
while (year <= period)
{
sum = sum * (1 + inrate);
year = year + 1;
}
printf(" The total amount is %f", sum);
}

OUTPUT:
The total amount is 5000.000000

RESULT:
Thus the implementation of Functions (call, return, passing parameters by (value, reference), passing
arrays to function) in C Programming has been successfully executed and Output was verified.

23GE221- PROGRAMMING IN C | LABORATORY


18

7 RECURSION - FACTORIAL AND FIBONACCI OF THE NUMBER DATE:

AIM:
To write a C program to implement the recursion function.

ALGORITHM:
Step 1: Recursion is the process of repeating items in a self-similar way. In programming languages, if a
program allows you to call a function inside the same function, then it is called a recursive call of the
function.
Step 2: The C programming language supports recursion, i.e., a function to call itself. But while using
recursion, programmers need to be careful to define an exit condition from the function, otherwise
it will go into an infinite loop.
Step 3: Recursive functions are very useful to solve many mathematical problems, such as calculating the
factorial of a number, generating Fibonacci series, etc.

FLOWCHART:

RECURSION FACTORIAL
PROGRAM:
#include <stdio.h>
unsigned long long int factorial(unsigned int i)
{
if(i <= 1)
{
return 1;
}
return i * factorial(i - 1);
}
int main()
{
int i = 5;

23GE221- PROGRAMMING IN C | LABORATORY


19

printf("Factorial of %d is %d\n", i, factorial(i));


return 0;
}

OUTPUT:
Factorial of 5 is 120

RECURSION FIBONACCI
PROGRAM:
#include <stdio.h>
int fibonacci(int i)
{
if(i == 0)
{
return 0;
}
if(i == 1)
{
return 1;
}
return fibonacci(i-1) + fibonacci(i-2);
}
int main()
{
int i;
for (i = 0; i < 10; i++)
{
printf("%d\t\n", fibonacci(i));
}
return 0;
}

OUTPUT:
0
1
1
2
3
5
8
13
21
34

RESULT:
Thus the implementation of recursion functions with factorial value and Fibonacci series in C
Programming has been successfully executed and Output was verified.

23GE221- PROGRAMMING IN C | LABORATORY


20

POINTERS: POINTERS TO FUNCTIONS, ARRAYS, STRINGS, POINTERS


8 DATE:
TO POINTERS, ARRAY OF POINTERS

AIM:
To write a C program to implement the Pointers: Pointers to functions, Arrays, Strings, Pointers to
Pointers, Array of Pointers.

ALGORITHM:
Step 1: Unlike normal pointers, a function pointer points to code, not data. Typically a function pointer
stores the start of executable code.
Step 2: Unlike normal pointers, we do not allocate de-allocate memory using function pointers.
Step 3: A function’s name can also be used to get functions’ address. For example, in the below program, we
have removed address operator ‘&’ in assignment. We have also changed function call by removing
*, the program still works.

FLOWCHART:

POINTERS
PROGRAM:
#include <stdio.h>
void TechVidvan()
{
int var = 25;
int *pt;
pt = &var;
printf("Address is: %p \n",pt);
printf("Value is: %d \n", *pt);
}
int main()
{
TechVidvan();
}

23GE221- PROGRAMMING IN C | LABORATORY


21

OUTPUT:
Address is: 0x7ffc9e1fb704
Value is: 25

ARRAY OF POINTERS
PROGRAM:
#include <stdio.h>
int main()
{
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i)
{
// Equivalent to scanf("%d", &x[i]);
scanf("%d", x+i);
// Equivalent to sum += x[i]
sum += *(x+i);
}
printf("Sum = %d", sum);
return 0;
}

OUTPUT:
Enter 6 numbers:
7
8
9
3
2
7
Sum = 36

POINTER TO POINTER
PROGRAM:
#include <stdio.h>
int main ()
{
int var;
int *ptr;
int **pptr;
var = 3000;
ptr = &var; pptr = &ptr;
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

23GE221- PROGRAMMING IN C | LABORATORY


22

Value available at **pptr = 3000

POINTERS TO STORE STRING


PROGRAM:
#include <stdio.h>
int main(void)
{
char *strPtr = "Hello";
char *t = strPtr;
while(*t != '\0')
{
printf("%c", *t); t++;
}
return 0;
}

OUTPUT:
Hello

RESULT:
Thus the implementation of Pointers (Pointers to functions, Arrays, Strings, Pointers to Pointers, Array of
Pointers) in C Programming has been successfully executed and Output was verified.

23GE221- PROGRAMMING IN C | LABORATORY


23

STRUCTURES: NESTED STRUCTURES, POINTERS TO STRUCTURES,


9 DATE:
ARRAYS OF STRUCTURES AND UNIONS

AIM:
To write a C program to implement the Structures: Nested Structures, Pointers to Structures, Arrays of
Structures and Unions.

ALGORITHM:
Step 1: Passing each item of the structure as a function argument. It is similar to passing normal values as
arguments. Although it is easy to implement, we don’t use this approach because if the size of a
structure is a bit larger, then our life becomes miserable.
Step 2: Pass the whole structure as a value.
Step 3: We can also Pass the address of the structure (pass by reference).
Step 4: A union is a user-defined type similar to structs in C except for one key difference.
Step 5: Structures allocate enough space to store all their members, whereas unions can only hold one
member value at a time.

FLOWCHART:

NESTED STRUCTURE
PROGRAM:
#include <stdio.h>
#include <string.h>
struct Employee
{
int employee_id;
char name[20];
int salary;
};
struct Organisation
{
char organisation_name[20];
char org_number[20];

23GE221- PROGRAMMING IN C | LABORATORY


24

struct Employee emp;


};
int main()
{
struct Organisation org;
printf("The size of structure organisation : %ld\n", sizeof(org));
org.emp.employee_id = 101;
strcpy(org.emp.name, "Karthik");
org.emp.salary = 400000;
strcpy(org.organisation_name,"Infosis");
strcpy(org.org_number, "GFG123768");
printf("Organisation Name : %s\n",org.organisation_name);
printf("Organisation Number : %s\n",org.org_number);
printf("Employee id : %d\n",org.emp.employee_id);
printf("Employee name : %s\n",org.emp.name);
printf("Employee Salary : %d\n",org.emp.salary);
}

OUTPUT:
The size of structure organisation : 68
Organisation Name : Infosis Organisation
Number : GFG123768
Employee id : 101
Employee name : Karthik
Employee Salary : 400000

POINTERS TO STRUCTURE
PROGRAM:
#include <stdio.h>
struct person
{
int age;
float weight;
};
int main()
{
struct person *personPtr, person1;
personPtr = &person1;
printf("Enter age: ");
scanf("%d", &personPtr->age);
printf("Enter weight: ");
scanf("%f", &personPtr->weight);
printf("Displaying:\n");
printf("Age: %d\n", personPtr->age);
printf("weight: %f", personPtr->weight);
return 0;
}

OUTPUT:
Enter age: 34
Enter weight: 60
Displaying:
Age: 34
weight: 60.000000

23GE221- PROGRAMMING IN C | LABORATORY


25

UNION OF STRUCTURE
PROGRAM:
#include <stdio.h>
union unionJob
{
char name[32];
float salary;
int workerNo;
} uJob;
struct structJob
{
char name[32];
float salary;
int workerNo;
} sJob;
int main()
{
printf("size of union = %d bytes", sizeof(uJob));
printf("\nsize of structure = %d bytes", sizeof(sJob));
return 0;
}

OUTPUT:
size of union = 32 bytes
size of structure = 40 bytes

ARRAY OF STRUCTURE
PROGRAM:
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[30];
float percentage;
};
int main()
{
int i;
struct student record[2];
record[0].id=1;
strcpy(record[0].name, "Bhanu");
record[0].percentage = 86.5;
record[1].id=2;
strcpy(record[1].name, "Priya");
record[1].percentage = 90.5;

23GE221- PROGRAMMING IN C | LABORATORY


26

record[2].id=3;
strcpy(record[2].name, "Hari");
record[2].percentage = 81.5;
for(i=0; i<3; i++)
{
printf(" Records of STUDENT : %d \n", i+1);
printf(" Id is: %d \n", record[i].id);
printf(" Name is: %s \n", record[i].name);
printf(" Percentage is: %f\n\n",record[i].percentage);
}
return 0;

OUTPUT:
Records of STUDENT : 1
Id is: 1
Name is: Karthi
Percentage is: 76.500000

Records of STUDENT : 2
Id is: 2
Name is: Sabari
Percentage is: 86.500000

Records of STUDENT : 3
Id is: 3
Name is: Hari
Percentage is: 85.500000

RESULT:
Thus the implementation of Structure (Nested Structures, Pointers to Structures, Arrays of Structures and
Unions) in C Programming has been successfully executed and Output was verified.

23GE221- PROGRAMMING IN C | LABORATORY


27

FILES: READING AND WRITING, FILE POINTERS, FILE OPERATIONS,


10 DATE:
RANDOM ACCESS, PROCESSOR DIRECTIVES

AIM:
To write a C program to implement the Files: Reading and Writing, File Pointers, File Operations, Random
access, Processor Directives.

ALGORITHM:
Step 1: Opening a file - for creation and edit:-
• Opening a file is performed using the fopen() function defined in the stdio.h header file.
• The syntax for opening a file in standard I/O is:
• ptr = fopen("fileopen","mode");
• For example,
• fopen("E:\\cprogram\\newprogram.txt","w");
Step 2: Closing a File
• The file (both text and binary) should be closed after reading/writing.
• Closing a file is performed using the fclose() function.
• fclose(fptr);
• Here, fptr is a file pointer associated with the file to be closed.
Step 3: Reading and writing to a text file
• For reading and writing to a text file, we use the functions fprintf() and fscanf().

FLOWCHART:

READING AND WRITING TO A TEXT FILE


PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;

23GE221- PROGRAMMING IN C | LABORATORY


28

//use appropriate location if you are using MacOS or Linux


fptr = fopen("C:\\program.txt","w");
if(fptr == NULL)
{
printf("Error!"); exit(1);
}
printf("Enter num: "); scanf("%d",&num);
fprintf(fptr,"%d",num); fclose(fptr);
return 0;
}

OUTPUT:
Error!

READ FROM A TEXT FILE


PROGRAM:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num;
FILE *fptr;
if ((fptr = fopen("C:\\program.txt","r")) == NULL)
{
printf("Error! opening file");
// Program exits if the file pointer returns NULL.
exit(1);
}
fscanf(fptr,"%d", &num);
printf("Value of n=%d", num);
fclose(fptr);
return 0;
}

OUTPUT:
Error! opening file

FILE POINTERS
PROGRAM:
# include <stdio.h>
# include <string.h>
int main( )
{
FILE *filePointer ;
char dataToBeWritten[50]= "GeeksforGeeks-A Computer Science Portal for Geeks";
filePointer = fopen("GfgTest.c", "w") ;
if ( filePointer == NULL )
{
printf( "GfgTest.c file failed to open." ) ;
}
else

23GE221- PROGRAMMING IN C | LABORATORY


29

{
printf("The file is now opened.\n") ;
if ( strlen ( dataToBeWritten ) > 0 )
{
fputs(dataToBeWritten, filePointer) ;
fputs("\n", filePointer) ;
}
fclose(filePointer) ;
printf("Data successfully written in file GfgTest.c\n");
printf("The file is now closed.") ;
}
return 0;
}

OUTPUT:
GfgTest.c file failed to open.

WRITE TO A BINARY FILE USING FWRITE()


PROGRAM;
#include <stdio.h>
#include <stdlib.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:\\program.bin","wb")) == NULL)
{
printf("Error! opening file");
exit(1);
}
for(n = 1; n < 5; ++n)
{
num.n1 = n;
num.n2 = 5*n;
num.n3 = 5*n + 1;
fwrite(&num, sizeof(struct threeNum), 1, fptr);
}
fclose(fptr);
return 0;
}

23GE221- PROGRAMMING IN C | LABORATORY


30

OUTPUT:
Error! opening file.

WRITE TO A BINARY FILE USING FWRITE()


PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:\\program.bin","rb")) == NULL)
{
printf("Error! opening file"); exit(1);
}
for(n = 1; n < 5; ++n)
{
fread(&num, sizeof(struct threeNum), 1, fptr);
printf("n1: %d\tn2: %d\tn3: %d\n", num.n1, num.n2, num.n3);
}
fclose(fptr);
return 0;
}

OUTPUT:
Error! opening file

PROGRAM FOR FSEEK()


PROGRAM:
#include <stdio.h>
#include <stdlib.h>
struct threeNum
{
int n1, n2, n3;
};
int main()
{
int n;
struct threeNum num;
FILE *fptr;
if ((fptr = fopen("C:\\program.bin","rb")) == NULL)

23GE221- PROGRAMMING IN C | LABORATORY


31

{
printf("Error! opening file"); exit(1);
}
fseek(fptr, -sizeof(struct threeNum), SEEK_END);
for(n = 1; n < 5; ++n)
{
fread(&num, sizeof(struct threeNum), 1, fptr);
printf("n1: %d\tn2: %d\tn3: %d\n", num.n1, num.n2, num.n3);
fseek(fptr, -2*sizeof(struct threeNum), SEEK_CUR);
}
fclose(fptr);
return 0;
}

OUTPUT:
Error! opening file

FTELL() FUNCTION
PROGRAM:
#include<stdio.h>
int main()
{
FILE *fp; fp=fopen("scaler.txt","r");
if(!fp)
{
printf("Error: File cannot be opened\n") ; return 0;
}
printf("Position pointer in the beginning : %ld\n",ftell(fp));
char ch;
while(fread(&ch,sizeof(ch),1,fp)==1)
{
printf("%c",ch);
}
printf("\nSize of file in bytes is : %ld\n",ftell(fp));
fclose(fp);
return 0;
}

OUTPUT:
Error: File cannot be opened

RESULT:
Thus the implementation of the Files (Reading and Writing, File Pointers, File Operations, Random access,
Processor Directives) in C Programming has been successfully executed and Output was verified.

23GE221- PROGRAMMING IN C | LABORATORY


32

CONTENT
BEYOND
SYLLABUS

23GE221- PROGRAMMING IN C | LABORATORY


33

C PROGRAM TO CONVERT UPPERCASE STRING TO LOWERCASE


1 DATE:
STRING

AIM:
To write a C program to convert uppercase string to lowercase string.

CONVERT UPPERCASE STRING TO LOWERCASE STRING


PROGRAM:
#include<stdio.h>
#include<string.h>
int main()
{
char str[25];
int i;
printf("Enter the string: ");
scanf("%s",str);
for(i=0;i<=strlen(str);i++)
{
if(str[i]>=65&&str[i]<=90)
{
str[i]=str[i]+32;
}
}
printf("\nLower Case String is: %s",str);
return 0;
}

OUTPUT:
Enter the string: SIVA
Lower Case String is: siva

RESULT:
Thus the conversion of uppercase string to lowercase string in C Programming has been successfully
executed and Output was verified.

23GE221- PROGRAMMING IN C | LABORATORY


34

2 PROGRAM TO FIND THE AREA OF A RECTANGLE DATE:

AIM:
To write a C program to find the area of a rectangle.

AREA OF A RECTANGLE
PROGRAM:
#include<stdio.h>
int main()
{
// declare variables
float len, wid, area;
// take inputs
printf("Enter length & width of Rectangle (in cm): ");
scanf("%f %f",&len,&wid);
// calculate area
area = len * wid;
// display result
printf("Area of Rectangle= %.3f cm\n",area);
return 0;
}

OUTPUT:
Enter length & width of Rectangle (in cm): 12.5
15.2
Area of Rectangle= 190.000 cm

RESULT:
Thus the calculation of area of rectangle in C Programming has been successfully executed and Output
was verified.

23GE221- PROGRAMMING IN C | LABORATORY

You might also like