0% found this document useful (0 votes)
84 views

Slide 04 - Variables and Constants

The document discusses variables and constants in C programming. It defines variables as storage locations with identifiers that can have changing values, while constants are values that do not change. It describes the different data types for variables in C, including integer, float, character, and others. The document provides examples of declaring and assigning values to variables, and explains that variable types must match the types of values assigned. Constants can be integers, floats, characters or strings.

Uploaded by

EffendyFooad
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Slide 04 - Variables and Constants

The document discusses variables and constants in C programming. It defines variables as storage locations with identifiers that can have changing values, while constants are values that do not change. It describes the different data types for variables in C, including integer, float, character, and others. The document provides examples of declaring and assigning values to variables, and explains that variable types must match the types of values assigned. Constants can be integers, floats, characters or strings.

Uploaded by

EffendyFooad
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Variables and Constants

Knowledge:
Understand the concept of storage location representation as
identifiers

Skill:
Identify and define data types in C programs

Computer Science Department

FTSM

Lets recap

Introduction

Which one is the preprocessor


instruction?

Consider the following


example:
Which
one is the main
function?
#include <stdio.h>

void main() {
printf(Welcome to UKM\n);
}
TK1913-C Programming

C Program Structure
Preprocessor
Instruction
Global
Pengisytiharan
Declaration
globl

Still remember
this diagram?

void main (void)


{
Pengisytiharan
setempat
Local Declaration
Statement
}
TK1913-C Programming

Identifiers
Identifiers are:

Variable

Constant

Function

Others

TK1913-C Programming

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 What
are C are
standard
C
reserved
identifiers?
words?
TK1913-C Programming

Identifiers
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, return

Examples of standard identifiers:


printf, scanf
TK1913-C Programming

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;
TK1913-C Programming

Variable
Types of variable:

Character: char

Integer: int

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

Float: float

An individual character value a letter, a digit or a


symbol (e.g. A, 4, *)

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


3.1416)

High-level Float: double

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:

quantity and price_per_kg

Output:

price

Process:

price = quantity * price_per_kg

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

Variable - Value Assignment


Example:
int number1, number2;
number1 = 25;
number2 = 23;
number1 = number2;

TK1913-C Programming

number1

25
23
?

number2

23
?

12

Variable - Value Assignment


Algorithm
variable expression
Syntax
variable = expression;
Rules
Expressions type must be the same as variables type

Valid Example:
int x;
x = 12;
TK1913-C Programming

Invalid Example:
int y;
y = 5.75;
13

Variable - Value Assignment


Example:
int quantity;
float price_per_kg, price;

How does this


program work?

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

TK1913-C Programming

14

Variable - Value Assignment


Example:
int quantity;
float price_per_kg, price;

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

Variable - Value Assignment


Example:
int number1, number2;

How does this


program
segment work?

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

TK1913-C Programming

16

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)

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

If you want to excel:


revise chapter 4
read chapter 5 & 6 for next week
Otherwise, you may watch tv, sleep etc. as a
preparation for next week classes.

TK1913-C Programming

19

You might also like