Data Types in C
Data types are used for declaring variables or functions
of different types before their use. The data type is required to identify the
storage usage and the different type of operations that can be performed on
that data.
Basically, Data Types divided into two categories.
Primary/ Primitive Data Type: Integer float, character
Secondary/Derived Data Type: Array, Pointer, Structure,
Union, Enum. etc.
There
are two types of type qualifier in c
Size qualifier: short, long
Sign qualifier: signed, unsigned
Basic data type |
Data
type with type qualifier |
Consume memory space
in bytes |
Range |
Int |
int or signed int unsigned int short int or signed short int unsigned short int long int or signed long int unsigned long int |
2 2 1 1 4 4 |
-32768 to 32767 0 to 65535 -128 to 127 0 to 255 -2147483648 to 2147483647 0 to 4294967295 |
Float: float keyword
used to define a float data type, These contain whole numbers with a fractional part
, it can be positive and negative, if a sign qualifier not mentioned them consider
sign qualifier as default, It must consist of a decimal point, and %f format Specifier
is used for input and output purposes.
Float also having types called
as double and
long double, both are
keywords.
double format specifier is %lf and long double format specifier is %Lf
Basic data type |
Data type with type qualifier |
Consume memory space in bytes |
Range |
float
|
float
|
4 |
-3.4E-38
to 3.4E+38 |
double |
double Long
double |
8 10 |
.7E-308
to 1.7E+308 3.4E-4932
to 1.1E+4932 |
Character: char keyword used to define a character data type, The value may be a small letter or capital value contain It must be enclosed into the ‘ ‘ single quotes., and %c format Specifier is used for input and output purpose. When you declare a group of characters is called a string which is enclosed into “ “ double quotes
Basic data type |
Data type with type qualifier |
Consume memory space in bytes |
Range |
Char |
char
or signed char Unsigned
char |
1 1 |
-128
to 127 0
to 255 |
Ex. char
ch= ’P’;
Secondary/Derived Data Type
Array: It is a
collection of objects of the same data type, Array elements are stored in
contiguous memory Location
Ex. int a [10]; /*array of 10 elements whose name is ‘a’
and type is integer */
Structure: It is
a collection of objects of different data types.
Ex. struct student
{ int rollno=25;
Float marks=10.5;
};
Union: A collection of objects of different types, is also called structure. but the difference between structure and union is that union uses shared memory for variables while structure assigns separate memory to every variable.
Enumeration: It is a user-defined data type that allows the user to create their own data type values as required.
Ex. enum color {red, orange, green, black, white, pink,
blue};