[C]字符串读取,遇0结束
#include <stdio.h> int main() { char a[] = "The quick brown fox jumps over the lazy dog."; a[4] = '\0'; //a[] = "The \0uick brown fox jumps over the lazy dog." //或者a[4] = 0; printf("%s#\n", a); //字符串读到0,即结束 return 0; } //所以结果为:The #
#include <stdio.h> int main() { // char a[] = "The quick brown fox jumps over the lazy dog."; // a[4] = '\0'; //a[] = "The \0uick brown fox jumps over the lazy dog." //或者a[4] = 0; char a[] = {'q', 'u', 'i', 'c', 'k', 'b', 'r', 'o', 'w', 'n'}; printf("%s#\n", a); //字符数组: 如果没有0,读到结尾, 输出:quickbrown# a[4] = 0; //'k'换成0; printf("%s#\n", a); //字符串遇到0,就结束.输出:quic# return 0; }
//思考题:输出结果为何? #include <stdio.h> int main() { char a[] = "The \0uick brown fox jumps over the lazy dog."; printf("%s#\n", a); return 0; }