c - 有人可以解释一下为什么当“字符串”与“%d” fromat 说明符一起使用时,它会给出奇怪的输出
问题描述
这是程序:
#include <stdio.h>
main()
{
printf("%d", "A"); // Can i know what the output from "printf" even means why the output is so strange
} // this outputs: "4214884" in my compiler
如您所见,输出是如此奇怪,你们中的任何人都可以向我解释一下。这是未定义的行为吗?这种行为是否在C 标准中的任何地方描述,所以我可以阅读它
解决方案
是的,这是 未定义的行为。%d
期望参数是一个整数,这里你传递的是字符串文字的第一个元素的地址,它是一个指针类型。
根据第 7.21.6.1/P9C11
章
[...]如果任何参数不是相应转换规范的正确类型,则行为未定义。
也就是说,对于托管环境,main()
应该是int main(void)
.
推荐阅读
- docker - 每次docker-compose时如何让docker拉最新版本
- android - 如何使android材料设计与android 4兼容
- angular - 角度形式的异步与同步
- google-sheets - 如何在 Google 表格中使用 ArrayFormula 计算总数和唯一性(基于 2 个单独的列)?
- reactjs - 如何更新 React setState() 方法中的列表?
- angular - 如何将 Ion Slide 设置为仅由 FIRST TIME 用户查看一次?
- swift - 如何快速获取 WKWebView 的 Post 请求正文
- react-native - 在 react-native 中使用 material-ui
- android - Build bundle 与 Generate Signed bundle apk 之间的主要区别是什么?
- r - KnitR R - SVG 未在 Word (docx) 中显示,但在 PDF 和 HTML 中显示