Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

DPT 6102 COMPUTER PROGRAMMING 1

1
[DATA TYPES AND VARIABLES]

Module 3<Week 3>< [DATA TYPES AND


VARIABLES >

Course Learning Outcomes:


1. To be able to learn the different types of data and their importance.
2. To be able to create variables, store data in variable, and access data in a
variable.

< DPT 6102 COMPUTER PROGRAMMING 1 – DATA TYPES >


Let's talk about a very simple but very important concept that is available in nearly all the
programming languages called data types. A data form, as its name suggests, describes a
type of data that you can process through your computer program. This can be binary,
alphanumeric, decimal and so on.
Let's leave computer programming aside for a while and take an simple example of adding
two whole numbers 10 & 20 that can be done simply as follows − 10 + 20. Let's take
another question where we want to add two decimal numbers 10.50 & 20.50, which can be
written as follows − 10.50 + 20.50. All examples are straightforward. Now let's take
another example where we want student details registered
Name: Jean Eyre; Class: 5th; Section: A; Age: 16; Sex: F
The first example deals with whole numbers, the second example introduced two decimal
numbers, while the third example presents a combination of different statistics. Let's put it
this way − Student name " Jean Eyre" is a series of characters often called a string.
Student class "5th" was represented by a mix of entire numbers and a two-character string.
That kind of mix is called alphanumeric.
A single character that is 'A' was depicted in the student segment. A whole number which is
16 has reflected student age. A single character representing student sex is 'F.'
Likewise, when we write a computer program for processing different data types, we need
to explicitly define its type; otherwise the machine does not understand how different
operations can be performed on that data. Different programming languages use various
keywords to describe the different types of data. For example, programming languages C
and Java use int to specify integer data, while char specifies a character data type.

Course Module
< C and Java Data Types>
Type Keyword Value range which can be represented by this data type
Character char -128 to 127 or 0 to 255
Number int -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
Small Number short -32,768 to 32,767
Long Number long -2,147,483,648 to 2,147,483,647
Decimal Number float 1.2E-38 to 3.4E+38 till 6 decimal places

< Variables>
Variables are the names you assign to the positions in computer memory which are used to store
values in a computer program.
For example, assume you want to store two 10 and 20 values in your program, and use these two
values at a later point. Let's see how they're going to do it. Below are three basic steps below –
o Build variables with correct names.
o In those two variables, store your values.
o Get the stored values from the variables, and use them.

< Creating variables>


In programming C, the development of variables is often called declaring variables. Different
programming languages have different ways to construct variables within a program.
For example, C programming has the following simple way of creating variables −

#include <stdio.h>
int main() {
int a;
int b;
}
The system above generates two variables to assign two positions of memory with names a and b.
These variables were generated using int keyword to define the form of variable data which
means we want to store integer values in these two variables. Similarly, you can build variables for
storing long, float, char or any other form of data. For examples-−
/* variable to store long value */
long a;

/* variable to store float value */


float b;
DPT 6102 COMPUTER PROGRAMMING 1
3
[DATA TYPES AND VARIABLES]

You can create variables of similar type by putting them in a single line but separated by comma as
follows −
#include <stdio.h>
int main() {
int a, b;
}

< Store Values in Variables>


You have seen how we created variables in the previous section. Now, let's store some values in
those variables −

#include <stdio.h>
int main() {
int a;
int b;
a = 10;
b = 20;
}
The system above has two additional statements where we store 10 in variable a, and 20 in
variable b. Nearly all the programming languages have a common way to store values in variable
where we keep the variable name on the left side of an equal sign = and whatever value we want
to store in the variable, we keep that value on the right side.
Now, we have completed two steps, first we created two variables and then we stored required
values in those variables. Now variable a has value 10 and variable b has value 20. In other words
we can say, when above program is executed, the memory location named a will hold 10 and
memory location b will hold 20.

< Access stored values in variables>


When we do not use the stored values in the variables, then it is useless to construct variables and
store values in them. We realize there are two variables a and b in the above program, and they
store the values 10 and 20 respectively. So let's try to print the stored values in those two
variables. A C program follows, which prints the values contained in its variables −

Course Module
#include <stdio.h>
int main() {
int a;
int b;
a = 10;
b = 20;
printf( "Value of a = %d\n", a );
printf( "Value of b = %d\n", b );
}
Output:
Value of a = 10
Value of b = 20

In the previous chapter you must have seen printf() function where we used it to print "Hello,
World!". This time, we use it to print variable values. We make use of percent d, which will be
replaced in printf() statements by the values of the specified variable. We can print both values
using a single statement in printf() as follows −

#include <stdio.h>
int main() {
int a;
int b;
a = 10;
b = 20;
printf( "Value of a = %d and value of b = %d\n", a, b );
}
Output:
Value of a = 10 and value of b = 20
When you want to use float variable in C programming, use %f instead of %d and if you want to
print a character value, use %c. Similarly, the various forms of data may be written using different
% and characters.
DPT 6102 COMPUTER PROGRAMMING 1
5
[DATA TYPES AND VARIABLES]

References and Supplementary Materials


Books and Journals
1. <Rachel Rivera, Eileen Choe, Ray Toal, Alexander Schneider>; <2016>; <Programming
Language Explorations>; <New York, USA>; <A Chapman & Hall Book>

2. <Cory Althoff>; <2017>; <The Self-Taught Programmer: The Definitive Guide to


Programming Professionally>; <USA>; <Self-Taught Media>

Online Supplementary Reading Materials


1. <Complete title of online reading material>; <Complete URL of online reading
material>; <Date of access to online reading material>
(Online supplementary reading materials should come from sources that are not
within the domain of “.com”)

Online Instructional Videos


1. <Brief description of video>; <Complete URL of online video>; <Date of access to
online video>
(Online instructional videos should come from sources that are not within the domain
of “.com”. In cases when videos are only available within the “.com” domain, it is
expected that 2 back-up links will be provided.)

Note:
 There should only be one file for module per week.
 Avoid plagiarism by paraphrasing information and texts coming from the Internet and
other sources.

Course Module

You might also like