C Strings: Definition and Assignment Guide
In the C language, strings can be defined and assigned using character arrays. Strings are character arrays that end with a null character ‘\0’.
Here is an example code demonstrating how to define and assign a string:
#include <stdio.h>
int main() {
// 定义一个字符数组来存储字符串
char str[50];
// 赋值字符串
strcpy(str, "Hello, World!");
// 打印字符串
printf("The string is: %s\n", str);
return 0;
}
In the given example, we first defined an array of characters named str with a length of 50 to store a string. Then we used the strcpy function to assign the string “Hello, World!” to str. Finally, we used the printf function to print out this string.
It is important to note that when assigning strings in C language, the size of the target character array must be large enough to accommodate the assigned string content and the null character at the end.