Upload
effendyfooad
View
236
Download
0
Embed Size (px)
Citation preview
8/10/2019 Slide 04 - Variables and Constants
1/19
Computer Science Department FTSM
Variables and Constants
Knowledge:
Understand the concept of storage location representation asidentifiers
Skill:
Identify and define data types in C programs
8/10/2019 Slide 04 - Variables and Constants
2/19
TK1913-C Programming 2
IntroductionConsider the following example:
#include
void main() {
printf(Welcome to UKM\n);
}
Lets recap
Which one is the preprocessor
instruction?
Which one is the main
function?
8/10/2019 Slide 04 - Variables and Constants
3/19
TK1913-C Programming 3
C Program Structure
Preprocessor
Instruction
Pengisytiharan globl
void main (void)
{
}
Pengisytiharan setempat
Statement
Global Declaration
Local Declaration
Still rememberthis diagram?
8/10/2019 Slide 04 - Variables and Constants
4/19
TK1913-C Programming 4
Identifiers
Identifiers are:
Variable
Constant
Function
Others
8/10/2019 Slide 04 - Variables and Constants
5/19
TK1913-C Programming 5
Identifiers
Syntax Rules:
Consist of only letters, digits and underscores
Cannot begin with a digit
Cannot use C reserved words
Try not to use/redefine C standard identifiers
What are C
reserved words?
What are C standard
identifiers?
8/10/2019 Slide 04 - Variables and Constants
6/19
TK1913-C Programming 6
C Reserved Word
A word that has special meaning in C
C Standard Identifier
A word having special meaning but may beredefined (but is not recommended!!)
Examples of reserved word:
int, void, double, returnExamples of standard identifiers:
printf, scanf
Identifiers
8/10/2019 Slide 04 - Variables and Constants
7/19
TK1913-C Programming 7
Variable
A name associated with a memory
cell whose value can change
Needs to be declared:
variable_type variable_name;
Example:int x;
int entry_time, charge;
8/10/2019 Slide 04 - Variables and Constants
8/19
TK1913-C Programming 8
VariableTypes of variable:
Character: char
An individual character valuea letter, a digit or a
symbol (e.g. A, 4, *) Integer: int
Whole numbers (e.g. +16, 568, -456)
Float: float
A real number which has a decimal point (e.g. 8.00,
3.1416)
High-level Float: double
8/10/2019 Slide 04 - Variables and Constants
9/19
TK1913-C Programming 9
Variable
Variable Declaration:
Example 1:
char letter;
letteris a character-type variable
Example 2:
float matric;
matricis a ??? variable
8/10/2019 Slide 04 - Variables and Constants
10/19
TK1913-C Programming 10
Variable
Example:
Calculate and display the price of a number of
apples if the quantity in kg and price per kg aregiven.
Input: quantityand price_per_kg
Output: price
Process: price= quantity* price_per_kg
8/10/2019 Slide 04 - Variables and Constants
11/19
TK1913-C Programming 11
Variable
Example:
int quantity;
float price_per_kg;
float price;
Why did we
declare it as int?
Why did wedeclare them as
float?
8/10/2019 Slide 04 - Variables and Constants
12/19
TK1913-C Programming 12
Example:
int number1, number2;
number1 = 25;
number2 = 23;
number1 = number2;
Variable - Value Assignment
number1 ?
number2 ?
2523
23
8/10/2019 Slide 04 - Variables and Constants
13/19
TK1913-C Programming 13
Variable - Value Assignment
Algorithm
variable expression
Syntax
variable = expression;
Rules
Expressions type must be the same as variables type
Valid Example: Invalid Example:
int x; int y;
x = 12; y = 5.75;
8/10/2019 Slide 04 - Variables and Constants
14/19
TK1913-C Programming 14
Variable - Value Assignment
Example:
int quantity;
float price_per_kg, price;
quantity = 5;
price_per_kg = 4.50;price = quantity * price_per_kg;
How does this
program work?
8/10/2019 Slide 04 - Variables and Constants
15/19
TK1913-C Programming 15
Example:
int quantity;
float price_per_kg, price;
quantity = 2;
price_per_kg = 4.50;price = quantity * price_per_kg;
Variable - Value Assignment
quantity ?
price_per_kg ?
price ?
4.50
9.00
2
8/10/2019 Slide 04 - Variables and Constants
16/19
TK1913-C Programming 16
Variable - Value Assignment
Example:
int number1, number2;
number1 = 25;
number2 = 23;
number1 = number2;
How does this
program
segment work?
8/10/2019 Slide 04 - Variables and Constants
17/19
TK1913-C Programming 17
Constant
A value that will not change
Consists of:
Float(e.g. 2.3 2.73F 1.2e-5)
Integer (e.g. 3 67 -2)
Character(e.g. z 3 $ \n) String (e.g. UKM 1 5a)
8/10/2019 Slide 04 - Variables and Constants
18/19
TK1913-C Programming 18
ExerciseTo practice what youve
learned, try exercises on
page 83 (Latih Diri)
from Pengaturcaraan C
8/10/2019 Slide 04 - Variables and Constants
19/19
TK1913-C Programming 19
End of Lecture 4Whats next?INPUT ANDOUTPUTon the way
If you want to excel:revise chapter 4
read chapter 5 & 6 for next weekOtherwise, you may watch tv, sleep etc. as apreparation for next week classes.