(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

相关推荐
xieliyu.3 小时前
Java算法精讲:双指针(二)
java·开发语言·算法
何以解忧,唯有..3 小时前
Python包管理工具pip:从入门到精通
开发语言·python·pip
雪的季节3 小时前
RabbitMQ详解
开发语言
ice8130331814 小时前
【Python】Matplotlib折线图绘制
开发语言·python·matplotlib
三品吉他手会点灯4 小时前
C语言学习笔记 - 44.运算符和表达式 - 运算符2 - 除法与取余运算符
c语言·开发语言·笔记·算法
kkeeper~4 小时前
0基础C语言积跬步之动态内存管理
c语言·开发语言
橘右今4 小时前
2026 Java后端高频面试宝典
java·开发语言·面试
艾iYYY4 小时前
string 类的模拟实现
android·服务器·c语言·c++·算法
微小冷5 小时前
Julia卫星工具箱SatelliteToolbox简介
开发语言·航天·坐标转换·julia·卫星工具箱
2601_colin5 小时前
Codex插件全流程实战指南
开发语言·经验分享·笔记·微信开放平台