08 CBCP2101 - Topic04
08 CBCP2101 - Topic04
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.
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.
The size of the memory cell can double by using the qualifier long in a variable.
See the example above.
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.
(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.
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
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
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.
(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.
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.
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.
SELF-CHECK 4.2
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.
ACTIVITY 4.2
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
Declaration Type
char Character
int Integer
float Real numbers
double Real numbers with high precision
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.
Syntax:
data_type variable_name;
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
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.
variable = value;
? ? ?
? ? ?
10 25 250
SELF-CHECK 4.4
int marks;
marks = 10;
marks = 20;
marks = 30;
3. State whether the variables below are valid. If they are not valid,
provide reasoning for this.
4. State which of the identifiers below are (a) C reserved words (b)
standard identifier, (c) other valid identifier and (d) invalid identifier.
Variables have characteristics such as name and type, and they are used to
represent a value.
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.
Marini Abu Bakar, Norleyza Jailani & Sufian Idris. (2002). Pengaturcaraan C.
Kuala Lumpur: Prentice Hall.