Data Types
Filed under Data Types and Operators , on Thursday, February 04, 2010
0
Different types of data are stored in variables. Some examples are:
• Numbers
Whole numbers. For example, 10 or 178993455
Real numbers. For Example, 15.22 or 15463452.2511
Positive numbers
Negative numbers
• Names. For example, John
• Logical values. For example, Y or N
The data that is to be stored in variables is of different types and so it requires different amounts of memory.
A data type decides the amount of memory to be allocated to a variable to store a particular type of data.
To allocate memory for a particular piece of data we have to:
Declare a variable of a particular data type.
The term, declaring a variable, means that some memory is allocated and that portion of memory will be referred to by the variable name.
The general form of declaring a variable is:
Data Type [Variable Name] (C style)
Dim [Variable Name] as Data Type (Basic style)
Data types, which are commonly used in programming tools can be classified as:
• Numeric: Stores numeric value.
• Alphanumeric: Stores descriptive information
These data types can have different names in different tools. For example, a numeric data type is called int in the tool C while Visual Basic refers to it as Integer. Similarly, an alphanumeric data type is named char in C while in Visual Basic it is named string. In any case, the data stored is the same. The only difference is that the variables used in a tool have to be declared with the name of the data type supported by that tool.
Numeric data
Numbers of any type constitute numeric data. The numbers may be whole or real.
Numeric data can contain:
• Only the numeric characters of 0 to 9
• The decimal point
The most important numeric data types are Integer, Float and Double.
Integer
The data type, which stores numeric data is one of the basic data types in any programming language. It consists of a sequence of one or more digits.
For example in Visual Basic, to store an integer value in a variable called ‘num’ the declaration would be as follows:
Dim num as Integer
The variable ‘num’ cannot then store any other type of data like “Alan” or “abc”. Can store only negative and positive numbers in the range -32,768 to 32,767. In modern Visual Programming Language, Integer types can store numbers in the wide range -2,147,483,648 to 2,147,483,647.
Float
A variable declared to be of type float could be used for storing values containing decimal places. The compiler differentiates between the data types float and integer.
The main difference between the two is, the integer type includes only whole numbers but the float type can be either whole or fractional numbers.
In Visual Basic, Float is usually known as Single data type. Float variable declaration in Visual Basic would be as follows:
Dim f as Single
The variable ‘f’ cannot then store any other type of data like “Alan” or “abc”. However ‘f’’ can store any number type and decimal like 101 or 101.5667. Float type can store real numbers in the range -3.402823E38 to -1.40129E-45 for negative values and 1.401298E-45 to 3.402823E38.
Double
The type Double is used when the accuracy obtained using a variable of the type Float is not sufficient. Variables of the type double can roughly store twice the number of digits as can a float type.
The precise number of digits the float and the double types can store depends upon the particular computer system.
Numbers of type float and double are treated identically by the system in terms of calculation. However using the type float saves memory as it takes only half as much space as a double.
Alphanumeric data
Alphanumeric data can be made up of any type of characters:
• Numeric – 0 to 9
• Alphabetic – A to Z, a to z
• Special Characters – #, $, *, (, ?, >, etc.
Data that is descriptive such as Name, Address, etc. fall in this category.
For example:
• “#10, Rodeo Drive”
• “John Martin”
• “12 Baker Street”
• “$1000.00”
• “1234”
Alphanumeric data is generally enclosed within quotes. Observe that “1234” is considered alphanumeric data and not numeric data. If it were numeric data, it would be represented as 1234, without the enclosing quotes. Mathematical calculations cannot be performed on alphanumeric data in some Programming Tools, but in several Programming Tools, alphanumeric data are converted automatically into numeric data in Mathematical calculations.
String
The type String is used to store a single or multiple characters of information.
A String type must be enclosed within two double quotation marks. Some valid examples of String types are “Halo Mr. $”, “I”, “My hometown is America”, etc.
It is also possible to store digits as characters by enclosing them within double quotes. These should not be confused by numeric values. For example, “1”, “5” and “9” are not to be confused with the numbers 1, 5 and 9. Thus, String types like “5123” and “10020.555” are not the same with Number types 5123 and 10020.555. Although in Visual Basic, arithmetic operations between String and Number types are converted automatically.
Date/Time
Date and time values have special data types all to themselves. This is necessary because a date comprises of the:
• Day
• Month
• Year
While time values comprises of:
• Hour
• Minute
• Second
There are special data types for date and time values. This is necessary because a date comprises of the:
• Data – In the case of some tools, this is a single data type that handles both date and time values while in some others this data type handles only date values.
• Time – Tools that handle date and time values separately provide this data type to handle time values.
There is no general rule followed as to the size of variables of this type. It is dependent on the tool.
Logical Data
Logical data can have only two values:
• True
• False
When represented in numeric terms False is equivalent to zero while any other number (including negative numbers) translates to True.
Logical data is handled by a single data type.
Data Type Size
Boolean 2 bytes
Special Data Types
Some of the programming tools support special types of data such as:
• Large amount of text
• Sound
• Movie
These cannot be handled by any of the regular types discussed above. They require special data types. An example of a situation where a special data type is required, is when an X-ray (image data) is to be scanned and stored.
Every programming tool provides a data type to handle special types of data. The data type is known by different names such as:
• Blob
• Memo
• Long Binary
There is no fixed size for these data types. It depends on the data being stored. In some cases, they can hold as much as 1.2 Gigabytes of data.
Comments Posted (0)
Post a Comment