0% found this document useful (0 votes)
254 views13 pages

Os Lab 3

The document provides an overview of operating system lab 3 on C programming at the University of Central Punjab's Faculty of Information Technology. It covers various topics in C programming like data types, input/output, control structures like if/else statements, looping, functions, and command line arguments. It also includes example code and tasks for students to practice the concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
254 views13 pages

Os Lab 3

The document provides an overview of operating system lab 3 on C programming at the University of Central Punjab's Faculty of Information Technology. It covers various topics in C programming like data types, input/output, control structures like if/else statements, looping, functions, and command line arguments. It also includes example code and tasks for students to practice the concepts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

University of Central

Punjab
FACULTY OF INFORMATION TECHNOLOGY

Operating System
Lab No 3
C Programming

Faculty of Information Technology

UCP Lahore Pakistan

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central
Punjab
FACULTY OF INFORMATION TECHNOLOGY

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++

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central
Punjab
FACULTY OF INFORMATION TECHNOLOGY

Basic C programing

Data Type

Type Size (Bytes) Format Specifier


int at least 2, usually 4 %d
char 1 %c
flaot 4 %f
double 8 %lf
short int 2 usually %hd
unsigned int at least 2, usually 4 %u
long int at least 4, usually 8 %li
long long int at least 8 %lli
unsigned long int at least 4 %lu
unsigned long long int at least 8 %llu
long double at least 10, usually 12 or 16 %lf
signed char 1 %c
unsigned char 1 %c
c-string (char[ ]) %s
pointer 4 %p

Input/output

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central
#include <stdio.h>
int main()
Punjab
FACULTY OF INFORMATION TECHNOLOGY
{

printf(" H ello, World!");


return 0;
}

The #include <stdio.h> is a preprocessor command. This command tells compiler to


include the contents of stdio.h (standard input and output) file in the program. The stdio.h
file contains functions such as scanf () and printf () to take input and display output
respectively. If you use printf () function without writing #include <stdio.h>, the program
will not be compiled. The execution of a C program starts from the main () function. The
printf () is a library function to send formatted output to the screen. In this program, the
printf () displays Hello, World! Text on the screen. The return 0; statement is the "Exit
status" of the program. In simple terms, program ends with this statement.

CP Task 1 (0.5 Marks)

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.

Area of rectangle = length * width

Sample Output:

What is the length of rectangle? 10


What is the width of rectangle? 20
The area of rectangle is 200.

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central
Punjab
FACULTY OF INFORMATION TECHNOLOGY
Control Structures

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
}

How if statement works?


The if statement evaluates the test expression inside the parenthesis ().
If the test expression is evaluated to true, statements inside the body of if are executed.
If the test expression is evaluated to false, statements inside the body of if are not
executed.
C if...else Statement

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
}

How if...else statement works?


If the test expression is evaluated to true, statements inside the body of if are executed.
Statements inside the body of else are skipped from execution. If the test expression is

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central
Punjab
FACULTY OF INFORMATION TECHNOLOGY
evaluated to false, statements inside the body of else are executed statements inside the
body of if are skipped from execution.

CP Task 2 (0.5 Marks)

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.

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central
Punjab
FACULTY OF INFORMATION TECHNOLOGY

Looping

The syntax of the for loop is:


for (initializationStatement; testExpression; updateStatement)
{
// statements inside the body of loop
}

How for loop works?

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.

CP Task 03 (0.5 Marks)

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.

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central

Sample Input:

Input Sequence: 15 3 2 16 7 9 12 25 5 56 8 2 -99

FACULTY OF INFORMATION TECHNOLOGY

Sample Output:

The entered sequence is: 15 3 2 16 7 9 12 25 5 56 8 2

Updated sequence is: 2 2 3 5 7 8 9 12 15 16 25 56

Functions

A function is a block of code that performs a specific task.

#include <stdio.h>
void functionName(){
}
int main()
{ functionName()
;
}

CP Task 4 (0.5)

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central Punjab
(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the
Punjab)

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)

FACULTY OF INFORMATION TECHNOLOGY

Command Line Arguments

Command line argument is a parameter supplied to the program when it is invoked.


Command line argument is an important concept in C programming. It is mostly used
when you need to control your program from outside. Command line arguments are
passed to the main() method.

int main (int argc , char ** argv)


#include <stdio.h>
int main(int argc, char **argv){
printf(“No of arguments %d”,argc);
return 0;
}

CP Task 5 (0.5)

Write a C program to receive two integers from command line and Multiply them.

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central
Dynamic memory allocation

The name "malloc" stands for memory allocation.


The malloc() function reserves a block of memory of the specified number of bytes. And,
it returns a pointer of void which can be casted into pointers of any form.

ptr = (castType*) malloc(size);


ptr = (int*) malloc(100 * sizeof(int);

(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the


Punjab)

FACULTY OF INFORMATION TECHNOLOGY

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

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central
Structures

Syntax of struct
struct structureName {
dataType member1;
dataType member2;
};

https://www.studytonight.com/c/structures-in-c.php

CP Task 7 (0.5)

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central
Write a C program stores the information (id, name, age and pay) of an employee and
displays it on the screen using structures
(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the Punjab)

FACULTY OF INFORMATION TECHNOLOGY

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:

==================== MENU ===================


1. Add a record
2. Search a record by ID
3. Show all records
4. Show employees having pay less then basic pay (20000)
5. Save and exit

Edited by M.waqar Mughal & Sherjeel Ahmad


University of Central
Fig. 1. File format (ID Name Gender Position Experience Pay)

(Incorporated by Ordinance No. XXIV of 2002 promulgated by Government of the


Punjab)

FACULTY OF INFORMATION TECHNOLOGY


Hint. You can read all the records at a time using structures array. You can update file by opening it in
append mode.

Functions prototype

void printEMP(employee tmp);


void readData(employee rec[], int &count);
void addRecord(employee rec[], int*count);
void searchByID(char reg[], employee rec[], int
count);
void showAllRecord(employee rec[], int count);
void showBelowBasicpay(employee rec[], int count, int
basicpay);

REFERENCE LINK
HTTPS://WWW.PROGRAMIZ.COM/C-PROGRAMMING/C-FILE-INPUT-OUTPUT

Edited by M.waqar Mughal & Sherjeel Ahmad

You might also like