Storage of strings
The string is stored in a character array, and the last one must be
, the compiler considers this character array as a pointer to the char* type.
For example: printf("abc") //传递的时abc的地址,并且是a的地址
Operation:
char"abc";
char"abc"[1];
String variables
charstr[8"abcdefg";//会把这几个字符复制到这个字符数组中;
He is stored in the following ways:
If the string is not 7, the remaining units will be filled in.
If more than 7 will not consider this array as a string, the string must be followed by A;
charstr"abcdeg";//编译器会自动计算长度,str是首字符的地址char *str"abcdef";//str是一个指针指向abcdef首字符的地址,并且str一定要指向一个存在的字符串
Read and write String
printf("string is %s"str);//puts(str);scanf("%s"str); //首先会跳过空白字符,然后读取,再次遇见空白字符停止读取gets(str);//遇见换行符才会停止
C Language String Library
The C language string library must contain # include
charstr[10];str"abc";//错误,不能使用=号运算符把字符串复制到字符数组中
String copy function strcpy function
char"hello world!";char str2[10];strcpy(str2, str1);
string concatenation function Strcat function
strcat(str1, "abc");//这里的str1要有足够控件容下abc
string comparison function strcmp function
strcmp(str1, str2);//相等返回0,str1>str2就返回大于0的数
function Strlen function for finding string length
intlen = strlen(str1);
Best way to store strings
The best way to store a string is a two-dimensional array;
charstring[][3] = {"hello""nihao"};char *string[] = {"hello","world","I","love","you"};
"C-language summary" string