c - C:返回 D 作为输出,为什么?
问题描述
#include <stdio.h>
int main(){
char *s="hello world";
printf("%c\n",s);
}
我用 C 编写了一个小代码。在此代码的最后一条语句中,我%c
在函数中使用格式说明符printf()
并分配其中命名s
的指针。它给我D
作为输出。它是返回垃圾值还是我的代码自动在其中分配 ASCII 值或其他东西?
当我添加s+1
它时返回E
和s+2
返回F
等等。谁能澄清我?
解决方案
%c
打印 a char
,而不是字符串。由于您的变量s
不是 a char
,而是指向 a 的指针char
,printf
因此将字符串的地址解释为字符。您可能想要使用其中一个片段:
printf("%c\n", s[0]); // print the first character in your string
printf("%s\n", s); // print the whole string
推荐阅读
- javascript - 如何在Javascript中修改克隆的对象数组中的对象值
- unit-testing - 我如何用 Jasmine 和 Karma 覆盖承诺响应
- python - 将二维列表转换为字符串
- python - 在 python 中有效地对不同输入(不同大小)的函数进行基准测试
- json - TPT12109:Export Operator 不支持 JSON 列。除了 bteq 导出之外,还有其他方法可以导出涉及 json 列的结果吗?
- excel - 日期格式无法识别
- javascript - javascript函数不执行,仅在控制台中有效
- apache-flink - 使用广播状态使用虚假消息强制关闭窗口
- angular - 如何从 Go HTTP Server 提供 Angular 应用程序?
- database - 从表中获取最大值并按列分组,该列具有不同顺序的逗号分隔值或具有更多值