Easy Tutorial
❮ C Basic Syntax C Function Tolower ❯

C Data Types

In C language, data types refer to a broad system used to declare different types of variables or functions. The type of a variable determines the space it occupies in storage and how the bit pattern stored is interpreted.

Types in C can be classified into the following categories:

No. Type & Description
1 Basic Types: <br>These are arithmetic types and include integer types and floating-point types.
2 Enumeration Types: <br>These are also arithmetic types and are used to define variables that can only be assigned certain discrete integer values throughout the program.
3 void Type: <br>The type specifier void indicates that no value is available.
4 Derived Types: <br>These include pointer types, array types, structure types, union types, and function types.

Array types and structure types are collectively referred to as aggregate types. The type of a function indicates the type of the return value of the function. In the following sections of this chapter, we will introduce basic types, and other types will be covered in the subsequent chapters.

Integer Types

The following table provides details about the storage size and value range for standard integer types:

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 4 bytes -2,147,483,648 to 2,147,483,647
unsigned long 4 bytes 0 to 4,294,967,295

Note that the storage size of various types is related to the system's bit count, but currently, 64-bit systems are commonly used.

The following lists the differences in storage size between 32-bit and 64-bit systems (same for Windows):

To get the exact size of a type or a variable on a specific platform, you can use the sizeof operator. The expression sizeof(type) gives the storage bytes size of the object or type. The following example demonstrates getting the size of the int type:

Example

#include <stdio.h>
#include <limits.h>

int main()
{
   printf("Storage size for int: %lu \n", sizeof(int));

   return 0;
}

%lu is for 32-bit unsigned integers. For detailed explanations, see C Library Function - printf().

When you compile and execute the above program on Linux, it produces the following result:

Storage size for int: 4

Floating-Point Types

The following table provides details about the storage size, value range, and precision for standard floating-point types:

Type Storage Size Value Range Precision
float 4 bytes 1.2E-38 to 3.4E+38 6 decimal places
double 8 bytes 2.3E-308 to 1.7E+308 15 decimal places
long double 16 bytes 3.4E-4932 to 1.1E+4932 19 decimal places

The float.h header file defines macros, which can be used in a program to get details about the binary representation of real numbers. The following example will output the storage space taken up by a float type and its range values:

Example

#include <stdio.h>
#include <float.h>

int main()
{
   printf("Storage size for float: %lu \n", sizeof(float));
   printf("Minimum float positive value: %E\n", FLT_MIN );
   printf("Maximum float positive value: %E\n", FLT_MAX );
   printf("Precision value: %d\n", FLT_DIG );

   return 0;
}

%E is for outputting single and double precision real numbers in exponential form. For detailed explanations, see C Library Function - printf().

When you compile and execute the above program on Linux, it produces the following result:

Storage size for float: 4 
Minimum float positive value: 1.175494E-38
Maximum float positive value: 3.402823E+38
Precision value: 6

void Type

The void type specifies that no value is available. It is used in three kinds of situations:

No. Type & Description
1 Function returns as void <br>There are various functions in C that do not return any value, or you can say they return void. A function with no return value has the return type as void. For example, void exit (int status);
2 Function arguments as void <br>There are various functions in C that do not accept any parameter. A function without parameters can accept a void. For example, int rand(void);
3 Pointer to void <br>A pointer of type void * represents the address of an object, not its type. For example, a memory allocation function void *malloc( size_t size ); returns a pointer to void which can be cast to any data type.

If you are not completely understanding the void type now, don't worry too much, as these concepts will be explained in detail in subsequent chapters.

❮ C Basic Syntax C Function Tolower ❯