Data Types
Primary objective of any program is to process data. Data is raw information that your program manipulated to yield a meaningful information. Data comes in various forms, name of a city is a data and age of a person is also a data but fundamentally they are of different nature. age is a number and you can add two numbers, multiply them but you cannot add or multiply city names. Bottom line is, you need to classify data on the basis of applicable operations, storage requirement, internal representation and other factors.
When you write a program, you think of required data. You have to understand in which category it falls. C language provides fundamental categories of data known as data types. Of course you can create your own data categories whenever required. You will learn about defining your own data types in chapters covering structure, union and enumerators.
Data types provided by C language are also known as primitive data types. On various occasions, like during variable creation, you need to specify its ability to hold data of which category. To support such situations, C language provided five reserved words (or keywords). They are known as data types.
- int
- char
- float
- double
- void
As they are keywords, their meaning is known to the compiler.
The use of void is little bit different from the rest of the four, you will learn about void later in functions chapter.
Data Type Declaration Instruction
One of the use of these data types is during declaration of variables. Declaring variables is a statement telling compiler about the kind of data that variable can hold.
Following example statements provide a glimpse of data type declaration instruction
int x=5, y;
float k=2.34;
char a=’y’, ch;
double d1;
Explanation:
- Each statement in C language is terminated with a semi-colon (;)
- You can use only one data type in a statement to declare variable. So there are four different statements to demonstrate the use of all the data types
- statement begins with the data type, followed by the variable names separated by commas(,)
- In above example, there are six variables declared. They are x, y, k, a, ch and d1.
- Variables declared in one statement are of same kind. For example x and y are both of type int, so they have common characteristics
- Size of int type block is 4 bytes, size of float block is 4 bytes, size of char block is 1 byte, size of double block is 8 bytes. So you can calculate the total memory consumption. It is 4 bytes for x, 4 bytes for y, 4 bytes for k, 1 byte for a, 1 byte for ch and 8 bytes for d1.
- Data type also specify the kind of data a variable can accommodate. int variable can contain integer constant, float and double variable can contain real constant and char variable can contain character constant.
- Some of the variables are initialized during declaration. Assigning values to the variables during declaration is optional. Uninitialized variable contains unpredictable pattern of 0s and 1s, which is known as garbage value.x contains 5, k contains 2.34, a contains ‘y’ and rest of the variable contains garbage values.
- x=5, do not read it like x is equal to 5, the correct way to read it is x assigns 5 or 5 is assigned to x. In C language, ‘=’ is known as assignment operator and ‘==’ is known as ‘equal to’ operator.