Cprogramming 简明教程

C - The sizeof Operator

sizeof operator 是编译时 unary operator 。它用于计算其操作数的大小,该操作数可以是数据类型或变量。它以字节数返回大小。

The sizeof operator is a compile−time unary operator. It is used to compute the size of its operand, which may be a data type or a variable. It returns the size in number of bytes.

它可以应用于任何数据类型、浮点类型或指针类型变量。

It can be applied to any data type, float type, or pointer type variables.

sizeof(type or var);

当 sizeof() 与数据类型一起使用时,它只是返回分配给该数据类型的内存量。输出在不同的机器上可能有所不同,例如,32 位系统可以显示与 64 位系统不同的输出。

When sizeof() is used with a data type, it simply returns the amount of memory allocated to that data type. The outputs can be different on different machines, for example, a 32-bit system can show a different output as compared to a 64-bit system.

Example 1: Using the sizeof Operator in C

我们来看看下面的例子。它强调了如何在 C 程序中使用 sizeof 运算符 −

Take a look at the following example. It highlights how you can use the sizeof operator in a C program −

#include <stdio.h>

int main(){

   int a = 16;

   printf("Size of variable a: %d \n",sizeof(a));
   printf("Size of int data type: %d \n",sizeof(int));
   printf("Size of char data type: %d \n",sizeof(char));
   printf("Size of float data type: %d \n",sizeof(float));
   printf("Size of double data type: %d \n",sizeof(double));

   return 0;
}

Output

运行此代码,您将获得以下输出−

On running this code, you will get the following output −

Size of variable a: 4
Size of int data type: 4
Size of char data type: 1
Size of float data type: 4
Size of double data type: 8

Example 2: Using sizeof with Struct

在这个例子中,我们声明了一个类型 struct 并查找了结构类型变量的大小。

In this example, we declare a struct type and find the size of the struct type variable.

#include <stdio.h>

struct employee {
   char name[10];
   int age;
   double percent;
};

int main(){
   struct employee e1 = {"Raghav", 25, 78.90};
   printf("Size of employee variable: %d\n",sizeof(e1));
   return 0;
}

Output

运行代码并检查其输出:

Run the code and check its output −

Size of employee variable: 24

Example 3: Using sizeof with Array

在下面的代码中,我们声明了一个由 10 个 int 值组成的 array 。在数组变量上应用 sizeof 运算符将返回 40。这是因为 int 的大小是 4 个字节。

In the following code, we have declared an array of 10 int values. Applying the sizeof operator on the array variable returns 40. This is because the size of an int is 4 bytes.

#include <stdio.h>

int main(){

   int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   printf("Size of arr: %d\n", sizeof(arr));
}

Output

运行代码并检查其输出:

Run the code and check its output −

Size of arr: 40

Example 4: Using sizeof to Find the Length of an Array

在 C 语言中,我们没有一个函数可以返回数字数组中的元素数量(我们可以使用 strlen() function 查找字符串中的字符数量)。为此,我们可以使用 sizeof() 运算符。

In C, we don’t have a function that returns the number of elements in a numeric array (we can get the number of characters in a string using the strlen() function). For this purpose, we can use the sizeof() operator.

我们首先查找数组大小,然后将其除以其数据类型的大小。

We first find the size of the array and then divide it by the size of its data type.

#include <stdio.h>

int main(){

   int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
   int y = sizeof(arr)/sizeof(int);

   printf("No of elements in arr: %d\n", y);
}

Output

当你运行这段代码时,它将产生以下输出:

When you run this code, it will produce the following output −

No of elements in arr: 10

Example 5: Using sizeof in Dynamic Memory Allocation

sizeof 运算符用于计算使用 malloc()calloc() 函数动态分配的内存块。

The sizeof operator is used to compute the memory block to be dynamically allocated with the malloc() and calloc() functions.

malloc() 函数与以下 syntax 一起使用 −

The malloc() function is used with the following syntax

type *ptr = (type *) malloc(sizeof(type)*number);

以下语句分配一个包含 10 个整数的块,并将其地址存储在指针中 −

The following statement allocates a block of 10 integers and stores its address in the pointer −

int *ptr = (int *)malloc(sizeof(int)*10);

当 sizeof() 与表达式一起使用时,它将返回表达式的长度。这里有一个示例。

When the sizeof() is used with an expression, it returns the size of the expression. Here is an example.

#include <stdio.h>

int main(){

   char a = 'S';
   double b = 4.65;

   printf("Size of variable a: %d\n",sizeof(a));
   printf("Size of an expression: %d\n",sizeof(a+b));

   int s = (int)(a+b);
   printf("Size of explicitly converted expression: %d\n",sizeof(s));

   return 0;
}

Output

运行代码并检查其输出:

Run the code and check its output −

Size of variable a: 1
Size of an expression: 8
Size of explicitly converted expression: 4

Example 6: The Size of a Pointer in C

sizeof() 运算符返回相同的值,而与类型无关。其中包括内置类型的 pointer 、派生类型或 double pointer

The sizeof() operator returns the same value irrespective of the type. This includes the pointer of a built−in type, a derived type, or a double pointer.

#include <stdio.h>

int main(){

   printf("Size of int data type: %d \n", sizeof(int *));
   printf("Size of char data type: %d \n", sizeof(char *));
   printf("Size of float data type: %d \n", sizeof(float *));
   printf("Size of double data type: %d \n", sizeof(double *));
   printf("Size of double pointer type: %d \n", sizeof(int **));
}

Output

运行代码并检查其输出:

Run the code and check its output −

Size of int data type: 8
Size of char data type: 8
Size of float data type: 8
Size of double data type: 8
Size of double pointer type: 8