CC-112L Programming Fundamentals Laboratory 02 Introduction To C & Structured Program Development
CC-112L Programming Fundamentals Laboratory 02 Introduction To C & Structured Program Development
CC-112L Programming Fundamentals Laboratory 02 Introduction To C & Structured Program Development
Programming Fundamentals
Laboratory 02
Version: 1.0.0
Contents:
• Learning Objectives
• Required Resources
• General Instructions
• Background and Overview
o C Language
o Program Sequence
o Arithmetic Operators
o Relational Operators
o if Statement
o if…else Statement
o Increment & Decrement Operators
o Microsoft ® Visual Studio
• Activities
o Pre-Lab Activity
▪ MinGW installation
▪ A simple C Program
▪ Comments
▪ Preprocessor Directive
▪ Escape sequences
▪ Execution Sequence of a Program
▪ Arithmetic Operators
▪ Task 01: Adding Two Numbers
▪ Task 02: Arithmetic Expressions
o In-Lab Activity
▪ Execute C program with Command Prompt ▪
Relational Operators
▪ if Statement
▪ Multiple if Statement
▪ if…else Statement
▪ if…else if Statement
▪ Increment and Decrement Operators
▪ Task 01: Find Largest Number
▪ Task 02: Fill in the blank area
▪ Task 03: Dry Run Program
▪ Task 04: Find and Resolve Errors
▪ Bonus Task: Add Digits
o Post-Lab Activity
▪ Task 01: Simple Calculator
• Submissions
• Evaluations Metric
• References and Additional Material
• Lab Time and Activity Simulation Log
Resources Required:
• Desktop Computer or Laptop
• Microsoft ® Visual Studio 2022
General Instructions:
• In this Lab, you are NOT allowed to discuss your solution with your colleagues, even not
allowed to ask how is s/he doing, this may result in negative marking. You can ONLY
discuss with your Teaching Assistants (TAs) or Lab Instructor.
• Your TAs will be available in the Lab for your help. Alternatively, you can send your queries
via email to one of the followings.
Course Instructors:
Program Sequence: Statements are executed one after another in programming. The order in which
these statements are executed is program sequence.
Arithmetic Operators: Operators which perform mathematical operations such as addition,
subtraction, etc.
Relational Operators: Operators which create a relationship and compares values of the two
if…else Statement: if…else statement executes two different blocks of code depending if a specific
condition is true or false.
Increment & Decrement Operators: The increment operator (++) and the decrement operator (--)
add one to and subtract one from an integer variable.
Microsoft ® Visual Studio supports 36 different programming languages and allows the code editor
and debugger to support nearly any programming language, provided a language-specific service
exists. Built-in languages include C, C++, C++/CLI, Visual Basic, .NET, C#, F#, JavaScript,
TypeScript, XML, XSLT, HTML, and CSS. Support for other languages such as Python, Ruby,
Node.js, and M among others is available via plug-ins. Java (and J#) were supported in the past.
© https://en.wikipedia.org/wiki/Microsoft_Visual_Studio
Activities:
Pre-Lab Activities:
MinGW installation:
Fig. 08 (Comments in C)
Preprocessor Directive:
“#include <stdio.h>” is a C preprocessor directive. The preprocessor handles lines beginning with #
before compilation. It tells the preprocessor to include the contents of the “standard input/output
header ()”. The contents of headers will be explained in more detail in the upcoming manuals.
The main Function:
“int main() { … }” is a part of every C program. The parentheses after main indicate that main is a
function (building block of a program). C programs must contain a main function. Execution of every
program begins at the main function. Functions can return information. The keyword “int” to the left
of main indicates that main returns an integer value.
Escape sequences:
The “backslash (\)” is an escape character in a string. It indicates that printf should do something
different. Backslash combined with the next character to form an escape sequence. Some common
escape sequences are:
Following example demonstrates the use of “\n” escape sequence. It is used in the printf statement to
display text on two lines.
A program is executed line by line in a top to bottom fashion. The execution of a program starts from
the main() function.
Arithmetic Operators:
Operation Arithmetic Operator
Addition +
Subtraction -
Multiplication *
Division /
Modulus/Remainder %
The following program demonstrates the multiplication of two numbers and print the result on the
console.
Fig. 13 (Arithmetic in C)
The “Modulus (%) Operator” returns the remainder after division. For example, if we divide 9 by 2
the remainder will be 1. Example is shown in the figure below:
Fig. 14 (Arithmetic in C)
Task 01: Adding Two Numbers [Estimated 15 minutes / 10 marks] Create a C program which:
In-Lab Activities:
Execute C program with Command Prompt:
Not equal to !=
if Statement:
The if Statement runs a block of code if a condition is true. Its syntax is “if (condition) {…}”
In the figure below if Statement is used to compare (using “==” relational operator) two integers if
they are equal or not.
• At line 11, it checks if condition, as it is false, as a is not greater than 10, so program shifts to line
15
• At line 16, it checks if condition, as it is true i.e., a is equal to 0, so “Number is less than or
equal to 10” is displayed on the console
The if…else Statement runs a specific block of code depending if a condition is true or false. Its
syntax is “if (condition) {…} else {…}”. Figure below shows an example code for if…else
Statement.
If the value of a was “5”, then else portion will be executed as all conditions becomes false.
Task 01: Find Largest Number [40 minutes / 25 marks] Write a C program which: [Hint: Use
Multiple if Statements]
89 33 56 Largest number is 89
10 33 33 Largest number is
33.
Task 02: Fill the blank area [25 minutes / 25 marks] The following program:
88 A
67 B
Task 03: Dry Run Program. [30 minutes / 30 marks] Dry Run the following program and fill
up the trace table:
1 2 0 5
23
72
-12
99
Task 04: Find and resolve the errors. [25 minutes / 20 marks] (a):
Post-Lab Activities:
Task 01: Simple Calculator [Estimated 45 minutes / 30 marks] Create a Console based calculator
which:
• Displays the menu on Console and takes an input from user as follows:
Submissions:
• For Pre-Lab Activity:
▪ Submit the .c file on Google Classroom and name it with your roll no. •
For In-Lab Activity:
▪ Save the files on your PC.
▪ TA’s will evaluate the tasks offline.
• For Post-Lab Activity:
▪ Submit the .c file on Google Classroom and name it with your roll no.
Evaluations Metric:
• All the lab tasks will be evaluated offline by TA’s
• Division of Pre-Lab marks: [20 marks] ▪ Add Two Numbers [10 marks] ▪ Arithmetic
Expression [10 marks]
• Division of In-Lab marks: [100 marks] ▪ Task 01: Find Largest Number [25 marks] ▪
Task 02: Fill in the blank area [25 marks] ▪ Task 03: Dry Run Program [30 marks] ▪ Task
04: Find and Resolve Errors [20 marks]
• Division of Post-Lab marks: [30 marks] ▪ Task 01: Simple Calculator [30 marks]
• Operators in C
https://www.programiz.com/c-programming/c-operators