sizeof与strlen的区别:
1.sizeof为一个运算符,可以以类型,函数做参数。而strlen是一个函数,只能用char*做参数,且该char数组必须是以’/0’结尾的,同时是函数引用时就需要头文件。
sizeof以类型为参数
int main()
{
printf("%d\n", sizeof(char));
printf("%d\n", sizeof(short));
printf("%d\n", sizeof(int));
printf("%d\n", sizeof(long));
printf("%d\n", sizeof(float));
printf("%d\n", sizeof(double));
system("pause");
return 0;
}
输出结果为1,2,4,4,4,8
2.sizeof计算声明后字符串所占的内存数(字节大小),不是实际长度。即包含‘\0’的大小。而strlen求字符串长度不包含‘\0’的长度。
具体代码如下;
```int main()
{
char str[20] = "hello";
printf("strlen: %d\n", strlen(str));
printf("sizeof: %d\n", sizeof(str));
system("pause");
return 0;
}
//结果显示为:
//strlen : 5
//sizeof : 20
3.数组做sizeof的参数不退化,传递给strlen就退化为指针了。
数组传递给strlen的本质为将数组内第一个元素的地址传递给strlen。
ps:这几天在论坛上看文章发现自己第一篇博客的一个问题这里修正一下。
static修饰函数:表面显示出来和修饰全局变量是相同的,都是改变作用域,准确的说是缩小了作用域;但本质上修饰函数是改变了函数的链接属性,具体是由外部链接属性转变为内部链接属性。
这次就先分享这些,如有错误希望大家多多指正。共同学习,共同进步。