C 的scanf数组奇怪问题??

#include <stdio.h>
#include <stdbool.h>


int main(void){

    bool a = true;
    printf("bool value is %d \n", a);

    char name[2];
    scanf("%s", &name[0]);

    printf("name is %s \n", name);
    printf("char is %s", name[5]);

}

我的那个字符串长度为2.为什么可以接受很长的字符串。

比如我运行的 时候输入helloworld 那么输出printf("name is %s \n", name); 可以输出helloworld 但是name[5]就会提示段错误??

阅读 2.7k
1 个回答

%s是一直输出到\0才终止intent:#Intent;S.K_1171477665=57AC7D74F5F3A45EE3A66AF7A1EF75E6B3866417FEE80745E115483E0A37CC72;end

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进