【例2-6】 转义字符\a警报、\b退格、\t水平制表符、\r回车及\n的作用。
#include<stdio.h>
void main()
{
float height;
printf("\aPlease enter your height:_____cm\b\b\b\b\b\b\b");
scanf("%f", &height);
printf("\tYour height is %.2fcm.\rOh!\n",height);
}
建议初学者亲自运行感受。
程序运行的结果为:
Please enter your height: 180.5 cm
Oh! Your height is 180.50cm.
#include <stdio.h>
int main() {
printf("Hello,\nWorld!\n"); // 换行
printf("This is a tab:\tExample\n"); // 水平制表符
printf("Backslash: \\\n"); // 输出反斜杠
printf("Single quote: \'\n"); // 输出单引号
printf("Double quote: \"\n"); // 输出双引号
printf("Press Enter:\r"); // 回车符
printf("Beep:\a\n"); // 警报(可能在某些终端中不起作用)
printf("Vertical tab:\v\n"); // 垂直制表符
// 使用八进制和十六进制表示字符
printf("Octal example: \123\n"); // 八进制表示的字符
printf("Hexadecimal example: \x41\n"); // 十六进制表示的字符(A)
return 0;
}
程序运行结果为:
Hello,
World!
This is a tab: Example
Backslash: \
Single quote: '
Double quote: "
Beep: Enter:
Vertical tab:
Octal example: S
Hexadecimal example: A