基础
- 在
printf
时,遇到\0
会停止打印 - 字符串字面量""在输入数组时会自动在末尾加入’\0’也就是说,字符串字面量在内存的长度是多一个字节的
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char arr[10] = { 'h','e','l','l','o' };
//%s遇到'\0'就会停止打印,而在ascii中'\0'就是数字0(数组未初始化的值都是0)
printf("%s\n", arr);
char arr1[]= { 'h','e','l','l','o' };//这里只输入了5个字符'h' 'e' 'l' 'l' 'o'
char arr2[] = "hello";//这里输入了六个字符"hello"和"\0"
printf("%d\t%d\n", sizeof(arr1),sizeof(arr2));
printf("%s\n", arr1);//因为arr1数组里没有'\0'所以会继续打印一直到有'\0'为止
system("pause");
return 0;
}
- 初始化数组之后,不能再赋值,要使用
string.h
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
//char arr[10] = "wang"; // 声明时直接赋值是可以的
char arr[10];
//arr = "hello"; //声明后,再赋值是不行的
printf("%s\n", arr);
strcpy(arr, "wang"); //声明后想要赋值,需要使用`strcpy`.这种操作主要用在结构体含有字符串的情景下,因为结构体在声明数组元素时是先声明,没有赋值
printf("%s\n", arr);
system("pause");
return 0;
}
# include <stdio.h>//引用函数库
# include <stdlib.h>
void main(){
char str1[3] = {'a','b','c'};//错误,因为没有"\0"终止符,如果后面不为0则会出现异常
printf("%x\n", str1);
printf("%s\n",str1);
char str2[4] = {'a','b','c','\0'};//最后一个赋值为\0,打印终止
printf("%x\n", str2);
printf("%s\n",str2);
char str3[5] = {'a','b','c'};//数组位赋值的默认为0,而ascii编号为0代表的就是\0
printf("%x\n", str3);
printf("%s\n",str3);
printf("%d %d",sizeof("abc"),sizeof(str3));
//"abc"字符串长度为4,因为最后一位是\0,而str是一个数组所以长度为5,但打印字符串时
//遇到\0会停止,所以也会打印出"abc"
}
2cc929b0
abc
2cc929c0
abc
2cc929d0
abc
4 5
字符串常量
# include <stdio.h>//引用函数库
# include <stdlib.h>
void main(){
char *str = "hello world";
printf("%d\n",sizeof(str));
printf("%d\n",sizeof("hello world"));
printf("%x\n%c\n",str,*str);
int i=0;
while(*str){//*str为\0的时候停止(*str!="\0",\0既是ascii中的0,这里注意space空格ascii是32
putchar(*str);//输出字符串
str++;//指针向前移动1位
i++;//移动一位计数1
}
printf("\n%d",i);//打印字符串长度,利用"\0"结尾这一特性
}
字符串变量(利用数组)
# include <stdio.h>//引用函数库
# include <stdlib.h>
void main(){
char str[12]= "hello world";
int i=0;
while (str[i]){
putchar(str[i]);
i++;
}
printf("\n");
char *p = str;
while (*p){//纯指针移动打印
putchar(*p);
p++;
}
printf("\n");
char *q = str;
while (q[0]){//q[0]等价于*(q+0),指针数组的方式
putchar(q[0]);
q++;
}
}
字符串初始化
# include <stdio.h>//引用函数库
# include <stdlib.h>
void main(){
char str1[100] = {'h','e','l','l','o',' ','w','o','r','l','d'};//每个数组元素赋值
char str2[100] = {'h','e','l','l','o',' ','w','o','r','l','d','\0'};//每个数组元素赋值
char str3[100] = {"hello world"};//直接赋值
char str4[100] = "hello world";
//str1和str2等价,注意在给每一个元素赋值的时候不能用""号
printf("%s\n%s\n%s\n%s\n",str1,str2,str3,str4);
}
字符串数组
# include <stdio.h>//引用函数库
# include <stdlib.h>
void main(){
char str[5][5] = {
"abc1",
"abc2",
"abc3",
"abc4",
"abc5",
};
for (int i =0;i<5;i++){
printf("%s\n",str[i]);
}
}
字符串输入
- 数组字符串输入
# include <stdio.h>//引用函数库
# include <stdlib.h>
void main(){
char str[10]={0};//初始化数组
scanf("%s",str);//初始化字符串
printf("%s",str);
}
- 指针字符串输入
# include <stdio.h>//引用函数库
# include <stdlib.h>
void main(){
//char *p = NULL;//0x00000不行,必须让指针指向可读写的内存地址
//char *p = "AAAAA";//常量不可写
char str[10] = {0};
char *p =str;//这里指针存储了可读可写的内存地址
scanf("%s",p);
printf("%s",p);
}
- 指针与字符串常量
# include <stdio.h>//引用函数库
# include <stdlib.h>
void main(){
//char str[100];
//str[100]="abc";//str[100]代表了第101个元素
//str="abc"//str是数组名,常量不可修改
char *p = "abc";//p存储了字符串常量的地址
printf("%s\n",p);//打印字符串
printf("%p\n",*p);//打印字符串的地址,这个和其他指针正好相反
char *q;
q="abc";
printf("%s",q);
}
其他
# include <stdio.h>//引用函数库
# include <stdlib.h>
void main(){
char str[10]="abc";
char *p=str;
printf("%p\n",p);//打印指针地址
printf("%c %c %c\n",*(p+0),*(p+1),*(p+2));//指针形式打印每个字符
printf("%c %c %c\n",p[0],p[1],p[2]);//数组指针的形式打印每个字符
printf("%p %p %p\n",p+0,p+1,p+2);//打印每个字符的地址
printf("%s %s %s\n",p,p+1,p+2);//打印指针地址开始的字符串,所以内容需要是地址
*p='x';//可读写内存,可以被赋值
//str="def"//str是常量不可以被赋值
printf("%s\n",p);
char *q ="def";
printf("%c %c %c\n",*(q+0),*(q+1),*(q+2));//%c打印q是无法显示的因为是地址
printf("%c %c %c\n",q[0],q[1],q[2]);
printf("%p %p %p\n",q+0,q+1,q+2);
printf("%s %s %s\n",q,q+1,q+2);
//*q='x';//常量不可读写,不可以被赋值
}