computer science department ftsm variables and constants knowledge: understand the concept of...

19
Computer Science Departme nt FTSM FTSM Variables and Variables and Constants Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify and define data types in C programs

Upload: noreen-lambert

Post on 18-Dec-2015

214 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

Computer Science Department FTSMFTSM

Variables and ConstantsVariables and Constants

Knowledge:Understand the concept of storage location representation as identifiers

Skill:Identify and define data types in C programs

Page 2: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 22

IntroductionIntroductionConsider the following example:

#include <stdio.h>

void main() {

printf(“Welcome to UKM\n”);

}

#include <stdio.h>

void main() {

printf(“Welcome to UKM\n”);

}

Let’s recap…Let’s recap…

Which one is the preprocessor Which one is the preprocessor instruction?instruction?

Which one is the main Which one is the main function?function?

Page 3: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 33

C Program StructureC Program StructurePreprocessor Instruction

Pengisytiharan globl

void main (void){

}

Pengisytiharan setempat

Statement

Global Declaration

Local Declaration

Still remember Still remember this diagram?this diagram?

Page 4: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 44

IdentifiersIdentifiers

Identifiers are:

Variable

Constant

Function

Others

Page 5: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 55

Identifiers 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 identifiersWhat are C What are C

reserved words?reserved words?What are C standard What are C standard

identifiers?identifiers?

Page 6: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 66

C Reserved Word A word that has special meaning in C

C Standard Identifier A word having special meaning but may be

redefined (but is not recommended!!)

Examples of reserved word:int, void, double, returnint, void, double, return

Examples of standard identifiers:printf, scanfprintf, scanf

Examples of reserved word:int, void, double, returnint, void, double, return

Examples of standard identifiers:printf, scanfprintf, scanf

Identifiers Identifiers

Page 7: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 77

Variable Variable A name associated with a memory

cell whose value can change

Needs to be declared:

variable_type variable_name;variable_type variable_name;

Example: int x;int x; int entry_time, charge;int entry_time, charge;

Example: int x;int x; int entry_time, charge;int entry_time, charge;

Page 8: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 88

VariableVariableTypes of variable: Character: charchar

An individual character value – a letter, a digit or a symbol (e.g. ‘A’, ‘4’, ‘*’)

Integer: intint Whole numbers (e.g. +16, 568, -456)

Float: floatfloat A real number which has a decimal point (e.g. 8.00,

3.1416)

High-level Float: doubledouble

Page 9: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 99

VariableVariable

Variable Declaration:

Example 1:

char letter;char letter;

letterletter is a character-type variable

Example 1:

char letter;char letter;

letterletter is a character-type variable

Example 2:

float matric;float matric;

matricmatric is a ??? variable

Example 2:

float matric;float matric;

matricmatric is a ??? variable

Page 10: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 1010

VariableVariable

Example:Calculate and display the price of a number of apples if the quantity in kg and price per kg are given.

•Input: quantityquantity and price_per_kgprice_per_kg

•Output: priceprice

•Process: priceprice = quantityquantity * price_per_kgprice_per_kg

Example:Calculate and display the price of a number of apples if the quantity in kg and price per kg are given.

•Input: quantityquantity and price_per_kgprice_per_kg

•Output: priceprice

•Process: priceprice = quantityquantity * price_per_kgprice_per_kg

Page 11: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 1111

VariableVariable

Example:

int quantity;int quantity;

float price_per_kg;float price_per_kg;

float price;float price;

Example:

int quantity;int quantity;

float price_per_kg;float price_per_kg;

float price;float price;

Why did we Why did we declare it as declare it as intint??

Why did we Why did we declare them as declare them as

floatfloat??

Page 12: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 1212

Example:int number1, number2;

number1 = 25;number2 = 23;number1 = number2;……

Example:int number1, number2;

number1 = 25;number2 = 23;number1 = number2;……

Variable - Value AssignmentVariable - Value Assignment

number1 ?

number2 ?

2523

23

Page 13: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 1313

Variable - Value AssignmentVariable - Value AssignmentAlgorithm

variable variable expression expression Syntax

variable = expression;variable = expression;

RulesExpression’s type must be the same as variable’s type

Valid Example: Invalid Example:int x; int x; int y;int y;x = 12;x = 12; y = 5.75;y = 5.75;

Valid Example: Invalid Example:int x; int x; int y;int y;x = 12;x = 12; y = 5.75;y = 5.75;

Page 14: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 1414

Variable - Value AssignmentVariable - Value Assignment

Example:int quantity;float price_per_kg, price;

quantity = 5;price_per_kg = 4.50;price = quantity * price_per_kg;…

Example:int quantity;float price_per_kg, price;

quantity = 5;price_per_kg = 4.50;price = quantity * price_per_kg;…

How does this How does this program work?program work?

Page 15: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 1515

Example:int quantity;float price_per_kg, price;

quantity = 2;price_per_kg = 4.50;price = quantity * price_per_kg;…

Example:int quantity;float price_per_kg, price;

quantity = 2;price_per_kg = 4.50;price = quantity * price_per_kg;…

Variable - Value AssignmentVariable - Value Assignment

quantity ?

price_per_kg ?

price ?

4.50

9.00

2

Page 16: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 1616

Variable - Value AssignmentVariable - Value Assignment

Example:int number1, number2;

number1 = 25;number2 = 23;number1 = number2;……

Example:int number1, number2;

number1 = 25;number2 = 23;number1 = number2;……

How does this How does this program program

segment work?segment work?

Page 17: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 1717

ConstantConstant

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

Page 18: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 1818

ExerciseExerciseTo practice what you’ve learned, try exercises on page 83 (Latih Diri) from Pengaturcaraan C

Page 19: Computer Science Department FTSM Variables and Constants Knowledge: Understand the concept of storage location representation as identifiers Skill: Identify

TK1913-C ProgrammingTK1913-C Programming 1919

End of Lecture 4End of Lecture 4

What’s next?What’s next? …INPUT …INPUT AND OUTPUTAND OUTPUT on the way on the way … …

If you want to excel:If you want to excel:• revise chapter 4revise chapter 4• read chapter 5 & 6 for next weekread chapter 5 & 6 for next week… … Otherwise, you may watch tv, sleep etc. as a Otherwise, you may watch tv, sleep etc. as a preparation for next week classes.preparation for next week classes.