LECTURE#02
Variables & Data types
SUMMARY
· Variables
· Data types
Variables
In computer programming variable is a name given to data storage location. During Programming we need to store data, this data is stored in variables. The memory is divided into blocks and each block has unique hex-decimal address like 0xA1111111. It is very difficult for us to handle these addresses in our program so we named these hex-decimal addresses and these names are called variables because they can contains different values at different times.
Data types
While programming we need to store different types of data like “123” is numeric type data,” 123.4” is decimal fraction number, “ABC” is alpha type data. In computer programming these names are different
Numeric = Integer [examples Student Roll No, Marks Obtained in Mathematics]
Decimal = Float [examples Percentage of matriculation like 67.65]
Alpha = Character [examples to store Name of student]
So every variable has data type associated with it.
How to declare variable in C Language?
Suppose we need to store the Student Roll No in a variable, so we declare it like
Int <variable-Name>;
So it will only store the whole numbers because we declare it as Integer (Int)
Consider another example if we need to calculate percentage of matriculation exams, so here we need variable that can hold decimal fraction like
Float <variable-name>;
Now variable “percentage” is of float type so it can hold a value with full decimal point.
Consider another example for character data type, if we need to store the name of the student, so we need character type variable that can hold alpha data like
Char <variable-name>;
Each data type has its size defined, like Integer has 4-bytes, well C language provide 3 variant for numeric data type, depict in figure below
Short Int | Int | Long Int |
2 bytes | 4 bytes | 8 bytes |
-32768 to +32768 | | |
C language provide 3 variants for fraction data type
Float | Double | Long double |
4 bytes | 8 bytes | 10 bytes |
Unsigned variable
A situation may arise in programming that you need a variable that have a capability to store just positive value, for example if you want to store price of an item, price can’t be in negative so you can declare unsigned variable like
Unsigned Int <variable-name>;
Unsigned float <variable-name>;
No comments:
Post a Comment