The data type defines a set of values and a set of operation on those values. The computer manipulates various types of data. This data is given to the computer as input. The data is processed according to the program instructions to produce outputs. As the computer memory is organized in bytes, and the memory for each variable is associated with its data type.
The data that is entered or the information and its data type are defined before designing the actual program that is used to process the data. The type of each data value is identified at the beginning of program design.
C program may need to process different types of data. Each data type require amount of memory. C programming language provides the following data types:
|int||Stores numeric values|
|float||Stores real values|
|double||Stores large real values|
|char||Stores character values|
|bool||Stores either true or false|
|void||It is value less. Void means “nothing”|
Categories of Data types:
C programming language provides the following ways to use data types:
- Standard data types/built in data types
- User defined data types
A data type that is predefined in the language is called standard data type or built in data type. Some examples of standard data type are int, float, long, bool and char etc.
The C programming language also allows the user to define his data types and they are called user defined data types.
Integer data is the numeric value with no decimal point or fraction. It includes both positive and negative values. The minus sign – is used to indicate the negative value. If no sign is used, the value is considered to be positive by default.
Some of examples of integer type data are 15, -2, etc.
The C programming language provides different types of integer data. These are as follows:
- Int (it is used to store whole number values)
- Short int (it takes two bytes in memory)
- unsigned int (it stores only positive integer values)
- long int (it stores larger values)
- Unsigned long int (it is used to store larger positive values)
|Data type||Size in Bytes||Ranges|
|int||2||-32768 to 32767|
|short||2||-32768 to 32767|
|Unsigned int||2||0 to 65535|
|long||4||-2147483648 to 2147483647|
|Unsigned long||4||0 to 4294967295|
Floating point data type:
This type of data is a numeric value with decimal point or fraction. It is also called real type data. It includes both positive and negative values.
10.5, -3.4, etc
Types of floating point:
C programming language provides different types of floating point data. These are as follows:
- Long double
|Data type||Size in Bytes||Description|
|float||4||3.4*10^-38 to 3.4*10^38|
|double||8||1.7*10^-308 to 1.7*10^308|
|Long double||10||1.7 *10^-4932 to 1.7*10^4932|
Character data type:
This data type is used to store character value. It takes 1 byte in memory. It is used to represent letters, number or punctuation marks and a few other symbols.
These types of values are normally given in single quotes.
‘a’, ‘5’, ‘#’, etc
The bool data type is used to store either true or false.
Void data type is used in functions that do not have any return value this will be discussed on later chapters.
The data type of an expression depends on the types of operand in the expression. For example the result of an expression is int if both the operands are integer.
A variable is a named memory location or memory cell. It is used to store program’s input data and its computational results during execution. The value of variable may be changed during the execution of program. However, the name of the variable cannot be changed.
The variables are created in RAM. RAM is a temporary memory. That is why the data stored is also temporary. It can only be used and processed during the execution of program. The data stored in the variable is automatically removed when program ends.
Name of variable: It refers to an identifier that represents a memory location.
Address of variable: It refers to the memory location of the variable.
Contents of variable: It refers to the value stored in memory location referred by variable.
The process of specifying the name of the variable and its data type is called variable declaration. A program can have as many variables as needed. C is a strongly typed language. It means that all the variables must be declared before their use in the program.
The variable declaration provides information to the compiler about the variable. The compiler uses the declaration to determine how much memory is needed for each variable. Different types of data require different amount of memory. The memory is allocated when the variable is declared.
Once the variable is declared its data type cannot be changed during the program execution. However the value can be changed.
Data_type variable name;
EXAMPLE: int marks;
Generally there is a slight difference between declaring and defining a variable. The variable declaration specifies only the name of the variable and its data type but definition of variable specifies the name, data type of variable as well as allocates the memory location.
Declaration and definition of variables are not specified separately. The variable definition is automatically performed when a variable is declared.
The process of assigning a value to a variable at the time of declaration is known as variable initialization.
It is discussed earlier that the compiler automatically allocates the required memory for the variable when it is declared. This memory location may contain some meaningless data. This meaningless data is call garbage value. It may produce unexpected results in computation. To avoid this are variables are initialized.
Following are some rules for naming a variable:
- Variables may include letters, numbers and underscore.
- The must character must be a letter or underscore.
- Blank spaces are not allowed.
- Special symbols cannot be used.
- Reserved words are not allowed.
- A variable can be up to 31 characters long.
- Both upper and lower case letters are allowed.
Consider the following line of code in which we have declared a variable:
int a, b;
In the above line of code we have declared two variables of same data type that is integer data type.