📜  C中的转义序列

📅  最后修改于: 2021-05-25 21:53:16             🧑  作者: Mango

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

转义序列列表

转义字符的一些编码示例

// C program to illustrate
// \a escape sequence
#include 
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 
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);
}

输出:

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

输出:

Hello
GeeksforGeeks
// C program to illustrate
// \t escape sequence
#include 
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 GFG");
    return (0);
}

输出:

Hello   GFG

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

// C program to illustrate
// \v escape sequence
#include 
int main(void)
{
    // Here we are using \v, which
    // is vertical tab character.
    printf("Hello friends");
  
    printf("\v Welcome to GFG");
  
    return (0);
}

输出:

Hello Friends
Welcome to GFG
// C program to illustrate \r escape 
// sequence
#include 
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 
int main(void)
{
    // Here we are using \,
    // It contains two escape sequence 
    // means \ and \n.
    printf("Hello\\GFG");
    return (0);
}

输出:(取决于编译器)

Hello\GFG 

说明:它包含两个转义序列意味着它打印\编译后读取下\作为新行字符,即\ n,这个打印GFG中的下一行

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

输出:

' Hello Geeks
" Hello Geeks
// C program to illustrate
// \? escape sequence
#include 
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 
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 
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。

想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。