0% found this document useful (0 votes)
27 views12 pages

08 CBCP2101 - Topic04

NOTA COMPUTER PROG TOPIK 4

Uploaded by

nur izan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views12 pages

08 CBCP2101 - Topic04

NOTA COMPUTER PROG TOPIK 4

Uploaded by

nur izan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Topic  Data Types

4 and Variables

LEARNING OUTCOMES
By the end of this topic, you should be able to:
1. Differentiate three types of data;
2. Choose the type of data that is suitable for the variable, depending on
the type of value to be represented;
3. Differentiate between variables, reserved words and standard
identifiers;
4. Identify variables that are valid and invalid; and
5. Write syntax to declare and initialise variables.

 INTRODUCTION
In the first part of this topic, we are going to see two types of input and output
data. One is of character type, for example, your name, and another is of the
numeric type, for example, your age. Data of character type is represented by
char data type. Numeric data is represented by data types int, float or
double.

In the second part, you will learn about variables. Why are variables important?
When a composer composes a new song, he/she will give a title to the song.
Your parents spent some time choosing a name for you. Similarly, when we
write a program, we need to choose an appropriate name for the file, variables,
constants, functions, etc. This name is known as the identifier. The identifier is
the official word used for any name in a high-level programming language. In
this topic, we will go into detail about variables and how to name them.

Copyright © Open University Malaysia (OUM)


62  TOPIC 4 DATA TYPES AND VARIABLES

4.1 CELL SIZES AND QUALIFIERS


Bytes is the unit for cell sizes. Variables are assigned cell sizes or memory spaces
that are different, according to their data type. Variables of int type are given a
cell size of 2 bytes, that can keep numbers up to a maximum of 32767.

Variables of float type are assigned a memory cell size of 4 bytes and can keep
larger numbers, up to 1038.

Let us see an example of initialising variables and memory cell sizes assigned for
each declaration in Table 4.1 below.

Table 4.1: Relationship between Declaration, Variables and Total bytes

Declaration Memory Total Bytes


int age; age 2 bytes
float height; height 4 bytes
long int multiples; multiples 4 bytes

The size of the memory cell can double by using the qualifier long in a variable.
See the example above.

4.2 INTEGER DATA TYPE (int)


The int data type is to represent negative or positive numbers, for example ă
77, 0, 999, +999. The memory cell size for int types differs according to
the different computer systems. In the computer family of 80386 processors and
below, the memory cell size for int is 2 bytes. For processors like SPARC, the
cell size for int is 4 bytes.

4.3 CHARACTER DATA (char)


The char data type is used to represent characters such as letters, digits or
special symbols like „?‰.

To assign a character to a variable, we will need to put single quotation marks as


in: Â Â, next to the character, for example: ‘A’, ‘z’, ‘3’, ‘?’.

Copyright © Open University Malaysia (OUM)


TOPIC 4 DATA TYPES AND VARIABLES  63

A character can be read, printed or changed to an integer value. We can also


make character value comparisons, by using operators like equal (==) or not
equal (!=), and relational operators such as <, <=, > and >=. To understand how
character values can be compared, we need to know how each character is
represented in a computer. Each character has its own unique numeric code,
using the American Standard Code for Information Interchange, commonly
known as ASCII codes.

If you want to know more about ASCII codes, you can visit the relevant websites.
The binary representation of this code is kept in the memory cell and the values
can be known. The binary numbers can be compared by using relational
operators, just like comparing normal numbers. The character code usually used
is ASCII code. The character value range that can be represented by char type is
between -128 to 128.

4.4 REAL DATA TYPE


ANSI C, introduced by the American National Standards Institute (ANSI),
describes three types of data type that can be used for manipulating real data
types:

(a) float;
(b) double; and
(c) long double.

All the types above can keep real values such as 0.001, 2.0 and 3.14159.
The choice is made depending on which range of real values are to be
represented. Table 4.2 below shows the range and number of bytes that are
needed for each of the real type numbers.

Table 4.2: Real Data Type

Real Data Type Number of Bytes Precision Range


float 4 6 10-37 .. 1038
double 8 15 10-307 .. 10308
Long double 16 19 10-4931 .. 104932

However, on some computer systems, the implementation of double and long


double is the same for the number of bytes allocated.

Copyright © Open University Malaysia (OUM)


64  TOPIC 4 DATA TYPES AND VARIABLES

