Professional Documents
Culture Documents
Computer Programming I
Hamid Harroud
School of Science and Engineering,
Al Akhawayn University in Ifrane
h.harroud@aui.ma
Spring 2015
Lecture 3
Overview of C
Objectives
• Structure of simple C programs
• Simple data types int, double, and char.
• Input/output functions printf and scanf.
• Preprocessor directives #include and #define
• Comments
• Assignment statement along with the basic
arithmetic operators
• Case Study
• Common Errors
3
C Language Elements
Preprocessor
directives
Main function
4
General Form of a C program
SYNTAX: EXAMPLES:
int variable_list; int count,large;
double variable_list; double x, y, z;
char variable_list; char first_initial;
char ans;
6
Data Types
• int
– Range : -32767 ~ + 32767 --- in ANSI C
– -10500 435 +15 -25 32767
• double
• char
– ‘A’ ‘a’ ‘z’ ‘2’ ‘9’ ‘*’ ‘?’
7
Executable Statements
• Program in Memory
• Assignment Statements
• Input/Output Functions
– C function: printf
– C function: scanf
• Arithmetic Expressions
8
Assignment Statements
10
Before and After Assignment of
a variable
sum = sum + item;
11
Function: scanf
scanf("%lf", &miles);
12
scanf
13
Function: printf
14
Arithmetic Expressions
• Arithmetic Operators
• Mixed-Type Assignment Statement
• Expression with Multiple Operators
15
Arithmetic Operators
16
Execution of Multiple Operators
• Can be expressed as an Evaluation Tree.
17
Step by Step Expression
Evaluation
18
Operator Precedence
• An operator’s precedence determines
its order of evaluation.
- x – y * z
z-(a + b/2)+ w * -y
19
Evaluation Tree (1)
20
Evaluation Tree (2)
z - (a + b / 2) + w * -y
21
Common Programming Errors
• Syntax Errors
• Run-Time Errors
• Logic Errors
22
Summary
23