我有以下程序,我试图在其中了解\b转义序列的功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int disp(char *a)
{
return printf("%s", a);
}
int main(void)
{
char *s = "Hello\b\b";
printf(" %d\n", disp(s));
printf("%s %d\n", s, strlen(s));
return 0;
}
输出:
$ ./a.out
Hel 7
Hel 7
$
正如预期的那样Hello\b\b打印Hell,但strlen()返回 7 其中包括两个\b字符。
根据 C99 5.2.2\b定义如下:
\b (backspace) Moves the active position to the
previous position on the current line. If the
active position is at the initial position of
a line, the behavior of the display device is
unspecified.
如何\b在与字符串相关的函数中解释strlen()?和其他转义序列是否\b在编译时或运行时解析?