Data types in C Language
Data type is the way to specify the type of data and range of values that can be stored in a variable. C supports three types of data types:-
- Primary Data Types
- Derived Data Types
- User Defined Data Types
Primary Data Types
There are four primary data types provided by C as follows:
Integer Data Type
Different Integer data types are:
Name
of Data Type |
Size in Bytes |
Range
of Values |
short | 1 | -27 to 27 –1
(-128 to 127) |
signed short | 1 | -27 to 27 –1
(-128 to 127) |
unsigned short | 1 | 0 to 28-1
(0 to 255) |
int | 2 | -215 to 215 –1
(-32768 to 32767) |
signed int | 2 | -215 to 215 –1
(-32768 to 32767) |
unsigned int | 2 | 0 to 216-1 (0 to 65535) |
long | 4 | -231 to 231 –1
(-2,147,483,648 to 2,147,483,647) |
signed long | 4 | -231 to 231 –1
(-2,147,483,648 to 2,147,483,647) |
unsigned long | 4 | 0 to 231 –1
(0 to 4,294,967,295) |
Floating point Data Type
Different types of floating point data type are:
Name
of Data Type |
Size
in Bytes |
Range
of Values |
float | 4 | ±3.4X10-38 to ±3.4X 10+38 |
double | 8 | ±1.7X10-308 to ±1.7X 10+308 |
Long double | 10 | ±3.4X10-4932 to ±3.4X 10+4932 |
Character Data Type
Character data type can be categorized into following types:
Name
of Data Type |
Size
in Bytes |
Range
of Values |
char | 1 | -27 to 27 –1
(-128 to 127) |
signed char | 1 | -27 to 27 –1
(-128 to 127) |
unsigned char | 1 | 0 to 28 –1
(0 to 255) |
void
void means null or empty.
Derived Data Types
There are following types of derived data types provided by C Language.
Array
It is defined as collection of homogeneous types of elements having same size, type and range of values which can be stored in it.
Function
It is defined as named block of code which can be called anywhere in the same program or another program.
Pointer
It is basically used to store the address of another variable. Pointers provide faster execution of program.
const
The keyword const can be used to declare a named constant.
Example: const int a=10;
User Defined Data Types
There are following types of user defined data types provided by C
Structure
It is defined as collection of heterogeneous types of elements. Structure can contain a multiple variables of different data types under a single name of structure.
Union
It is also defined as collection of heterogeneous types of elements. Unions are basically used where we need to use one of the members of a union at a time. A union starts with the keyword union.
typedef
typedef is used to give a new name to the existing data type.
Example:
typedef int distance.
In this example, data type int has been named as distance. We can use the word distance as a data type to declare variables of int type as:
distance one,two;
Output:
one=1500
enum
enum stands for enumerated. enum is used to define a sequence of named constants which contain consecutive values starting from 0 (zero).
Example:
enum month{jan, feb, mar, apr, may=10, jun, jul, aug, sep, oct, nov, dec};
In this example, JAN has been assigned value 1, so FEB will automatically contain 2, and so on. MAY has been assigned 10 so JUN will contain value 11 and JUL will contain 12 and so on.
Program to demonstrate the use of enum
#include<stdio.h> int main() { enum month{jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};clrscr(); month m; m=MAR; printf(”m=%d”,m); printf(”\nDEC=%d”,DEC); return(0); } |
volatile keyword
volatile keyword is a qualifier that is applied to a variable when it is declared. It tells the compiler that the value of the variable may change at any time without any action being taken by the code that compiler finds nearby.
volatile int a=10;
In this example a is the name of the volatile variable and it contains value 10.
Program to demonstrate the use of keyword volatile
#include<stdio.h> int main() { volatile a=10; printf(“\na=%d”,a); return(0); } |
Comments
These are user defined messages written inside a program. Comments start with /* (Slash asterisk) and end with */ (Asterisk Slash).
/* Developed By: Author name*/ int main() /*Program starts here*/ { printf(”Comments are here”); return(0); } /*program ends*/ |