CS101 Lecture 04 - Fundamentals of Programming
CS101 Lecture 04 - Fundamentals of Programming
PROGRAMMING
Lecture 4 (online)
Introduction to Computers & Programming
Week 11 May – 15 May 2020
Zoubeir Aungnoo
Lecture Aims
■ Consider:
■ Output is??
Structure of a C Program
■ Comments are like helping text in your C program and they are
ignored by the compiler. They start with /* and terminate with
the characters */ or // as shown below −
■ Code:
■ Output:
A C Program – E.g.#4
■ Let’s write a program to add two integer (whole) number and display the
sum.
■ printf("%d \n", sum);
– %d is a placeholder. Whenever we want to display an integer in the
printf(), we write %d
■ Output:
A C Program – E.g.#5
■ Let’s write an improved version of a program to add two integer (whole)
number and display the sum.
■ Write the Pseudocode for it
– Begin
– DISPLAY “Enter Number 1: “
– READ number1
– DISPLAY “Enter Number 2: “
– READ number2
– CALCULATE sum = number1 + number2
– DISPLAY sum
– End
A C Program – E.g.#5
■ The corresponding program in C:
A C Program – E.g.#5
■ The output:
■ scanf() is the tool we use for a user to input data into a program
■ scanf("%d", &number1);
■ scanf() needs two arguments (options) to work:
1. placeholder – indicates the type of input
– E.g. %d indicates the input data to be a decimal (integer)
– E.g. %c indicates the input data to be a single character
2. Container – indicates where to store the input
– E.g. &number1 means to store the input value into the variable
(container) called number1.
– But how do we find out where this variable is in memory??
– The ampersand (&) gives the address of the specified variable