Professional Documents
Culture Documents
Module 3
Module 3
1
[DATA TYPES AND VARIABLES]
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.
#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;
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;
}
#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.
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]
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