1. 主页
  2. 文档
  3. C语言教程
  4. C语言基础教程
  5. C中的转义序列

C中的转义序列

在 C 编程语言中,字符集中有 256 个字符。整个字符集分为两部分,即ASCII字符集和扩展ASCII字符集。但除此之外,还有一些不属于任何字符集的其他字符,称为 ESCAPE 字符。

转义序列列表

转义字符的一些编码示例

// C program to illustrate
// \a escape sequence
#include <stdio.h>
int main(void)
{
printf("My mobile number "
"is 7\a8\a7\a3\a9\a2\a3\a4\a0\a8\a");
return (0);
}

输出:

My mobile number is 7873923408.
// C program to illustrate
// \b escape sequence
#include <stdio.h>
int main(void)
{
// \b - backspace character transfers
// the cursor one character back with 
// or without deleting on different 
// compilers.
printf("Hello Geeks\b\b\b\bF");
return (0);
}/* Your code... */

输出:

The output is dependent upon compiler.
// C program to illustrate
// \n escape sequence
#include <stdio.h>
int main(void)
{
// Here we are using \n, which
// is a new line character.
printf("Hello\n");
printf("52cxydh");
return (0);
}

输出:

Hello
52cxydh
// C program to illustrate
// \t escape sequence
#include <stdio.h>
int
main(void)
{
// Here we are using \t, which is
// a horizontal tab character.
// It will provide a tab space 
// between two words.
printf("Hello \t 52cxydh");
return (0);
}

输出:

Hello   52cxydh

转义序列“\t”在基于循环的图案打印程序中非常常用

// C program to illustrate
// \v escape sequence
#include <stdio.h>
int main(void)
{
// Here we are using \v, which
// is vertical tab character.
printf("Hello friends");

printf("\v Welcome to 52cxydh");

return (0);
}

输出:

Hello Friends
Welcome to 52cxydh
// C program to illustrate \r escape 
// sequence
#include <stdio.h>
int main(void)
{
// Here we are using \r, which
// is carriage return character.
printf("Hello fri \r ends");
return (0);
}

输出:(取决于编译器)

ends
// C program to illustrate \\(Backslash)
// escape sequence to print backslash.
#include <stdio.h>
int main(void)
{
// Here we are using \,
// It contains two escape sequence 
// means \ and \n.
printf("Hello\\52cxydh");
return (0);
}

输出:(取决于编译器)

Hello\52cxydh

解释:它包含两个转义序列意味着它在打印\之后编译器读取下一个\作为换行符,即\n,它在下一行打印52cxydh

// C program to illustrate \' escape
// sequence/ and \" escape sequence to
// print single quote and double quote.
#include <stdio.h>
int main(void)
{
printf("\' Hello 52cxydh\n");
printf("\" Hello 52cxydh");
return 0;
}

输出:

' Hello 52cxydh
" Hello 52cxydh
// C program to illustrate
// \? escape sequence
#include <stdio.h>
int main(void)
{
// Here we are using \?, which is 
// used for the presentation of trigraph
// in the early of C programming. But
// now we don't have any use of it.
printf("\?\?!\n");
return 0;
}

输出:

??!
// C program to illustrate \OOO escape sequence
#include <stdio.h>
int main(void)
{
// we are using \OOO escape sequence, here 
// each O in "OOO" is one to three octal 
// digits(0....7).
char* s = "A\0725";
printf("%s", s);
return 0;
}

输出:

A:5

解释:这里000是一到三个八进制数字(0….7)表示\后面必须至少有一个八进制数字,最多三个。这里072是八进制,首先转换为十进制,即ASCII值字符’:’。在 \072 的地方有 : 并且输出是 A:5。

// C program to illustrate \XHH escape 
// sequence
#include <stdio.h>
int main(void)
{
// We are using \xhh escape sequence.
// Here hh is one or more hexadecimal
// digits(0....9, a...f, A...F).
char* s = "B\x4a";
printf("%s", s);
return 0;
}

输出:

BJ

解释:这里 hh 是一个或多个十六进制数字(0….9, a…f, A…F)。\x 后面可以有多个十六进制数字。这里,’\x4a’ 是一个十六进制数,它是一个字符。首先它将被转换为十进制表示法,它是 char ‘J’ 的 ASCII 值。因此在 \x4a 的地方,我们可以写 J。所以输出是 BJ。

这篇文章对您有用吗?