(c语言版)strlen和sizeof的区别

c 复制代码
#include<stdio.h>
#include<string.h>
void Fun(char str[100]){
    printf("7:%d ",sizeof(str)); 
    printf("8:%d ", strlen(str));
    char *p=str;
    printf("9:%d ",sizeof(p));   
    printf("10:%d ", strlen(p));
    printf("11:%d ",sizeof(*p));
    printf("12:%d ", strlen(*p));
}
int main(){
    char str[100]={"a\101\08abc"};
    printf("1:%d ",sizeof(str));   //开辟的字节数为100
    printf("2:%d ",strlen(str));   //遇到第一个\0结束,输出2
    printf("3:%d ",sizeof("a\101\08abc")); //输出8,会计算字符串结尾的\0
    printf("4:%d ", strlen("a\101\08abc")); //遇到\0结束,且不计算\0所占字节
    char *p=str;
    printf("5:%d ",sizeof(p));        
    printf("6:%d ", strlen((p)));
    Fun(str);
}

输出结果:

1:100 2:2 3:8 4:2 5:8 6:2 7:8 8:2 9:8 10:2 11:1

c 复制代码
#include<stdio.h>
#include<string.h>
int main(){
    char str[100]={65,0,'a','b','c'};
    printf("%s",str);
    return 0;
}

输出结果:

A

相关推荐
代龙涛7 小时前
WordPress page.php 页面模板与自定义模板使用方法
android·开发语言·php
bigfootyazi7 小时前
python爬虫-基本库-urllib库(常用速查)
开发语言·爬虫·python
belong_my_offer8 小时前
认识到精通函数
开发语言·python
guygg888 小时前
最大相关-最小冗余(mRMR)特征选择 MATLAB 实现
开发语言·matlab
郭涤生8 小时前
C++ 高性能编程最佳实践清单
开发语言·c++
烛衔溟8 小时前
TypeScript 类的静态成员与静态方法
开发语言·javascript·typescript
Nile8 小时前
解密Palantir系列一:4. Ontology 不是哲学
开发语言·前端·javascript
罗超驿9 小时前
15.JavaScript 函数与作用域完全指南:语法、参数、表达式与作用域链实战
开发语言·前端·javascript
.千余9 小时前
【C++】C++类与对象2:C++构造函数、运算符重载与流输入输出全面解析
c语言·开发语言·前端·c++·经验分享
郭涤生9 小时前
C++ 高性能状态机
开发语言·c++