#include <stdio.h> int main() { //char name[] = {'i', 't', 'c', 'H', 's', 't', '\0'}; char name[] = "itcast"; name[3] = 'H'; /* int size = sizeof(name); printf("%d\n", size); */ printf("我在%s上課\n", name); return 0; } // 字符串的一個初始化 void test2() { // \0的ASCII碼值是0 // 都是字符串 char name[8] = "it"; char name2[8] = {'i', 't', '\0'}; char name3[8] = {'i', 't', 0}; char name4[8] = {'i', 't'}; // 不算是一個字符串(只能說是一個字符數組) char name5[] = {'i', 't'}; } /* void test() { // 'a' 'b' 'A' // "jack" == 'j' + 'a' + 'c' + 'k' + '\0' char name[10] = "jack888\n"; // 把數組傳入,僅僅是個警告 printf(name); printf(name); printf(name); printf("57843578435"); }*/
#include <stdio.h> /* \0的作用 1.字符串結束的標記 2.printf("%s", name2); 會從name2這個地址開始輸出字符,直到遇到\0為止 */ int main() { char name[] = "itc\0ast"; char name2[] = {'o', 'k'}; //printf("%s\n", name2); printf("%s\n", &name2[1]); return 0; }
/* strlen函數:計算字符串長度 1.計算的是字符數,並不是字數。一個漢字算作3個字符 2.計算的字符不包括\0 3.從某個地址開始數字符的個數,直到遇到\0為止 */ // strlen函數聲明在string.h文件中 #include <string.h> #include <stdio.h> int main() { //int size = strlen("哈haha"); //printf("%d\n", size); /* char name[] = "itcast"; char name2[] = {'0', '\0', '6'}; int size = strlen(name2); printf("%d\n", size); */ char name[] = "itcast"; //printf("%s\n", name); printf("%c\n", name[3]); return 0; }
#include <stdio.h> int main() { char name[] = "jack"; char name1[] = "rose"; char name2[] = "jim"; char name3[] = "jake"; char names[2][10]= {"jack", "rose"}; //printf("%s\n", names[0]); //printf("%c\n", names[0][3]); char names2[2][10] = { {'j', 'a', 'c', 'k', '\0'}, {'r', 'o', 's', 't', '\0'} }; return 0; }