一路极易出错的笔试题2

一道极易出错的笔试题2
#include<stdio.h>
#include<string.h>
int main()

{

    char s[10]="love mm";

    s[0]=0;//等价于 s[0]='\0';等价于s[0]=NULL;

    printf("%s\n",s);

    return 0;

}

一路极易出错的笔试题2

输出结果为:


结果是为空的,不明白的话接着看程序

#include<stdio.h>
#include<string.h>
int main()
{
    char s[10]="Love mm";
    s[1]=0;
    printf("%s\n",s);
    return 0;
}

输出结果为: 

L

一路极易出错的笔试题2

因为%s遇到'\0'就停止输出了。


但是之后的字符还是存在的,看下面的程序:

#include<stdio.h>
#include<string.h>
int main()
{
    char s[10]="love mm";
    s[0]=0;
    printf("%c\n",s[1]);
    return 0;
}

一路极易出错的笔试题2输出结果为:

o

我们还是可以输出s[1]的内容。