C语言sizeof和strlen

来源:互联网 发布:js给一个二维数组赋值 编辑:程序博客网 时间:2024/04/25 14:21

1、sizeof数据类型占的内存大小

数据类型        32位系统 (字节)       64位系统(字节)

char                 1                                1

short                2                                2

int                    4                                4

long                4                                8

double             8                                8

long double     12                              16

char *p            4                                8

数组(参数)       4                                8    //数组是函数参数时相当于指针

数组                  数组定义长度          数组定义长度

2、strlen占用内存大小

1)strlen计算字符串的长度,计算到字符串结束字符\0之前为止

char *p="hello world";

len=strlen(p); 

len的值是11

char *p="hello world\n";

len=strlen(p);

len的值是12

2)数组中存储的字符串的大小

char buf[128] = {0};

strcpy(buf,"hello world");

len=strlen(buf);

len的值是11

len=sizeof(buf);

len的值是128


0 0
原创粉丝点击