You are on page 1of 23

CSC 1401

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

• Preprocessor directives: #include, #define


• Main function: int main (void)
• Any used variable should be declared before its
usage.
5
Variable Declarations

• Variables: values stored in variables can changes


• Variables Declarations:
– What kind of information will be stored in each
variable
– How that information will be represented in memory

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

Memory(a) Before and (b) After Execution of a Program


9
Before and After Assignment of
a variable
kms = KMS_PER_MILE * miles;

10
Before and After Assignment of
a variable
sum = sum + item;

11
Function: scanf

scanf("%lf", &miles);

12
scanf

scanf("%c%c%c", &letter_1, &letter_2, &letter_3 );

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.

area = PI * radius * radius;

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)

v = (p2 - p1) / (t2 - t1);

20
Evaluation Tree (2)

z - (a + b / 2) + w * -y

21
Common Programming Errors
• Syntax Errors
• Run-Time Errors
• Logic Errors

22
Summary

• 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

23

You might also like