Latest web development tutorials

C data types

In the C language, data type refers to a wide range of systems for variable or function declaration different types. Type of the variable determines the variable storage space occupied, and how to interpret the stored bit patterns.

C can be divided into the following categories:

序号类型与描述
1基本类型:
它们是算术类型,包括两种类型:整数类型和浮点类型。
2枚举类型:
它们也是算术类型,被用来定义在程序中只能赋予其一定的离散整数值的变量。
3void 类型:
类型说明符void表明没有可用的值。
4派生类型:
它们包括:指针类型、数组类型、结构类型、共用体类型和函数类型。

Array type and structure of the type referred to as aggregate types. Type of the function refers to the type of function return values. In the next section of this chapter we will introduce the basic type, other types will explain in the back a few chapters.

Integer type

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

类型存储大小值范围
char1 字节-128 到 127 或 0 到 255
unsigned char1 字节0 到 255
signed char1 字节-128 到 127
int2 或 4 字节-32,768 到 32,767 或 -2,147,483,648 到 2,147,483,647
unsigned int2 或 4 字节0 到 65,535 或 0 到 4,294,967,295
short2 字节-32,768 到 32,767
unsigned short2 字节0 到 65,535
long4 字节-2,147,483,648 到 2,147,483,647
unsigned long4 字节0 到 4,294,967,295

Note that the size of the various types of storage systems related to the number of bits, but a 64-bit general-purpose system based.

Here are the differences between 32-bit and 64-bit system memory size (windows the same):

In order to obtain a type of a variable or the exact size on a particular platform, you can use thesizeof operator.Expressionsizeof (type)to give an object or type of storage bytes. The following example demonstrates Get int type size:

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

int main()
{
   printf("int 存储大小 : %lu \n", sizeof(int));
   
   return 0;
}

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

int 存储大小 : 4 

Float type

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

类型存储大小值范围精度
float4 字节1.2E-38 到 3.4E+386 位小数
double8 字节2.3E-308 到 1.7E+30815 位小数
long double16 字节3.4E-4932 到 1.1E+493219 位小数

Float.h header file defines macros can be used in the program and other relevant details of the values ​​of real numbers represented in binary. The following examples will output a floating-point type storage space as well as its range of values:

#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 in Linux and execute the above program, it will produce the following results:

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

void type

void type specified value is not available. It is typically used in the following three cases:

序号类型与描述
1函数返回为空
C 中有各种函数都不返回值,或者您可以说它们返回空。不返回值的函数的返回类型为空。例如void exit (int status);
2函数参数为空
C 中有各种函数不接受任何参数。不带参数的函数可以接受一个 void。例如int rand(void);
3指针指向 void
类型为 void * 的指针代表对象的地址,而不是类型。例如,内存分配函数void *malloc( size_t size );返回指向 void 的指针,可以转换为任何数据类型。

Now if you still can not fully understand the void type, do not worry too much, in the subsequent sections we will elaborate on these concepts.