#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/*逐个输出字符串中的字符*/
void const_print(const char * str);
int main(){
/*字符数组值可改,是不可改值的常量字符串的超集*/
/*字符串常量,不可二次赋值*/
const char *const_str = "const string by ==> const char * ";/*默认包含了'\0'*/
//char *const_str = (char *)"const string by ==> const char * ";/*默认包含了'\0'*/
printf("整体字符串输出:%s\n",const_str);
const_print(const_str);
printf("=====================================================\n");
/*字符数组,可以二次赋值*/
/*方法一*/
char str[] = "char array by ==> char str[]";/*默认包含了'\0'*/
printf("整体字符串输出:%s\n",str);
const_print(str);
printf("=====================================================\n");
/*方法二*/
char ch_arr[] = {'c','h','a','r',' ',
'a','r','r','a','y',' ',
'b','y',' ',
'=','=','>',' ',
's','e','p','a','r','a','t','e','d',' ',
'c','h','a','r','s','\0'
};/*结尾的'\0'不可或缺*/
printf("整体字符串输出:%s\n",ch_arr);
const_print(ch_arr);
return 0;
}
void const_print(const char * str){
printf("逐个字符输出:");
for (int i=0;i<strlen(str);i++){
if (str[i] == '\0') break;
printf("%c",str[i]);
}
printf("\n");
}
输出结果:?
整体字符串输出:const string by ==> const char *? 逐个字符输出:const string by ==> const char *? ===================================================== 整体字符串输出:char array by ==> char str[] 逐个字符输出:char array by ==> char str[] ===================================================== 整体字符串输出:char array by ==> separated chars 逐个字符输出:char array by ==> separated chars?
|