C语言字符串刷题

碰到一个很简单的题,但不明白其中的道理。求大佬指点。
设char *p=″abcdABCD″;, 则执行printf(″%s″, p+4); 后输出结果是_________。
答案是ABCD.
我不明白的是为什么把A到剩下的所有字符都输出来了? 我知道p+4代表A的地址.

阅读 2k
2 个回答

看一下 printf%s 是怎么工作的吧:

The argument must be a pointer to the initial element of an array of characters. Precision specifies the maximum number of bytes to be written. If Precision is not specified, writes every byte up to and not including the first null terminator. If the l specifier is used, the argument must be a pointer to the initial element of an array of wchar_t, which is converted to char array as if by a call to wcrtomb with zero-initialized conversion state.

那你执行 printf(″%s″, p); 为啥会把 abcdABCD 全部输出?

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