sizeof 运算符返回的是一个 unsigned 类型
#include<stdio.h>
int array_table[6]={12,14,15,35};
#define LEN (sizeof(array_table)/sizeof(array_table[0]))
int main(void)
{
int x=-1;
if(x<=LEN)
printf("can be printfed!!!! ");
else
printf("Can not be printf!!!");
return 0;
}
/*********************************
程序输出结果:
Can not be printf!!!
**********************************/
运算符 sizeof 的返回值是 unsigned 类型,x 是 int 类型,比较时会将 x 升级为 unsigned 类型,而 -1 变为 unsigned 类型是一个很大的整数。
2、sizeof操作符能够得到一个结构的整体长度,包括因边界对齐而跳过的那些字节。
3、sizeof 和 strlen 的区别
sizeof 是操作符,而 strlen 是库函数,sizeof 的参数可以是数据的类型,也可以是变量,而 strlen 只能以结尾为 \0 的字符串作为参数;
编译器在编译时就计算出了 sizeof 的结果。而 strlen 函数必须在运行时才能计算出来。并且 sizeof 计算的是数据类型占内存的大小,而 strlen 计算的是字符串实际的长度。