(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语言入门,深入理解指针(6)
c语言·开发语言
略略略咯咯11 小时前
33333
开发语言·windows·python
垆边人似月.11 小时前
第三篇:函数、文件与异常处理 / Functions, Files & Exception Handling
开发语言·python
云和数据.ChenGuang11 小时前
项目打开后没有主类
开发语言·人工智能·langchain·1024程序员节
鬼手点金11 小时前
缓存作为架构约束
开发语言·前端·人工智能·python·microsoft·缓存·架构
Navigator_Z11 小时前
LeetCode //C - 1306. Jump Game III
c语言·算法·leetcode
SEO_juper11 小时前
Node.js 页面主题聚焦度检测:title 和正文各说各的,AI 也读不懂
开发语言·人工智能·node.js·seo·外贸独立站
软件技术NINI11 小时前
基于PHP和MySQL的快递代收点管理系统的设计与实现
开发语言·javascript·数据库·mysql·毕业设计·php·毕设
Dovis(誓平步青云)11 小时前
一个分区超温就全棚浇水,控制系统怎样按区下策略
开发语言·前端·javascript·人工智能·机器学习
kiracrimson11 小时前
编码简述:内存里没有文字,只有一张对照表
开发语言·c++·二进制·计算机组成原理