C data type


In C language, data types refer to an extensive system for declaring variables or functions of different types. The type of a variable determines how much space the variable storage occupies and how the stored bit pattern is interpreted.

The types in C can be divided into the following categories:

Serial numberType and description
1Basic types:
They are arithmetic types, including two types: integer types and floating point types.
2Enumeration types:
They are also arithmetic types and are used to define only certain values ​​that can be assigned to them in the program. A discrete integer-valued variable.
3void Type:
Type specifier void indicates that no value is available.
4 Derived types:
They include: pointer types, array types, structure types, union types and function types.

Array types and structure types are collectively called aggregate types. The type of a function refers to the type of the function's return value. In the rest of this chapter we will introduce the basic types, and several other types will be explained in the following chapters.

Integer types

The following table lists details about the storage size and value range of the standard integer types:

##unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295short2 bytes-32,768 to 32,767unsigned short2 bytes 0 to 65,535long4 bytes -2,147,483,648 to 2,147,483,647unsigned long4 bytes 0 to 4,294,967,295

Note that the storage sizes of various types are related to the number of system bits, but currently the most common ones are 64-bit systems.

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

In order to get a certain type or the exact size of a variable on a specific platform, you can use the sizeof operator. The expression sizeof(type) gets the storage byte size of an object or type. The following example demonstrates getting the size of int type:

#include <stdio.h>#include <limits.h>int main(){
   printf("int 存储大小 : %lu \n", sizeof(int));   
   return 0;}

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

int 存储大小 : 4

Floating point type

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

TypeStorage sizeValue range
char1 Bytes-128 to 127 or 0 to 255
unsigned char1 byte0 to 255
signed char1 byte -128 to 127
int2 or 4 bytes -32,768 to 32,767 or -2,147,483,648 to 2,147,483,647
TypeStorage sizeValue rangePrecision
float4 bytes1.2E-38 to 3.4E+38 6 decimal places
double8 bytes2.3E-308 to 1.7E+30815 decimal places
long double16 bytes3.4E-4932 to 1.1E+493219 Decimal places

#The header file float.h defines macros that allow you to use these values ​​and other details about the binary representation of real numbers in your program. The following example will output the storage space occupied by the floating point type and its range value:

#include <stdio.h>#include <float.h>int main(){
   printf("float 存储最大字节数 : %lu \n", sizeof(float));
   printf("float 最小值: %E\n", FLT_MIN );
   printf("float 最大值: %E\n", FLT_MAX );
   printf("精度值: %d\n", FLT_DIG );   
   return 0;}

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

float 存储最大字节数 : 4 float 最小值: 1.175494E-38float 最大值: 3.402823E+38精度值: 6

void type

The void type specifies no available value. It is usually used in the following three situations:

##2##3The pointer points to void#If you still can’t fully understand the void type now, don’t worry too much, we will explain these concepts in detail in subsequent chapters.
Serial numberType and description
1Function returns null
There are various functions in C that do not return a value or you can say they return null. The return type of a function that does not return a value is null. For example void exit (int status);
The function parameter is emptyThere are various This function does not accept any parameters. Functions without parameters can accept a void. For example
int rand(void);
The pointer of type void * Represents the address of the object, not the type. For example, the memory allocation function void *malloc( size_t size );
returns a pointer to void that can be converted to any data type.