C Lab Manual
C Lab Manual
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)
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
6. Functions: call, return, passing parameters by (value, reference), passing arrays to function.
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.
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);
}
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.
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:
OUTPUT:
Please enter the value for a:67
Please the value for b:105
b is greater
OUTPUT:
Enter an operator (+, -, *, /): *
Enter two operands:
10
2
10*2 = 20
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
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.
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:
OUTPUT:
1
2
3
4
5
{
int i = 1;
while (i < 5)
{
printf("%d\n", i);
++i;
}
}
OUTPUT:
1
2
3
4
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.
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
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
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
{
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.
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");
}
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.
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:
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
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
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
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.
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;
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.
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();
}
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
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.
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];
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
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;
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.
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:
OUTPUT:
Error!
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
{
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.
OUTPUT:
Error! opening file.
OUTPUT:
Error! opening file
{
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.
CONTENT
BEYOND
SYLLABUS
AIM:
To write a C program to convert uppercase string to lowercase string.
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.
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.