day3_part_i.pdf
TRANSCRIPT
![Page 1: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/1.jpg)
Lecture #3
Introduction to C Programming
Language
الرحمن الرحيم بسم هللا
![Page 2: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/2.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Intr. to C
Programmi
ng Structured
Program
Development in
C
C Program
Control
C Functions
C Arrays
Intr. to C++
Programmi
ng
Syllabus
![Page 3: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/3.jpg)
3
Intr. to C programming
![Page 4: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/4.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Lecture #3
Objectives:
In this Lecture, you’ll learn
To write simple computer programs in
C.
To use simple input and output
statements.
To use the fundamental data types.
![Page 5: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/5.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Lecture #3
Objectives:
In this Lecture, you’ll learn
To use arithmetic operators.
The precedence of arithmetic
operators.
To write simple decision making
statements..
![Page 6: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/6.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Book:
![Page 7: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/7.jpg)
7
C Language
![Page 8: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/8.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
How do programs run?
Data Processing Unit
Input Output
![Page 9: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/9.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Writing C Programs
A programmer uses a text editor to create or
modify files containing C code.
Code is also known as source code.
A file containing source code is called a source
file.
After a C source file has been created, the
programmer must invoke the C compiler before
the program can be executed (run).
![Page 10: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/10.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
A Simple C Program:
Printing a Line of Text
![Page 11: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/11.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Escape sequence
![Page 12: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/12.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Escape sequence
![Page 13: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/13.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Escape sequence
![Page 14: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/14.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Example: Adding Two Integers.
![Page 15: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/15.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Example: Adding Two Integers.
![Page 16: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/16.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Scanf ("%d", &integer1 )
Uses scanf to obtain a value from the user. The
scanf function reads from the standard input,
which is usually the keyboard.
scanf has two arguments, "%d" and &integer1.
The first argument, the format control string,
indicates the type of data that should be input by
the user.
(the letter d stands for “decimal integer”).
![Page 17: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/17.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Scanf ("%d", &integer1 )
The % in this context is treated by scanf (and printf as
we’ll see) as a special character that begins a conversion
specifier.
The second argument of scanf begins with an ampersand
(&)—called the address operator in C followed by the
variable name.
The ampersand, when combined with the variable name,
tells scanf the location (or address) in memory at which
the variable integer1 is stored.
The computer then stores the value for integer1 at that
location.
![Page 18: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/18.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Arithmetic in C
![Page 19: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/19.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Rules of Operator Precedence
![Page 20: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/20.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Rules of Operator Precedence
Example:
![Page 21: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/21.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Rules of Operator Precedence
Example:
![Page 22: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/22.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Decision Making: Equality and Relational Operators
![Page 23: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/23.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Example:
![Page 24: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/24.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Example:
![Page 25: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/25.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Example:
![Page 26: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/26.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Example: Output
![Page 27: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/27.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Examples:
Example-3: Write an algorithm in flowchart to
determine the flying time between two cities
given the distance between them and the
average speed of the airplane.
![Page 28: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/28.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Examples:
Example-4: Write an algorithm in flowchart to
convert the temperature from Celsius to
Fahrenheit.
![Page 29: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/29.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Examples:
Example-4: Write an algorithm in flowchart to
compute Salary for any employee.
S= R H - D
![Page 30: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/30.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Exercises:
Given the equation y = ax3 + 7, which of the
following, if any, are correct C statements for
this equation?
![Page 31: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/31.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Exercises:
(Arithmetic) Write a program that asks the
user to enter two numbers, obtains them
from the user and prints their sum, product,
difference, quotient and remainder.
![Page 32: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/32.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Exercises:
(Shapes with Asterisks) Write a program
that prints the following shapes with
asterisks.
![Page 33: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/33.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Exercises:
(Table of Squares and Cubes) Using only
the techniques you learned in this lect.
write a program that calculates the squares and
cubes of the numbers from 0 to 10 and uses
tabs to print the following table of values:
![Page 34: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/34.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
• Summary of major points so far
program execution begins at main()
keywords are written in lower-case
statements are terminated with a semi-colon
text strings are enclosed in double quotes
![Page 35: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/35.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef
Anatomy of a C Program
program header comment
preprocessor directives (if any)
int main ( void )
{
statement(s)
return 0 ;
}
![Page 36: Day3_Part_I.pdf](https://reader033.vdocuments.us/reader033/viewer/2022042821/55cf919f550346f57b8f0971/html5/thumbnails/36.jpg)
L3, CSC121 2014-2015 © Dr. Basheer M. Nasef