36
Lecture #3 Introduction to C Programming Language بسمن الرحيم الرحم

Day3_Part_I.pdf

Embed Size (px)

Citation preview

Page 1: Day3_Part_I.pdf

Lecture #3

Introduction to C Programming

Language

الرحمن الرحيم بسم هللا

Page 2: Day3_Part_I.pdf

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

3

Intr. to C programming

Page 4: Day3_Part_I.pdf

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

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

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Book:

Page 7: Day3_Part_I.pdf

7

C Language

Page 8: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

How do programs run?

Data Processing Unit

Input Output

Page 9: Day3_Part_I.pdf

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

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

A Simple C Program:

Printing a Line of Text

Page 11: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Escape sequence

Page 12: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Escape sequence

Page 13: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Escape sequence

Page 14: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Example: Adding Two Integers.

Page 15: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Example: Adding Two Integers.

Page 16: Day3_Part_I.pdf

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

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

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Arithmetic in C

Page 19: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Rules of Operator Precedence

Page 20: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Rules of Operator Precedence

Example:

Page 21: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Rules of Operator Precedence

Example:

Page 22: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Decision Making: Equality and Relational Operators

Page 23: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Example:

Page 24: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Example:

Page 25: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Example:

Page 26: Day3_Part_I.pdf

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef

Example: Output

Page 27: Day3_Part_I.pdf

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

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

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

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

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

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

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

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

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

L3, CSC121 2014-2015 © Dr. Basheer M. Nasef