吾爱程序员:这里有好玩的游戏和软件
当前位置:首页C语言教程 → C中的转义序列

C中的转义序列

来源:网络 | 更新时间:2022-01-08 06:39:04
在 C 编程语言中,字符集中有 256 个字符。整个字符集分为两部分,即ASCII字符集和扩展ASCII字符集。但除此之外,还有一些不属于任何字符集的其他字符,称为 ESCAPE 字符。

转义序列列表

转义字符的一些编码示例

// C program to illustrate
// a escape sequence
#include <stdio.h>
int main(void)
{
printf("My mobile number "
"is 7a8a7a3a9a2a3a4a0a8a");
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 GeeksbbbbF");
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("Hellon");
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);
}

输出:(取决于编译器)

Hello52cxydh

解释:它包含两个转义序列意味着它在打印之后编译器读取下一个作为换行符,即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 52cxydhn");
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 = "A725";
printf("%s", s);
return 0;
}

输出:

A:5

解释:这里000是一到三个八进制数字(0….7)表示后面必须至少有一个八进制数字,最多三个。这里072是八进制,首先转换为十进制,即ASCII值字符':'。在 72 的地方有 : 并且输出是 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 = "Bx4a";
printf("%s", s);
return 0;
}

输出:

BJ

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

最新文章

热点资讯

手游排行榜

CopyRight 2020-2030吾爱程序员

鄂ICP备2021004581号-8

本站资源收集于网络,如有侵权请联系我们:35492删除0109@qq.com