A data type refers to an extensive system used for declaring variables or functions of different types. The type of a variable determines how much space it occupies in storage and how the bit pattern stored is interpreted.
There are three C data types:
- Primary data type (or fundamental or basic data type)
These are fundamental data types in C namely integral(int and char), floating-point(float and double) and void.
Those data types which are derived from fundamental data types are called derived data types. Derived data types don't create a new data type but, instead they add some functionality to the basic data types. They includes pointers, functions and arrays.
User-defined data type is a data type that is defined by the user. They allows you to create a new data type which can be used, just like any other basic data type, to declare variables. They include structure, union, enumeration and typedef.
|
C Data Types |
We will see primary data types in the following section where as other types will be covered in the upcoming tutorials.
Primary Data Types
These are fundamental data types in C namely integral(int and char), floating-point(float and double) and void.
char is used to store any single character, int is used to store integer value, float is used for storing single precision floating point number and double is used for storing double precision floating point number.
We can use type qualifiers with these basic types (int, char, float and double) except void to get some more types.
There are two types of type qualifiers:
- Size qualifiers - short, long
- Sign qualifiers - signed, unsigned
The qualifiers signed
and unsigned
can be applied to char and integer types. When the qualifier unsigned
is used the number is always positive, and when signed
is used number may be positive or negative. If the sign qualifier is not mentioned in integers, then by default signed
qualifier is assumed. If the sign qualifier is not mentioned for char type, then whether the char type is signed
or unsigned
is machine dependent. The range of values for signed data types is less than that of unsigned type. This is because in signed type, the leftmost bit is used to represent the sign, while in unsigned type this bit is also used to represent the value.
The qualifier short
and long
can be applied to int
type to get types short int
and long int
. The qualifier long
can be applied to double
to get the long double
which stores extended precision floating point number.
Size and range of Integer type on 16-bit machine:
Type |
Size in bytes |
Range |
char |
1 |
-128 to 127 |
unsigned char |
1 |
0 to 255 |
signed char |
1 |
-128 to 127 |
int |
2 |
-32,768 to 32,767 |
unsigned int |
2 |
0 to 65,535 |
signed int |
2 |
-32,768 to 32,767 |
short int |
2 |
-32,768 to 32,767 |
unsigned short int |
2 |
0 to 65,535 |
signed short int |
2 |
-32,768 to 32,767 |
long int |
4 |
-2,147,483,648 to 2,147,483,647 |
signed long int |
4 |
-2,147,483,648 to 2,147,483,647 |
unsigned long int |
4 |
0 to 4,294,967,295 |
Size and range of Floating point type on 16-bit machine:
Type |
Size in bytes |
Range |
Precision |
float |
4 |
3.4E-38 to 3.4E+38 |
6 decimal places |
double |
8 |
1.7E-308 to 1.7E+308 |
15 decimal places |
long double |
10 |
3.4E-4932 to 1.1E+4932 |
19 decimal places |
The size and range of different data types is machine and compiler dependent. The range of integral types is given in the header file limits.h, and the range and precision for floating point types is given in header file float.h. We can use the sizeof() operator to find out the size of various data types on our system. To find out the range of values we can see the macros defined in header files limits.h and float.h.
Program to find out the size and limits of data types:
#include <stdio.h>
#include <limits.h>
#include <float.h>
int main(void)
{
printf("sizeof(char) = %u\n", sizeof(char));
printf("sizeof(int) = %u\n", sizeof(int));
printf("sizeof(long) = %u\n", sizeof(long));
printf("sizeof(float) = %u\n", sizeof(float));
printf("sizeof(double) = %u\n", sizeof(double));
printf("SCHAR_MIN = %d\n", SCHAR_MIN);
printf("SCHAR_MAX = %d\n", SCHAR_MAX);
printf("UCHAR_MAX = %d\n", UCHAR_MAX);
printf("INT_MIN = %d\n", INT_MIN);
printf("INT_MAX = %d\n", INT_MAX);
printf("UINT_MAX = %u\n", UINT_MAX);
printf("LONG_MIN = %ld\n", LONG_MIN);
printf("LONG_MAX = %ld\n", LONG_MAX);
printf("ULONG_MAX = %lu\n", ULONG_MAX);
printf("FLT_MIN = %e\n", FLT_MIN);
printf("FLT_MAX = %e\n", FLT_MAX);
printf("DBL_MIN = %e\n", DBL_MIN);
printf("DBL_MAX = %e\n", DBL_MAX);
/* Number of digits of precision */
printf("FLT_DIG = %d\n", FLT_DIG);
printf("DBL_DIG = %d\n", DBL_DIG);
return 0;
}
When you compile and execute the above program on Visual Studio 2013 IDE it produces following result on Windows 7:
When you compile and execute the above program on Turbo C++ 3.0 IDE it produces following result:
The void Data Type
The void
type is added in ANSI C. It is also known as Empty
data type. It is used in three kinds of situations:
(a) Function returns as void
When specified as a function return
type, void
means that the function does not return
a value.
void message(char *name)
{
printf("Hello, %s", name);
}
Here, message()
is a void
function. The keyword void
is preceded by the function name. This function when executed displays only message and does not return any value to the calling function.
(b) Function arguments as void
The void
keyword is also used as argument for function. When found in a function heading, void
means that the function does not take any arguments.
int fun(void)
{
return 1;
}
Here the fun()
does not require any argument. It returns an integer value.
(c) Pointers to void
A pointer of type void *
represents the address of an object, but not its type.
void *malloc(size_t size);
Here the memory allocation function void *malloc(size_t size);
returns a pointer to void which can be casted to any data type.
ReplyDeleteThanks for sharing a good article. This is very interesting and I like this type of article only. I have always read important article like this.
C C++ Training in Chennai
C Training in Chennai
core java training in chennai
javascript training in chennai
javascript course in chennai
core java training in chennai
core java training