// 数字0和'\0'在字符串中都是表示字符串结束
0=='\0'// true
// printf,scanf用%c来输入输出字符
char c=4;// ASCII值为4对应的字符
char d='4';// 字符4,ASCII值为52
'A'是65;'a'是97
'0'// 字符0,ASCII值为48
'\0'// 空字符,ASCII值为0
printf("%d\n",'0');// 48
printf("%d\n",'\0');// 0
printf("%d\n",'\n');// 10
字符串
char a[] = "string literal";
char *p = "string literal";
A string literal can be used in two slightly different ways:
1.As the initializer for an array of char, as in the declaration of char a[] ,it specifies the initial values of
the characters in that array (and, if necessary, its size).
2.Anywhere else, it turns into an unnamed, static array of characters,and this unnamed array may be stored in read-only
memory,and which therefore cannot necessarily be modified.
In an expression context, the array is converted at once to a pointer,as usual,so the second declaration initializes p
to point to the unnamed array's first element.
// 字符串字面量
The reason the type is not const is backwards compability.
C didn't have the const qualifier in the beginning.
In C++, string literals have the type const char[].
char *str="hello";//strlen是5,sizeof是8(指针的大小都是8)
char word[]="hello";// strlen是5,sizeof是6
char word[]={'h','e','l','l','o','\0'};// strlen是5,sizeof是6
char line[10]="hello";//strlen是5,sizeof是10(剩下5个元素都是'\0')
char a[5] = "";// 数组的五个元素都是空字符'\0',strlen(a)是0
char a[] = "";// sizeof(a)是1,元素是空字符'\0'
char *p ="abcd";
// p存放的是字符串常量"abcd"的首地址(可以改变p的值,但是不能通过p来修改字符串的内容)
// 字符串常量存储在只读数据段中,不可修改
char p[] = "abcd";// 字符数组p,并将字符串"abcd"拷贝到该数组中
字符串函数
#include<string.h>
//字符串长度(不包含末尾的\0)
//The strlen() function calculates the length of the string pointed to by s, excluding the terminating null byte ('\0').
//strnlen()查看前maxlen个字符
//如果没碰到\0,就返回maxlen;
//如果碰到\0,就返回\0前面的字符个数(不含\0自己)
size_t strnlen(const char s[.maxlen], size_t maxlen);
//复制字符串,复制src到dest中
//dest至少要有strlen(src)+1的空间
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
//拼接字符串
//把src拼到dest后面.dest的terminating null byte被覆盖掉,换成src的首字符
//确保dest有strlen(原dest)+strlen(src)+1这么大的空间
//char *strcat(char *dest, const char *src);
char user_home[strlen("u_")+strlen(pt.c_str())+1] = "u_";
strcat(user_home,pt.c_str());
//比较两字符串
//返回值:
//0,s1等于s2
//正数,s1>s2,数值是第一对不相等的两个字符之间的差
//负数,s1<s2
int strcmp(const char *s1, const char *s2);
//比较前n个字符
strncmp
//返回字符出现的位置
//returns a pointer to the first occurrence of the character c in the string s.
char *strchr(const char *s, int c);
//returns a pointer to the last occurrence of the character c in the string s.
char *strrchr(const char *s, int c);
//寻找子串needle第一次出现的位置
char *strstr(const char *haystack, const char *needle);
https://stackoverflow.com/questions/12795850/string-literals-pointer-vs-char-array
https://c-faq.com/decl/strlitinit.html
https://man7.org/linux/man-pages/man3/strcmp.3.html
https://man7.org/linux/man-pages/man3/strcat.3.html
https://codingbison.com//c/c-string-library-copying-functions.html
https://man7.org/linux/man-pages/man3/strcpy.3.html
https://man7.org/linux/man-pages/man3/strstr.3.html
https://man7.org/linux/man-pages/man3/strchr.3.html