C のすべての文字列は '\0' 文字で終わることを知っています。文字列がいつ終了するかを知る必要がある場合に非常に便利です。ただし、文字列を印刷する場合と、それなしで文字列を印刷する場合の使用法を理解することはできません。私は次のコードを持っています:-
/* Printing out an array of characters */
#include<stdio.h>
#include<conio.h>
int main()
{
char a[7]={'h','e','l','l','o','!','\0'};
int i;
/* Loop where we do not care about the '\0' */
for(i=0;i<7;i++)
{
printf("%c",a[i]);
}
printf("\n");
/* Part which prints the entire character array as string */
printf("%s",a);
printf("\n");
/* Loop where we care about the '\0' */
for(i=0;i<7&&a[i]!='\0';i++)
{
printf("%c",a[i]);
}
}
出力は次のとおりです。
hello!
hello!
hello!
私は違いを理解することができません。説明はありますか?