(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

相关推荐
侃侃_天下11 小时前
最终的信号类
开发语言·c++·算法
echoarts12 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix12 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题12 小时前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说13 小时前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔13 小时前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
小莞尔13 小时前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
liujing1023292913 小时前
Day03_刷题niuke20250915
c语言
我是菜鸟0713号13 小时前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_13 小时前
QT(4)
开发语言·汇编·c++·qt·算法