You are on page 1of 36

Lecture #3

Introduction to C Programming
Language

Intr. to C
Programmi

Intr. to C++

ng

Programmi

Structured
Program

Development in

ng

Syllabus
C Program
C Arrays

Control
C Functions

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Intr. to C programming
3

Objectives:
Lecture #3

In this Lecture, youll learn


To write simple computer programs in
C.
To use simple input and output
statements.
To use the fundamental data types.

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Objectives:
Lecture #3

In this Lecture, youll learn


To use arithmetic operators.
The precedence of arithmetic
operators.
To write simple decision making
statements..

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Book:

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

C Language
7

How do programs run?

Input

Output

Data Processing Unit

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).

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

A Simple C Program:
Printing a Line of Text

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Escape sequence

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Escape sequence

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Escape sequence

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Example: Adding Two Integers.

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Example: Adding Two Integers.

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).
L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Scanf ("%d", &integer1 )


The % in this context is treated by scanf (and printf as
well 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.
L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Arithmetic in C

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Rules of Operator Precedence

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Rules of Operator Precedence


Example:

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Rules of Operator Precedence


Example:

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Decision Making: Equality and Relational Operators

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Example:

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Example:

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Example:

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Example: Output

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.

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Examples:
Example-4: Write an algorithm in flowchart to

convert the
Fahrenheit.

L3, CSC121 2014-2015

temperature

from

Celsius

to

Dr. Basheer M. Nasef

Examples:
Example-4: Write an algorithm in flowchart to

compute Salary for any employee.

S= R H - D

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?

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.

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

Exercises:
(Shapes with Asterisks) Write a program
that prints the following shapes with
asterisks.

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:

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

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 ;
}
L3, CSC121 2014-2015

Dr. Basheer M. Nasef

L3, CSC121 2014-2015

Dr. Basheer M. Nasef

You might also like