ADVERTISEMENT
Data Types In C
In C datatypes are a large set of rules for declaring variables and functions of different types. A variable’s type tells you how much space it takes up in storage and how to read the bit pattern it stores.
The types in C can be put into the following groups:
Sr.No. | Types & Description |
1 | Basic Types They are arithmetic types, and there are two more groups: (a) integer type (b) floating-point type . |
2 | Enumerated types Again, these are arithmetic types, and they’re used to set variables that can only take on discrete integer values throughout the program. |
3 | The type void If the type specifier is void, it means that there is no value. |
4 | Derived types This group includes (a) Pointer Types(b) Array Types(c) Structure Types(d) Union Types(e) Function Types |
The aggregate types are made up of the array types and the structure types. The type of a function tells you what kind of value it returns. The next section will talk about the basic types. The next chapters will talk about the other types
Integer Types
The standard integer types, their storage sizes, and the value ranges they support are shown in the table below.
Type | Storage size | Value range |
char | 1 byte | -128 to 127 or 0 to 255 |
unsigned char | 1 byte | 0 to 255 |
signed char | 1 byte | -128 to 127 |
int | 2 or 4 bytes | -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647 |
unsigned int | 2 or 4 bytes | 0 to 65,535 or 0 to 4,294,967,295 |
short | 2 bytes | -32,768 to 32,767 |
unsigned short | 2 bytes | 0 to 65,535 |
long | 8 bytes | -9223372036854775808 to 9223372036854775807 |
unsigned long | 8 bytes | 0 to 18446744073709551615 |
On a certain platform, you can use thesizeof operator to find out the exact size of a type or variable. When you type sizeof(type), you get the object or type’s storage size in bytes. Here is an example of how to use different constants set in limits to get the size of different types on a machine. h file header.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main(int argc, char** argv) {
printf("CHAR_BIT : %d\n", CHAR_BIT);
printf("CHAR_MAX : %d\n", CHAR_MAX);
printf("CHAR_MIN : %d\n", CHAR_MIN);
printf("INT_MAX : %d\n", INT_MAX);
printf("INT_MIN : %d\n", INT_MIN);
printf("LONG_MAX : %ld\n", (long) LONG_MAX);
printf("LONG_MIN : %ld\n", (long) LONG_MIN);
printf("SCHAR_MAX : %d\n", SCHAR_MAX);
printf("SCHAR_MIN : %d\n", SCHAR_MIN);
printf("SHRT_MAX : %d\n", SHRT_MAX);
printf("SHRT_MIN : %d\n", SHRT_MIN);
printf("UCHAR_MAX : %d\n", UCHAR_MAX);
printf("UINT_MAX : %u\n", (unsigned int) UINT_MAX);
printf("ULONG_MAX : %lu\n", (unsigned long) ULONG_MAX);
printf("USHRT_MAX : %d\n", (unsigned short) USHRT_MAX);
return 0;
}C ProgrammingWhen you compile and run the above program on Linux, it gives you the following output:
CHAR_BIT : 8
CHAR_MAX : 127
CHAR_MIN : -128
INT_MAX : 2147483647
INT_MIN : -2147483648
LONG_MAX : 2147483647
LONG_MIN : -2147483648
SCHAR_MAX : 127
SCHAR_MIN : -128
SHRT_MAX : 32767
SHRT_MIN : -32768
UCHAR_MAX : 255
UINT_MAX : 4294967295
ULONG_MAX : 4294967295
USHRT_MAX : 65535C ProgrammingFloating-Point Types
The table below shows the standard floating-point types, along with their precision, storage sizes, and value ranges.
Type | Storage size | Value range | Precision |
float | 4 byte | 1.2E-38 to 3.4E+38 | 6 decimal places |
double | 8 byte | 2.3E-308 to 1.7E+308 | 15 decimal places |
long double | 10 byte | 3.4E-4932 to 1.1E+4932 | 19 decimal places |
The header file float.h sets up macros that let you use these numbers and other information about how real numbers are represented in binary in your programs. The next code shows how much space a float type and its range values take up.
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <float.h>
int main(int argc, char** argv) {
printf("Storage size for float : %d \n", sizeof(float));
printf("FLT_MAX : %g\n", (float) FLT_MAX);
printf("FLT_MIN : %g\n", (float) FLT_MIN);
printf("-FLT_MAX : %g\n", (float) -FLT_MAX);
printf("-FLT_MIN : %g\n", (float) -FLT_MIN);
printf("DBL_MAX : %g\n", (double) DBL_MAX);
printf("DBL_MIN : %g\n", (double) DBL_MIN);
printf("-DBL_MAX : %g\n", (double) -DBL_MAX);
printf("Precision value: %d\n", FLT_DIG );
return 0;
}C ProgrammingWhen you compile and run the above program on Linux, it gives you the following output:
Storage size for float : 4
FLT_MAX : 3.40282e+038
FLT_MIN : 1.17549e-038
-FLT_MAX : -3.40282e+038
-FLT_MIN : -1.17549e-038
DBL_MAX : 1.79769e+308
DBL_MIN : 2.22507e-308
-DBL_MAX : -1.79769e+308
Precision value: 6C ProgrammingThe void Type
The void type means that there is no value. You can use it in three different ways:
Sr.No. | Types & Description |
1 | Function returns as void In C, there are several functions that return void, which means they don’t return anything. The return type of a function that doesn’t give anything back is void. In this case, void exit (int status); |
2 | Function arguments as void C has a lot of functions that don’t take any parameters. Someone can pass a void to a function that doesn’t need one. Something like this: int rand(void); |
3 | Pointers to void A pointer of type void * tells you where an object is, but not what kind of object it is. For example, the void *malloc (size t size); function that frees memory returns a void pointer that can be cast to any data type. |