首页  

C语言字符串     所属分类 c 浏览量 695
编译器在初始化数组时,自动把 \0 放在字符串的末尾

char str[] = "hello";
char * str3 = "hello";
str3 字符串常量 不可修改


#include <stdio.h>
#include <string.h>

int main()
{
   
    char str[] = "hello";
    printf("%s\n",str);
    // 6  编译器自动追加 \0
    printf("%d\n",sizeof(str));
    // 5
    printf("%d\n",strlen(str));
	
    int len = sizeof(str)/sizeof(str[0]);
    // 6
    printf("%d\n",len);
    char str2[9] = "hello";
    // 5
    printf("%s\n",str2);
    // 9
    printf("%d\n",strlen(str2));
	
    // 9
    len = sizeof(str2)/sizeof(str2[0]);
    printf("%d\n",len);

    char * str3 = "hello";
    printf("%s\n",str3);
    // 5
    printf("%d\n",strlen(str3));
	
    char str4[] = {'h','i'};
    // 2  结尾不会追加 \0
    printf("%d\n",sizeof(str4));
    // printf("%s\n",str4);
	
		
    char * str5 = "abc";
    // 字符串常量 不可修改
    // abc
    printf("%s\n",str5);
    str5++;
    // bc
    printf("%s\n",str5);

    return 0;
}


上一篇     下一篇
C C++ static关键字

指针数组 数组指针 函数指针 指针函数

康威定律

new和malloc的区别

堆和栈的区别

c语言中定义和声明的区别