在C语言中,字符串最本质就是一个字符数组,和整数数组类似,也可以对其进行下标遍历,当然在学完指针后,对字符串的定义和遍历就可以借用指针来输出
示例:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
printf("the first way to define a string :\n");
char str[25] = {'h','e','l','l','o'};
for(i=0;i<sizeof(str)/sizeof(str[0]);i++){
printf("%c",str[i]);
}
printf("\n");
printf("the second way to define a string:\n");
char str2[25] = "hello";
for(i=0;i<sizeof(str2)/sizeof(str2[0]);i++){
printf("%c",str2[i]);
}
printf("\n");
printf("the third way to define a string:\n");
char *str3 = "hello , this is WangXiaoming , a handsome boy , nice to meet you ! ";
printf("%s",str3);
return 0;
}