Slide 04 - Variables and Constants
Slide 04 - Variables and Constants
Knowledge:
Understand the concept of storage location representation as
identifiers
Skill:
Identify and define data types in C programs
FTSM
Lets recap
Introduction
void main() {
printf(Welcome to UKM\n);
}
TK1913-C Programming
C Program Structure
Preprocessor
Instruction
Global
Pengisytiharan
Declaration
globl
Still remember
this diagram?
Identifiers
Identifiers are:
Variable
Constant
Function
Others
TK1913-C Programming
Identifiers
Syntax Rules:
Identifiers
C Reserved Word
Variable
A
Needs
to be declared:
variable_type variable_name;
Example: int x;
int entry_time, charge;
TK1913-C Programming
Variable
Types of variable:
Character: char
Integer: int
Float: float
TK1913-C Programming
Variable
Variable Declaration:
Example 2:
1:
char letter;
float
matric;
letter is isa character-type
matric
a ??? variable variable
TK1913-C Programming
Variable
Example:
Calculate and display the price of a number of
apples if the quantity in kg and price per kg are
given.
Input:
Output:
price
Process:
TK1913-C Programming
10
Variable
Example:
int quantity;
Why did we
declare it as int?
float price_per_kg;
float price;
TK1913-C Programming
Why did we
declare them as
float?
11
TK1913-C Programming
number1
25
23
?
number2
23
?
12
Valid Example:
int x;
x = 12;
TK1913-C Programming
Invalid Example:
int y;
y = 5.75;
13
quantity = 5;
price_per_kg = 4.50;
price = quantity * price_per_kg;
TK1913-C Programming
14
quantity
price_per_kg 4.50
?
quantity = 2;
price_per_kg = 4.50;
price = quantity * price_per_kg;
TK1913-C Programming
?
2
price
9.00
?
15
number1 = 25;
number2 = 23;
number1 = number2;
TK1913-C Programming
16
Constant
Consists of:
TK1913-C Programming
17
Exercise
To practice what youve
learned, try exercises on
page 83 (Latih Diri)
from Pengaturcaraan C
TK1913-C Programming
18
End of Lecture 4
Whats next? INPUT AND
OUTPUT on the way
TK1913-C Programming
19