ACTIVITY 4.1

There are three types of basic data types: int, char and float.
char can contain letters, digits and symbols. Why do we still need int
and float when char can contain all types of characters? State your
opinion.

SELF-CHECK 4.1

1. How many real data types are there? List them.

2. Given the declaration below, how many bytes are assigned to


each data type?

Variable Declaration Bytes Used


float weight;
double volt1;
char letter;
int hour, minute;
long int second, length;
long double volt2;

3. State the data type that is suitable for each of the values below so
that memory space wastage does not happen:
(a) 0.000067 (d) 123.123456789
(b) # (e) -40000
(c) 30000 (f) 1000000

4.5 VARIABLE NAMING


Variables are an example of an identifier. It is used in certain program blocks. It
is initialised before it can be used. To declare a variable, we need to associate a
name and the data type to represent a particular value.

Copyright © Open University Malaysia (OUM)


TOPIC 4 DATA TYPES AND VARIABLES  65

Next, let us see how each of the characteristics above is applied in a program.

Variables are named as an identifier that is declared by the user. This name has
to be:

(a) Appropriate;
(b) Easy to understand; and
(c) A name that gives a clear meaning towards the value that it represents.

This name is used to name a memory area that will be used to keep the value of
the variable.

4.5.1 Rules to Name a Variable


The guidelines to choosing a name for a variable in the C language are as follows:

(a) Variable names can only contain letters, digits and underscores “_”;
(b) Variable names cannot start with a digit (number); and
(c) Reserved words in C cannot be used as variable names.

Here are some examples of variable names that are valid.

tot3 big_Total total BigTotal big_total

We can give variable names up to 31 characters. The 32nd character onwards will
not be taken into consideration (except for global variable names, which will not
be discussed in this module).

System identifiers usually start with the character „_‰. Here, we are not
encouraged to use the character „_‰ as the beginning for identifiers, (for example:
_total) to avoid confusion. Both lower case and capital letters can be used to
name variables. However, the C language is case sensitive, where lower case
letters are considered different from capital letters. Observe that each of the
variable names below is different in C.

total  Total  toTAL  TOtal  toTal

Copyright © Open University Malaysia (OUM)


66  TOPIC 4 DATA TYPES AND VARIABLES

The best way to name a variable, is with a name that represents the value that is
kept in the identifier. Therefore, sometimes a variable name can be made up of
two words or more. We cannot separate the words with spaces, because it is
against the first rule of naming identifiers, which states that identifiers that have
a space in between are not valid.

For identifiers that have two or more words, we can join them with a “_”
character (like big_total). Nevertheless, the usual practice is to write it as:

(a) Start with the first word being a lower case letter; and
(b) Start the second word with an upper case letter.

Therefore, the identifier above is better written as bigTotal.

SELF-CHECK 4.2

State whether the variables given are VALID or INVALID.

Variable VALID or INVALID


(a) Address2
(b) 33road
(c) _empty

4.5.2 C Reserved Words and Standard Identifiers


Reserved words are words that have special meanings in C and cannot be used
for other reasons. All reserved words come in lower case letters.

To learn more about this topic, refer to Pengaturcaraan C, Marini Abu Bakar et.
al. (2000).

Like reserved words, standard identifiers also have special meanings in C. In the
sample programs that you have seen before, the standard identifier printf is an
operational name that is declared in the stdio.h library. Standard identifiers
can be re-declared and used by the programmer for other reasons, unlike
reserved words. However, after re-declaring the standard identifier for another
reason, it cannot be used for its original use.

Copyright © Open University Malaysia (OUM)


TOPIC 4 DATA TYPES AND VARIABLES  67

ACTIVITY 4.2

So far, you have been introduced to variables, reserved words and


standard identifiers. Can you think of the differences among them?

SELF-CHECK 4.3

The following identifiers are not valid. Provide reasons as to why each
one is not valid.

Identifier Reason
(a) 2001SeaGames
(b) SEA GAMES
(c) %passes
(d) width+height
(e) double

4.6 VARIABLE TYPES


Variables can represent different types of data. Data types that are used will
determine the variable type. Look at Table 4.3 below. The basic variable types
below are usually used in C programs.

Table 4.3: Basic Variable Types

Declaration Type
char Character
int Integer
float Real numbers
double Real numbers with high precision

Copyright © Open University Malaysia (OUM)


