Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

C data type


May 11, 2021 C


Table of contents


C data type

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

The types in C can be divided into the following:

Serial number Type and description
1 basic type:
They are arithmetic types, including two types: integer type and floating point type.
2 Enumeration Type:
They are also arithmetic types, which are used to define variables that can only give them a certain discrete integer value in the program.
3 Void type:
Type specifier void Indicates that there is no available value.
4 Deport type:
They include: pointer type, array type, structural type, common type type, and function type.

Array types and structure types are collectively referred to as aggregate types. T he type of function refers to the type of value that the function returns. We'll cover the basic types later in this chapter, and the other types will be covered in the next few chapters.

The integer type

The following table lists 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

To get the exact size of a type or variable on a particular platform, you can use the sizeof operator. T he expression sizeof(type) gets the storage byte size of the object or type. The following example demonstrates the size of the get int type:

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

int main()
{
   printf("Storage size for int : %d \n", sizeof(int));
   
   return 0;
}

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

Storage size for int : 4

Floating-point type

The following table lists details about the storage size, value range, and accuracy of a standard floating-point type:

type Storage size Value range Precision
float 4 byte 1.2e-38 to 3.4e + 38 6 decimal
double 8 byte 2.3e-308 to 1.7e + 308 15 decimal
long double 10 byte 3.4e-4932 to 1.1e + 4932 19 decimal

The header file float.h defines macros that can be used in programs to use these values and other details about the representation of real binary numbers. The following example outputs the storage space occupied by the floating-point type and its range values:

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

int main()
{
   printf("Storage size for float : %d \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;
}

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

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 there are no values available. It is typically used in three situations:

Serial number Type and description
1 Function returns empty
There are various functions in c, or you can say that they return empty.The return type of the function that does not return value is empty.E.g void exit (int status);
2 Function parameter is empty
Various functions in C do not accept any parameters.A function without parameters can accept a void.E.g int rand(void);
3 Pointer pointing VOID
The type of pointer to the VOID * represents the address of the object, not the type.For example, memory allocation functions void *malloc( size_t size ); Returns a pointer to the VOID, which can be converted to any data type.

If you still don't fully understand the void type, don't worry too much, we'll cover these concepts in more detail in a later section.