\c 转义序列在 man 中列出但在 C 中未知

\c escape sequence listed in man but unkown in C

我正在学C,如果这道题看起来很简单或者是新手题,那你就知道为什么了。

阅读 printfman page 我发现 \c 列为一个 转义序列。 它的描述是

produce no further output

在此之前,我从未见过和听说过 \c。所以我决定在一个简单的 hello world 程序中尝试一下:

printf("\nHello World\n\n\c");

因此,gcc 给了我这个警告:

warning: unknown escape sequence: '\c' [enabled by default]

这对我来说听起来有点奇怪,所以我进一步调查了它:我去了 Wikipedia and \c wasn't listed as an escape sequence... Hence I tried searching around the web, and here on stack overflow. I've found very few references to \c (actually two) as discussed in this topic and this one(我认为后者与 C 并没有真正的关系,但我们似乎正在谈论"the same \c",阅读给出的描述)。 有人可以帮助我理解这件事吗?

没有阅读正确的手册页。你看的是:man 1 printf,这是关于shell命令printf,不是C标准函数printf.

使用:

 man 3 printf

阅读 C 库函数。 \c 不在 C 中,因此 printf(3) 无法识别它。

您正在查看的 printf(1) 确实按照记录工作。

$ /usr/bin/printf "ABC\chi"

产生:

ABC

请注意 Linux 手册页一般也可能有额外的 non-standard 扩展(特定于 Linux 或 glibc)和 POSIX 扩展等 Non-standard 扩展通常是这样记录的,但很容易被遗漏。因此,如果您正在寻找 C 标准所说的内容,那么您应该查看 C 标准。这是一个 online draft.

如果您想知道传递给 man 号码 是什么,那就是 部分 号码。 3对应库函数。您可以通过以下方式找到详细信息:man man。 以下是各部分的摘要:

   1   Executable programs or shell commands
   2   System calls (functions provided by the kernel)
   3   Library calls (functions within program libraries)
   4   Special files (usually found in /dev)
   5   File formats and conventions eg /etc/passwd
   6   Games
   7   Miscellaneous (including macro packages and conventions), e.g. man(7), groff(7)
   8   System administration commands (usually only for root)
   9   Kernel routines [Non standard]