Data Types in C
Data refers to some specific values used in a program. Data can be either a variable or a constant. The two fundamental data types used in C are integer and floating point. Other types are derived from the fundamental types.
Primitive Data Types
Integer
An integer value refers to a whole number. It may be positive or negative numbers. Integer data type in C program accepts to store only integer values in the variables.
Integer values range from -32768 to +32767 for a signed integer. Negative integer is represented by attaching a '-' (minus) sign to the number. Where as to represent a positive integer '+' (plus) sign is not used as it is illegal according to C programming. Any number represented without sign then it implicitly refers to positive number. If we try to store any decimal number then there is a spill of data and the exponent values will be lost.
Syntax:
int identifier_name;
int is a keyword used to define integer data type and identifier_name is the variable name.
Examples:
Example 1:
int a;
Integer Variable Memory Representation before initialization
Example 2:
int b = 10;
Integer Variable Memory Representation after initialization
Example 2, shows an integer constant 'b' with value 10 initialized.
In the examples shown, Example 1, it shows a variable 'a' of type integer declared and it doesn't have value stored in it. Whereas Example 2, shows an integer constant 'b' with value 10 initialized.
Floating Point
Floating point data type stores both integer as well as decimal numbers. Floating point as the name itself defines the point is floating or at variable location.
Double Precision
Double stores larger floating point values.
Character Type
Character type stores any character.
Data Type representation chart
Representation of different data types:
Sl. No. | Keyword | Type of data handled | Size of data type | Format Specifier |
---|---|---|---|---|
1 | char | Character type. | -128 to 127 or 0 to 255 | %c |
2 | unsigned char | Unsigned character type. | 0 to 255 | %c or %hhu |
3 | signed char | Signed character type. | -128 to 127 | %c or %hhi |
4 | signed int int | Signed Integer type. | ||
5 | signed short int short int | Signed Short Integer type. | ||
6 | signed long int long int long | Signed Long Integer type. | ||
7 | unsigned int unsigned | Unsigned Integer type. | ||
8 | unsigned short int unsigned short | Unsigned Short Integer type. | ||
9 | unsigned long int unsigned long | Unsigned Integer type. | ||
10 | float | Floating point decimal value. | ||
11 | double | Double precession floating point decimal value. | ||
12 | long double | Extended double precision floating point decimalvalue. |
Derived Datatypes
Arrays
Arrays are sequential memory locations under the same name. This section is covered in Arrays in-detail.
Pointers
Pointer data types are variables that holds address location of another variable. This section is covered in coming chapter in-detail.