C PROGRAM MANUAL COM 121
C PROGRAM MANUAL COM 121
ON
C PROGRAMMING LANGUAGE
(COM 121)
LAB EXERCISE 1
OBJECTIVES:
2. Understand the benefits of use of DEV C/C++ for windows as an application tool.
PROCEDURE:
EXERCISE:
a. Highlight the with a clear screenshots, the step taken to install DEV C/C++
compiler on your PCs
b. Document your reports and print out
CONCLUSION: The students have been taught the overview of the DEV C/C++ Integrated
Development Environment and the various components on the IDE.
NOTE: The technologist/Lab Assistant should assist the students during the practical work.
LAB EXERCISE 2
OBJECTIVE: At the end of the practical class, student should be able to demonstrate the use of Input
(scanf) for getting user’s input and Output operation (printf) to display the result of
processes.
PROCEDURE:
EXERCISE:
a. Write a C program that accept 5 numbers from user and display the sum and
average of the numbers
b. Develop a C program that takes a number as input and prints the multiplication
table for that number up to 10.
CONCLUSION: The students have been shown and taught how to work with functions,
modules and libraries C programming Language
NOTE: The technologist/Lab Assistant should assist the students during the practical work.
LAB EXERCISE 3
OBJECTIVE: At the end of the lab exercise, student should be able to demonstrate how to use
different control structures in program
PROCEDURE
EXERCISE:
a. Develop menu driven C program for a simple calculator that can perform
addition, subtraction, multiplication, and division based on user input. Use a
switch statement.
b. Create a C program to display the multiplication table of 12 x 12 using a for
loop.
CONCLUSION: The students have been shown and taught how to work with control structures
and its implementation in decision making using C programming Language
NOTE: The technologist/Lab Assistant should assist the students during the practical work.
LAB EXERCISE 4
OBJECTIVE: At the end of the Lab exercise, the students should be able to understand what functions
and scope rules are and also demonstrate how to perform function calls in program
PROCEDURE
1. Function Declaration:
Declare the function prototype. Specify the return type, function name,
and parameters (if any).
int add(int a, int b);
2. Function Definition:
Implement the function. Define the code that performs the desired
task.
int add(int a, int b) { return a + b; }
3. Main Program:
In the main function or any other function, call the defined function.
int main() { int result = add(5, 3); // Use the result or print it return 0; }
4. Compile and Run:
Compile the C code using a C compiler (e.g., DEV C/C++).
Execute the compiled program to see the result.
5. Check the Output:
Verify that the program produces the expected output based on the
defined function.
EXERCISE:
a. Write a C program that defines a function add which takes two integers as
parameters and returns their sum. In the main function, call this function and
print the result.
b. Write a C program that includes a function power to calculate the power of a
number. The function should take two integers as parameters (base and
exponent) and return the result. In the main function, call this function and
print the result.
CONCLUSION: The students have been shown and taught how to work with functions, and
scope rules C programming Language.
NOTE: The technologist/Lab Assistant should assist the students during the practical work.
LAB EXERCISE 5
OBJECTIVE: At the end of the Lab exercise, the students should be able to understand what arrays and
strings are and also demonstrate how to implement an array in programs.
PROCEDURE
1. Array Declaration:
Declare an array by specifying the data type and size.
int numbers[5];
2. Array Initialization:
Optionally, initialize the array elements.
int numbers[5] = {1, 2, 3, 4, 5};
3. Array Indexing:
Access individual elements using indices (starting from 0).
int thirdElement = numbers[2];
4. Array Modification:
Modify elements by assigning new values.
numbers[2] = 10;
5. Array Iteration:
Use loops to iterate through the array.
for (int i = 0; i < 5; i++) { // Access and use numbers[i] }
6. Multidimensional Arrays (Optional):
For multidimensional arrays, declare and access elements with
multiple indices.
int matrix[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
7. Array as Function Parameter:
Pass arrays to functions by reference.
void printArray(int arr[], int size) { // Function code }
8. Pointer Arithmetic (Optional):
Explore pointer arithmetic for advanced array manipulation.
int *ptr = numbers; int thirdElement = *(ptr + 2);
9. Dynamic Memory Allocation (Optional):
For dynamic arrays, use functions like malloc and free.
int *dynamicArray = (int *)malloc(5 * sizeof(int));
10. Compile and Run:
Compile the C code using a C compiler (e.g., DEV C/C++).
Execute the compiled program to see the result.
EXERCISE:
a. Write a C program that calculates and prints the sum of all elements in an integer
array.
b. Develop a C program that copies elements from one integer array to another. Print
both the original and copied arrays.
CONCLUSION: The students have been shown and taught how to work with arrays, and strings
C programming Language.
NOTE: The technologist/Lab Assistant should assist the students during the practical work.
LAB EXERCISE 6
OBJECTIVE: At the end of the Lab exercise, the students should be able to understand what pointer
operation and also demonstrate how to how to use pointers in programs.
PROCEDURE:
1. Pointer Declaration:
Declare a pointer variable of a specific data type.
int *ptr;
2. Assign Address to Pointer:
Assign the address of a variable to the pointer.
int number = 10; int *ptr = &number;
3. Dereference Pointer:
Access the value at the memory location pointed to by the pointer
using the dereference operator *.
int value = *ptr;
4. Pointer Arithmetic:
Explore pointer arithmetic for navigating through arrays or allocating
memory.
int arr[5] = {1, 2, 3, 4, 5}; int *ptr = arr; // Points to the first element
int thirdElement = *(ptr + 2);
5. Null Pointer:
Use null pointers to represent no valid address.
int *ptr = NULL;
6. Function Pointers (Optional):
Declare and use function pointers for advanced applications.
int add(int a, int b) { return a + b; } int (*ptr)(int, int) = &add;
7. Dynamic Memory Allocation:
Use pointers with malloc, calloc, and free for dynamic memory
allocation.
int *dynamicArray = (int *)malloc(5 * sizeof(int));
8. Pointer to Structures (Optional):
Explore pointers to structures for working with complex data types.
struct Point { int x; int y; }; struct Point p1; struct Point *ptr = &p1;
9. Pointer as Function Parameter:
Pass pointers to functions for efficient parameter passing.
void updateValue(int *ptr) { *ptr = 20; }
10. Compile and Run:
Compile the C code using a C compiler (e.g., GCC).
Execute the compiled program to see the result.
11. Check the Output:
Verify that the program produces the expected output based on pointer
operations.
EXERCISE:
a. Write a C program that uses a pointer to find and print the maximum element
in an array of integers.
b. Develop a C program that uses function pointers to implement a basic
calculator. Define functions for addition, subtraction, multiplication, and
division, and use function pointers to perform operations based on user input.
CONCLUSION: The students have been shown and taught how to work with pointers operation
in C programming Language.
NOTE: The technologist/Lab Assistant should assist the students during the practical work.
LAB EXERCISE 7
OBJECTIVE: At the end of the Lab exercise, the students should be able to understand what structures
and union data types are and also demonstrate how to implement structures and union
types in programs.
PROCEDURE
1. Define Structure:
Define a structure by specifying the data types of its members.
struct Point { int x; int y; };
2. Declare Structure Variable:
Declare variables of the structure type.
struct Point p1;
3. Access Structure Members:
Access members of the structure using the dot operator.
p1.x = 10; p1.y = 20;
4. Initialize Structure:
Optionally, initialize the structure at the time of declaration.
struct Point p2 = {5, 15};
5. Pass Structure to Function:
Pass structures to functions by value or reference.
void printPoint(struct Point p) { // Access members using p.x and p.y }
EXERCISE:
CONCLUSION: The students have been shown and taught how to work with structures and
union types in C programming Language.
NOTE: The technologist/Lab Assistant should assist the students during the practical work.