int main()
{
#ifdef BEGIN
printf("ifdef endif");
freopen("poj.txt","r",stdin);
#endif // BEGIN
char arrInt[10]={' ',' '};
double *arrIntP[20];
double arrDouble[20];
char charArr[20];
printf("arrInt:sizeofp:%d strlen:%d\n",sizeof(arrInt),strlen(arrInt));
printf("arrIntP:sizeofp:%d arrDouble:sizeofp%d\n",sizeof(arrIntP),sizeof(arrDouble));
printf("charArr:sizeof:%d \n",sizeof(charArr));
//printf(":%lf",pow(2,62));
return 0;
}
测试结果:
说明:head:#include <string.h>
function:size_t strlen (const char *s);//必须为不可变的字符串和整形、浮点数计算没有关系 函数说明:
strlen()用来计算指定的字符串s 的长度,不包括结束字符"\0"。 返回值:返回字符串s 的字符数
而sizeof是计算某变量所最大可拥有的存储空间,用于防治越界或者是超过最大范围
Sizeof() (sizeof运算符)from
描述
sizeof操作符返回一个变量类型的字节数,或者该数在数组中占有的字节数。
语法
sizeof(variable)
参数
- variable: 任何变量类型或数组(如int,float,byte)
示例代码
sizeof操作符用来处理数组非常有效,它能很方便的改变数组的大小而不用破坏程序的其他部分。
这个程序一次打印出一个字符串文本的字符。尝试改变一下字符串。
char myStr[] = "this is a test"; int i; void setup(){ Serial.begin(9600); } {0}void{/0}{1} {/1}{2}loop{/2}{1}() {{/1} for (i = 0; i < sizeof(myStr) - 1; i++){ Serial.print(i, DEC); Serial.print(" = "); Serial.println(myStr[i], BYTE); } }
请注意sizeof返回字节数总数。因此,较大的变量类型,如整数,for循环看起来应该像这样。
for (i = 0; i < (sizeof(myInts)/sizeof(int)) - 1; i++) {//此处为其真正用途 //用myInts[i]来做些事 }