DataTypes are used for declaring variables and functions of different types.
When program store data in a variable such as an integer, character floating etc, It is necessary that each variable must be assigned a specific data type.
Also, It helps the compiler to reserve memory for variable and how to read and write them.
There are two categories of Data types:
- Primitive/Basic/Primary Datatypes
- User-defined Datatypes
There are following primitive or primary datatypes:
- int (integers)
- char (characters)
- float (floating point numbers)
- void (void datatype)
Integers are whole numbers that represent positive and negative numbers, such as 3, 2, 1, 0, 1, 2, and 3, but not decimal or fractional numbers.
Integer data types hold a maximum of four bytes of information and are declared with the
int (short for integer) keyword, as shown in the following line of code.
In C, you can declare more than one variable on the same line using a single int declaration statement with each variable name separated by commas, as demonstrated next.
int x, y, z;
The preceding variable declaration declares three integer variables named x, y, and z. Remember that executable program statements such as a print statement or in this case a variable declaration require a statement terminator (;).
Floating-point numbers are all numbers, including signed and unsigned decimal and fractional numbers. Signed numbers include positive and negative numbers whereas unsigned numbers can only include positive values. Examples of floating-point numbers are shown in the following list:
Use the keyword
float to declare floating-point numbers, as shown next.
The preceding code declares three floating-point variable data types called operand1, operand2, and result.
Character data types are representations of integer values known as character codes. For example, the character code 90 represents the letter Z.
Characters represent more than just the letters of the alphabet; they also represent numbers 0 through 9, special characters such as the asterisk (*), and keyboard keys such as the Del (delete) key and Esc (escape) key. In all, there are a total of 128 common character codes (0 through 127), which make up the most commonly used characters of a keyboard.
Character codes are most notably organized through the ASCII (American Standard Code for Information Interchange) character set.
In C, character variables are created using the
char (short for the character) keyword as demonstrated next.
Character data assigned to character variables must be enclosed in single quotes (‘), also known as tick marks or apostrophes, and the equal sign (=) is used for assigning data to the character variable.
double-precision floating value is used to store real number with high precision of fractional part ( e.g 2.44445453, 5.4445453543, 4.45353453453, 45.453534534534 ).
void means no value. the
void is used to represent that it has no value. Generally used for mentioning function return type when the function has nothing to return.
The void uses
void as a datatype.
Following are the list of basic data types. It’s size is given according to 32-bit architecture.
|char or signed char||1 byte||-128 to 127|
|unsigned char||1 byte||0 to 255|
|int or signed int||2 byte||-32,768 to 32,767|
|unsigned int||2 byte||0 to 65535|
|short int or signed short int||1 byte||-128 to 127|
|unsigned short int||1 byte||0 to 255|
|long or signed long||4 bytes||-2,147,483,648 to 2,147,483,647|
|unsigned long||4 bytes||0 to 4,294,967,295|
|double||8 bytes||1.7E -308 to 1.7E +308|
|float||4 bytes||3.4E -38 to 3.4E +38|
|long double||10 bytes||3.4E -4932 to 1.1E +4932|
User-defined data type
The user-defined data type is built upon basic type.
The array represents multiple continuous elements of given data type. For example,
// Here 'nums' is an array of 10 elements of integer kind.
// Here 'name' is an array of 12 elements of character kind.
The structure represents different kind of data type as a single record. For example,
// Here 'item' is a structure having two members.
// 'item_id' of integer type.
// 'item_value' of floating type.
Union will be discussed separately in advanced topics of C.