【发布时间】:2023-04-07 15:11:02
【问题描述】:
忽略换行符之前字符串末尾的 C 字符串中的退格转义字符(在 Mac OS X 终端中)。
printf("hello, worl\bd"); // => hello word (OK)
printf("hello, world\b"); // => hello worl (OK)
printf("hello, world\b\n"); // => hello world\n (Why not hello worl\n ?)
为什么会这样?
【问题讨论】:
-
最好的解释请看这里:stackoverflow.com/a/6792880/2710409
-
@Joshpbarron - 感谢您的链接 - 我理解该链接示例中的行为,但在我的示例中,最后一行,\b 的存在似乎没有任何效果 - 也不应该“你好,世界\nd”还是“你好,世界\n”?
-
这已在实际答案中进行了解释,但只是为了完整性: \b 将光标向后移动一个字符,但不写任何内容。然后新行向下移动一行。我目前无法对此进行测试,但如果您使用 \r 而不是 \n,您可能会像预期的那样丢失 d。
-
@Joshpbarron :使用 \r 而不是 \n 删除(即不输出)整个字符串。
标签: c string format-string