c语言里:sizeof怎样用法?

Python018

c语言里:sizeof怎样用法?,第1张

1、首先打开VS,新建一个 使用sizeof求出数组大小 project。

2、接着在左侧文件树添加一个 sizeof.c 源文件。

3、其里面有stdio.h和stdlib.h头文件,也可自己输入。

4、然后输入main函数主体及返回值。

5、定义一个数组,使用sizeof计算出数组的大小。

6、最后编译运行程序,便能输出数组的大小。

sizeof是C语言中保留关键字,也可以认为是一种运算符,单目运算符。常见的使用方式:

int a=10

int arr=[1,2,3]

char str[]="hello"

int len_a = sizeof(a)

int len_arr = sizeof(arr)

int len_str = sizeof(str)

printf("len_a=%d,len_arr=%d,len_str=%d\n",len_a,len_arr,len_str)

结果是:len_a=4,len_arr=12,len_str=6

扩展资料:

在 Pascal 语言中,sizeof() 是一种内存容量度量函数,功能是返回一个变量或者类型的大小(以字节为单位)。

在 C 语言中,sizeof() 是一个判断数据类型或者表达式长度的运算符。

在Pascal 语言与C语言中,对 sizeof() 的处理都是在编译阶段进行。

sizeof是C/C++中的一个操作符(operator),简单的说其作用就是返回一个对象或者类型所占的内存字节数。

MSDN上的解释为:

The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type(including aggregate types). This keyword returns a value of type size_t.

参考资料:

百度百科-sizeof