VARIABLES AND DATA
TYPES
DR. THERESA-SAMUELLE ADJAIDOO
2
“ Sometimes it's better to leave
something alone, to pause, and
that's very true of programming.
JOYCE WHEELER ”
C PROGRAMMING - VARIABLES AND DATA TYPES
3
• VARIABLES
OUTLINE • DATA TYPES
C PROGRAMMING - VARIABLES AND DATA TYPES
4
VARIABLES
u In programming, a variable is a container (storage
area) to hold data.
u To indicate the storage area, each variable should be
given a unique name (identifier).
u A variable can be simply described as a named
(storage) space in (the computer’s) memory.
C PROGRAMMING - VARIABLES AND DATA TYPES
5
VARIABLES
u Variable names are just the symbolic representation of
a memory location.
u example: playerScore = 95;
u The value of a variable can be changed, hence the
name 'variable’.
u example: playerScore = 60;
C PROGRAMMING - VARIABLES AND DATA TYPES
6
VARIABLES
u Variables ought to be declared before they are used to
store data.
u To declare a variable simply means to state its Data
Type
u More on Data Types would be covered in the next
session
C PROGRAMMING - VARIABLES AND DATA TYPES
7
DATA TYPES
u Variables need to be declared before they are used.
u Such declarations are done simply by stating the Data
Type of the Variable.
u These Data Types simply refer to the type and size of
data associated with variables.
C PROGRAMMING - VARIABLES AND DATA TYPES
8
DATA TYPES
u In C, there are 4 basic/fundamental categories of data
types.
u These data types are represented using these four
keywords:
1. int => Integers
2. char => Characters
3. float => Floating Points (decimals)
4. double => Floating Points (decimals)
C PROGRAMMING - VARIABLES AND DATA TYPES
9
int - INTEGER DATA TYPES
u Integers are whole numbers that can have both positive and negative
values but no decimal values. Example: 0, -5, 10
u In C programming, the keyword int is used for declaring integer variables.
For example: int age;
u By declaring the variable age as having a data type of int means that
age would be used to store only integers.
u As such the compiler would reserve just the right amount of memory
space for storing an integer.
C PROGRAMMING - VARIABLES AND DATA TYPES
10
int - INTEGER DATA TYPES
u After declaring the data type of age, one can then go
ahead and use it for storing a value. For example:
int age;
age = 24;
u Or it could have been written simply as:
int age = 24;
C PROGRAMMING - VARIABLES AND DATA TYPES
11
char - CHARACTER DATA TYPES
u Characters data types are any one thing (key) that
can be found on the computer’s keyboard in addition
with other special symbols.
u Variables that store one of such characters are
declared with the keyword char . For example: char
choice = ‘y’;
u Characters in C are always enclosed in single quotation
marks i.e. ‘ ’
u By declaring a variable as char the compiler reserves
just the right amount of space for holding it in memory
C PROGRAMMING - VARIABLES AND DATA TYPES
12
float / double – FLOATING POINT DATA
TYPES
u Floating type variables can hold real numbers such as:
2.34, -6.8
u You can declare a floating point variable in C by using
either float or double keyword. For example:
float accountBalance = 3234.23;
double bookPrice = 23.99;
u In C, floating values can be represented in exponential
form as well. For example:
float normalizationFactor = 22.442e2;
C PROGRAMMING - VARIABLES AND DATA TYPES
13
DIFFERENCE BETWEEN FLOAT AND
DOUBLE
u The storage size of double (double precision float data
type) is usually twice or more the storage size of a
float (single precision float data type).
u Also a float variables has a precision of 6 digits whereas
the precision of double is 14 digits.
C PROGRAMMING - VARIABLES AND DATA TYPES
14
DECLARING MULTIPLE VARIABLES
u In C, multiple variables of a particular data type can be
declared and even assigned in one expression
statement. The following are some examples:
u int age, sum, result;
u float weight = 49.36, cwa =73.98;
u double balance, interest = 14.3856312;
C PROGRAMMING - VARIABLES AND DATA TYPES
15
DATA TYPE QUALIFIERS/MODIFIERS
u Data Type Qualifiers alter the meaning of base data
types to yield a new data type.
u There are two main categories of Data Type Qualifiers.
They are:
1. Size Qualifiers (short and long)
2. Sign Qualifiers (signed and unsigned)
C PROGRAMMING - VARIABLES AND DATA TYPES
16
SIZE QUALIFIERS/MODIFIERS
u short and long, when used may have an effect on the
storage space reserved for a particular data type.
u short hardly has any effect on the size. However, long
often increases the natural storage space requirement
for the data type it is used on.
u For example: long int count; would increase the storage
space allocated for the integer variable count
C PROGRAMMING - VARIABLES AND DATA TYPES
17
SIGN QUALIFIERS/MODIFIERS
u signed and unsigned, when used may have an effect
on the capability of a variable to store both positive
and negative values.
u When signed is used the variable is capable of storing
both positive and negative values.
u When unsigned is used the variable can only hold
positive values.
u It is important to note that, sign qualifiers can be
applied to int and char types only.
C PROGRAMMING - VARIABLES AND DATA TYPES
18
DATA TYPE QUALIFIERS/MODIFIERS
u Having understood that the two size qualifiers are a
direct opposite of each other, it would not be wise then
to use them at the same time in any declaration.
u The same applies to the two sign modifiers. However,
one of each category can be combined in a variable
declaration. For example: long unsigned int age = 25;
u It is not mandatory to use any of these qualifiers when
declaring a variable’s data type.
C PROGRAMMING - VARIABLES AND DATA TYPES
19
THE END
Any Questions?
Contact: tsadjaidoo@knust.edu.gh or thesma2006@gmail.com
C PROGRAMMING - VARIABLES AND DATA TYPES