68  TOPIC 4 DATA TYPES AND VARIABLES

When a variable has a certain data type, it means that the variable can only
represent data of that particular type. For example, a variable of type integer, C
assumes that only whole numbers can be used to represent those values.

There are also extended data types that use a qualifier. Unsigned, signed,
short, long, unsigned short, signed short, unsigned long and
signed long, are qualifiers that exist in C.

4.7 VARIABLE DECLARATIONS


Variables can be used to keep input data and the calculation of results or logic
manipulations. Values that are kept by variables can change throughout program
execution.

Syntax:

data_type variable_name;

Next are some examples of variable declarations:

Table 4.4: Variable Declarations

Data Types Variable Names


int marks1, marks2, marks3, marks4;
double averageMarks;
char grade;

When declaring variables, the compiler will be notified of four items:

(a) Variable name;


(b) Variable type;
(c) Size of cell of variable in the memory; and
(d) Variable storage class.

Different variables are used to keep different types of data. Therefore, in the
variable declaration, it has to be mentioned what data type the variable will
contain. In the example above, identifier marks1, marks2, marks3 and
marks4 are declared as type int, identifier averageMarks is of type double

Copyright © Open University Malaysia (OUM)


TOPIC 4 DATA TYPES AND VARIABLES  69

and identifier grade is of type char. Notice in the example above, few variables
of the same type can be declared in a group, by separating the names of variables
with a comma.

Cell sizes will be discussed in the next topic. However, storage classes will not be
taught in this course.

4.8 VARIABLE ASSIGNMENT


We can assign values to a variable by using the following statement:

variable = value;

Consider the next example:

/* 1 */ int number1, number2, multipliedNumber;


/* 2 */ number1 = 10;
/* 3 */ number2 = 25;
/* 4 */ multipliedNumber = number1 * number2;

When statement /* 1 */ is declared, the memory cell assigned to each


variable is:

? ? ?

number1 number2 multipliedNumber

Statement /* 2 */ assigns 10 to variable number1. and

Statement /* 3 */ assigns 25 to variable number2.

? ? ?

number1 number2 multipliedNumber

Copyright © Open University Malaysia (OUM)


70  TOPIC 4 DATA TYPES AND VARIABLES

Next, statement /* 4 */ is executed. When assignment statement


multipliedNumber = number1 * number2; is executed, the right
side of „=„ will be calculated first, which involves performing multiplication
on values kept by variables number1 and number2. The result is 250 and
this value will be assignment to the variable multipliedNumber that is on
the left of „=„. Next is the result of the execution of statement /* 4 */.

10 25 250

number1 number2 multipliedNumber

SELF-CHECK 4.4

1. Given a program code segment below, what is the value of the


variable marks after all the statements have been executed?

int marks;
marks = 10;
marks = 20;
marks = 30;

2. Sketch the memory cell of the program code segment below.

int num, newValue


num = 9;
newValue = num;
newValue = -num;

3. State whether the variables below are valid. If they are not valid,
provide reasoning for this.

(a) tot_Credit_Hours (c) current_bill

(b) _car_tyres_types (d) 8_8_88

Copyright © Open University Malaysia (OUM)


TOPIC 4 DATA TYPES AND VARIABLES  71

4. State which of the identifiers below are (a) C reserved words (b)
standard identifier, (c) other valid identifier and (d) invalid identifier.

(a) void (d) printf

(b) MAXIMUM_BIL (e) xyz123

(c) double (f) char

 Variables are used to keep data temporarily in memory.

 Variables have characteristics such as name and type, and they are used to
represent a value.

 Variables are named as an identifier that is declared by the user.

 Variables can represent different types of data. There are three types of basic
data types: Integer Data Type (int) - which is to represent negative or
positive numbers, Character Data (char) - which is used to represent
characters such as letters, digits or special symbols, and Real Data ă which
has three types of data type that can be used for manipulating real data
numbers.

 Variables can be used to keep input data and the calculation results or logic
manipulations.

C reserved words Syntax


Identifier Variables
Standard identifiers

Copyright © Open University Malaysia (OUM)


72  TOPIC 4 DATA TYPES AND VARIABLES

Marini Abu Bakar, Norleyza Jailani & Sufian Idris. (2002). Pengaturcaraan C.
Kuala Lumpur: Prentice Hall.

Copyright © Open University Malaysia (OUM)

You might also like