C Data types

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

Types in C can be divided into the following categories:

Serial NumberTypes and Descriptions
1Basic Data Types
They are arithmetic types, including integer (int), character (char), floating-point (float), and double-precision floating-point (double).
2Enumeration types:
They are also arithmetic types, used to define variables that can only be assigned certain discrete integer values in a program.
3void type:
Type SpecifiervoidA data type representing no value, usually used for function return values.
4Derived types:
Including array types, pointer types, and structure 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 following part of this chapter, we will introduce basic types; the other types will be explained in later chapters.

Integer Types

The following table lists details about the storage sizes and value ranges of standard integer types:

TypeStorage SizeValue Range
char1 byte-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
unsigned int2 or 4 bytes0 to 65,535 or 0 to 4,294,967,295
short2 bytes-32,768 to 32,767
unsigned short2 bytes0 to 65,535
long4 bytes-2,147,483,648 to 2,147,483,647
unsigned long4 bytes0 to 4,294,967,295

Note that the storage size of each type depends on the system's bit count, but currently 64-bit systems are the mainstream.

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

To obtain the exact size of a type or a variable on a specific platform, you can usesizeofOperator. Expressionsizeof(type)Obtains the storage byte size of an object or type. The following example demonstrates getting the size of the int type:

Example

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

%luis a 32-bit unsigned integer, see for detailsC Library Function - printf()。

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

int 存储大小 : 4 

Floating-point types

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

TypeStorage SizeValue RangePrecision
float4 bytes1.2E-38 to 3.4E+386 significant digits
double8 bytes2.3E-308 to 1.7E+30815 significant digits
long double16 bytes3.4E-4932 to 1.1E+493219 significant digits

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 floating-point types and their range values:

Example

#include <stdio.h> #include <float.h> int main() { printf("float maximum storage bytes: %lu\n", sizeof(float)); printf("float minimum value: %E\n", FLT_MIN ); printf("float maximum value: %E\n", FLT_MAX ); printf("Precision value: %d\n", FLT_DIG ); return 0; }

%Eoutputs single- and double-precision real numbers in exponential form, see for detailsC Library Function - printf()。

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

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

void type

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

Serial NumberTypes and Descriptions
1Function returns nothing
In C, various functions do not return a value, or you can say they return nothing. The return type of a function that does not return a value is void. For example:void exit (int status);
2Function parameters are empty
In C, various functions do not accept any parameters. A function without parameters can accept a void. For example:int rand(void);
3Pointer points to void
A pointer of type void * represents the address of an object, rather than a type. For example, the memory allocation functionvoid *malloc( size_t size );returns a pointer to void, which can be cast to any data type.

If you still cannot fully understand the void type now, don't worry too much; we will explain these concepts in detail in later chapters.


Type conversion

Type casting is converting a value of one data type to a value of another data type.

There are two types of type conversion in C:

  • Implicit type conversion:Implicit type conversion occurs automatically in expressions without any explicit instruction or function call. It usually automatically converts a smaller type to a larger type, for example, converting int to long or float to double. Implicit type conversion may also cause loss of data precision or data truncation.

  • Explicit type conversion:Explicit type conversion requires the use of the type casting operator, which can force a value of one data type to be converted to a value of another data type. Force casting allows programmers to control data types more precisely when necessary, but it may also cause data loss or truncation.

Implicit type conversion example:

Example

int i = 10;
float f = 3.14;
double d = i + f; // Implicitly convert int type to double type

Explicit type conversion example:

Example

double d = 3.14159;
int i = (int)d; // Explicitly convert double type to int type
other extensions