Wednesday, 29 August 2012

Chapter 3 - Variables and Introduction to Datatypes

In the previous chapter we learned how to format and print our output. Now that we are familiar with the printf() function we will move a step ahead. Those who have not mastered the printf() function please refer the previous chapter before continuing.

So what is a variable?

Consider the following sentence: There are 40 students in my class.
From this sentence we can say that there are exactly 40 students in the class.

Now consider this statement: There are 'x' students in the class.
The value of  'x' here can be any whole number(1,2,3... etc).

We use variables in our programs to store data and the values of these variables can be changed at any time.

Consider the following table. For different values of x we can print the same sentence, with the changing values.

X = 2
There are 2 students in the class
X = 5
There are 5 students in the class
X = 15
There are 15 students in the class
X = 30
There are 30 students in the class

How are Data Types related with Variables?

As x is a variable it can also store names, decimals and integers right.
Consider the following 3 sentences:

My name is Ryan
X = “Ryan”
I scored 65.5 % in the Exam
X =”65.5”
There were 15 passengers in the bus.
X = “15”

But another variation can be 

My name is 15
X = “15”
I scored Ryan % in the Exam
X = “Ryan”
There were 65.5 passengers in the bus.
X = ”65.5”

This has no meaning. Hence the data stored in the variable must be of a particular type. Therefore every time we create a variable we define the type of data that can be stored in the variable.
Hence for the above example the data types will be:

My name is Ryan
X = “Ryan”
 X is a Integer
I scored 65.5 % in the Exam
X =”65.5”
 X is a Float
There were 15 passengers in the bus.
X = “15”
 X is a String

Dont worry about the names, you will get used to them. We need to understand what each type means. Examples will clear all your doubts.

Data Types supported by C:
(Don't get scared by looking at the list and the numbers, it will all make sense afterwards)

They first column stands for the Data Type. The second column stands for the meaning and the third one for the range of values. The basic types are int , float and char , the remaining are rarely used and can be dealt with later on.

But why do all the Data Types have a range ?

As we know variables store information, and all the information stored in the computer requires memory. But we also know that our computer has limited memory. As the size of information increases  more memory is required. So we need to put a restriction on the size of information/data stored in the variable. Hence every data type has a fixed size and fixed range.

Remember, you need to Treat Every New Concept as an Opportunity to Improve and not as an Hurdle

1) Integer Data Type

Example: 1, 2, 0, -8, 5123, -8932, etc.
Description: Used to store integer values. They can be negative, zero and positive.
Range: -32768 to 32767
Size: 2 Bytes
Syntax: int x= 5;

2) Float Data Type

Example: 1.5621, 2.00, 0.435, -8.021, etc.
Description: Used to store decimal values. They can be negative, zero and positive.
Range: -3.4e38 to +3.4e38
Size: 4 Bytes
Syntax: float x= 5.43;

3) Character Data Type

Example: a, g, t, B, T, Z etc. Also 1, 2, 3 etc and symbols like +, %, # etc. But don't confuse these numbers with integers as we cannot apply any mathematical operations on them. It is just to print numbers. 
Description: Used to store a single Character
Range: -128 to +127 (ASCII values of characters)
Size: 1 Byte (notice the size difference between int and char)
Syntax: int x= 'c';  (always write the value inside single quote('))

 U might want to know how to store a name. I will answer it after some posts as it is an extension on Data Types and one needs to get familiar with these first before moving ahead.

Finally u might be exhausted reading a post this long and hence there wont be any tutorial. I recommend the student to be fresh before learning something new and hence end this post here.

No comments:

Post a Comment