为什么这里的头文件用到了string?
可以删掉,用不上
这里应该没什么用,头文件引入,可以多引入不相关的,但是不能少引入程序中使用到的
在这里 头文件string.h就是个多余,gets,puts等函数在stdio中
#include<stdio.h>//String
int main()
{
char *str="hello";
//字符串与字符数组的关系:
//字符串是结尾有'\0'的字符数组
//如:"hello"等于{'h','e','l','l','o','\0'}
for(int i=0;i<5;i++)
{
printf("%c",str[i]);
}
printf("\n");
//一行变两行
printf("hello \
world\n"); //注意第一行结尾\后还有一个空格
return 0;
}
在C语言程序设计中,头文件string.h主要是为了使用字符串处理函数。这些函数能方便地处理字符串的常见操作,包括连接,复制,比较和查找等。例如,可以使用strcat函数将两个字符串连接起来,使用strcpy函数复制一个字符串,使用strcmp函数比较两个字符串是否相等,使用strstr函数在一个字符串中查找另一个字符串出现的位置等。头文件string.h中还有很多其他有用的函数,需要根据实际需求选择使用。
下面举一个头文件string.h中函数的例子:使用strcpy函数复制一个字符串到另一个字符串中。
#include <stdio.h>
#include <string.h>
int main()
{
char source_string[20] = "hello, world!";
char destination_string[20];
strcpy(destination_string, source_string); //将source_string复制到destination_string
printf("source_string: %s\n", source_string);
printf("destination_string: %s\n", destination_string);
return 0;
}
在这个例子中,我们首先定义了一个源字符串source_string和一个目标字符串destination_string。然后使用strcpy函数将source_string中的内容复制到destination_string中。最后,我们输出两个字符串的内容,可以看到它们是相同的。
总之,头文件string.h提供了很多便于操作字符串的函数,可以在C语言程序设计中起到很重要的作用。
使用了puts() gets()等方法