Os Lab 3
Os Lab 3
Punjab
FACULTY OF INFORMATION TECHNOLOGY
Operating System
Lab No 3
C Programming
Topics to be covered
Basic C program coding
a. Data Type
b.
Input/output
c. Control structures (If/else/switch)
d. Looping
e. Functions with/without
parameters f. Command Line
Arguments
g. Dynamic memory allocation
h. Structures
Objectives:
• Students able to understand the concept of C Programing.
Pre-requisite:
• GCC installed in the system
• Visual studio code must be installed.
• Basic concept of C++
Basic C programing
Data Type
Input/output
Write a C program that ask the user to enter a length and width of a rectangle. It calculates
the rectangle area and display it on screen.
Sample Output:
C if Statement
The syntax of the if statement in C programming is:
if (test expression)
{
// statements to be executed if the test expression is true
}
The if statement may have an optional else block. The syntax of the if..else statement is:
if (test expression) {
// statements to be executed if the test expression is true
}
else {
// statements to be executed if the test expression is fa lse
}
Prompt a user to enter the length of three sides of a triangle. Determine if these three sides
form a valid triangle. If so than determine if the triangle is scalene, isosceles or
equilateral. Hint:
In triangle no one side can be greater than the sum of other two sides.
• Scalene: No sides of the triangle are equal to each other.
• Isosceles: Two sides of the triangle are equal.
• Equilateral: All three sides are equal.
Looping
The initialization statement is executed only once. Then, the test expression is evaluated.
If the test expression is evaluated to false, the for loop is terminated. However, if the
test expression is evaluated to true, statements inside the body of for loop are executed, and
the update expression is updated. Again the test expression is evaluated. This process
goes on until the test expression is false. When the test expression is false, the loop
terminates.
Write a program to perform sorting of numbers (Bubble Sort). The program shall take an
array of 20 integers as input, if user wants to enter less than 20 numbers, user shall
terminate it with -99. The program shall then sort the sequence in ascending order and
print both the original and sorted sequence.
Sample Input:
Sample Output:
Functions
#include <stdio.h>
void functionName(){
}
int main()
{ functionName()
;
}
CP Task 4 (0.5)
Write a void checkPrimeNumber() function which takes input from the user, checks whether
it is a prime number or not and displays it on the screen. (2+2+1)
Sample Output: -
Enter the number: = 66
Output: Not Prime
(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the Punjab)
CP Task 5 (0.5)
Write a C program to receive two integers from command line and Multiply them.
https://www.programiz.com/c-programming/c-dynamic-memory-allocation
CP Task 6 (0.5)
Write a C program to display second min number in an array. User enter integers and
you need to calculate the size of array and allocate run time memory for array.
Sample Output: -
Enter the array elements: = 1 2 3 4 5 6 7 8 9 0
Output:1
Syntax of struct
struct structureName {
dataType member1;
dataType member2;
};
https://www.studytonight.com/c/structures-in-c.php
CP Task 7 (0.5)
CP Task 8 (10.5)
You are given a file “task1.txt” containing record of 10 employees (Fig. 1). Write a C
program to manage employee’s information using structures. The information of an
employee contains ID (i.e. Emp01), Name (i.e. without space), gender (i.e. m/f), job
position (i.e. internee/developer without spaces), experience in years (i.e. 1/2) and
pay. All the data will be saved to a file in specific format as depicted in Fig. 1.
Maximum number of employees are 100. The program will prompt the user a Menu
for different operations as shown below:
Functions prototype
REFERENCE LINK
HTTPS://WWW.PROGRAMIZ.COM/C-PROGRAMMING/C-FILE-INPUT-OUTPUT