(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

相关推荐
tiana_12 分钟前
写了个零依赖的 Go 版本管理器,curl | bash 完事
开发语言·golang·bash
嘘嘘出差21 分钟前
Python 爬虫入门实战:爬取豆瓣电影 Top 250
开发语言·爬虫·python
爱写代码的倒霉蛋26 分钟前
实现协程的三种方式
开发语言·python
CS创新实验室1 小时前
NumPy数组的C风格和Fortran风格
c语言·开发语言·numpy
逝水无殇2 小时前
C# 正则表达式详解
开发语言·后端·正则表达式·c#
朱永博2 小时前
使用Onnruntime实现Padim/Patchcore推理
开发语言·c#
前端H2 小时前
Biome & Rolldown:Rust 工具链接管前端基建
开发语言·前端·rust
天天进步20152 小时前
UI-TARS 源码解析 #4:UI-TARS 与传统 RPA 的区别:为什么它不是简单的坐标点击脚本?
开发语言
运维行者_2 小时前
广域网性能监控:分布式IT架构下的链路质量保障
开发语言·网络·分布式·后端·架构·数据库架构
梅雅达编程笔记2 小时前
零基础学 Python 第7章 | 字典 dict:键值对存储
开发语言·python·beautifulsoup·numpy·pandas