C Advanced--continuation and escape characters 13

Source: Internet
Author: User

The meaning of the continuation character:
    • the continuation character (\) in the C language is a powerful tool to indicate the behavior of the compiler

Let's look at a case:

1#inch2 Clud3E <St4Dio.h>5 6 inch7 T M8 Ain (9 )Ten { One pri A ntf -     ( -     "Hello bupi\n" the     ) -     ; -  - ret +Urn0; -}

You may think that this is not a C-language code, but he does, but it is a large number of use of the continuation character, in reality do not use the continuation character, this is just an example.

One might ask, is it necessary to understand the continuation of a simple program that uses the continuation character to become such a mess? The answer is to understand

Pre-improvement Code:
1#include <stdio.h>2 3 #defineSWAP (b) {int temp = A; a = b; b = temp;}4 5 intMain ()6 {7     intA =1;8     intb =2;9     intc =3;Ten  One SWAP (A, b); A  -printf"A =%d, B =%d\n", A, b); -  the SWAP (b,c); -  -printf"B =%d, c =%d\n", B, c); -  +     return 0; -}

The third line is a block of macro code, which is the exchange of two variable values, but the code is so crowded in one line, the readability is not good.

After using the continuation character improvements:
1#include <stdio.h>2 3 #defineSWAP (A, B)4 {                      5     inttemp =A; 6A =b; 7b =temp; 8 }9 Ten intMain () One { A     intA =1; -     intb =2; -     intc =3; the      - SWAP (A, b); -      -printf"A =%d, B =%d\n", A, b); +      - SWAP (b,c); +      Aprintf"B =%d, c =%d\n", B, c); at      -     return 0; -}

The improved macro code block with the continuation character is significantly more readable.

Use of the continuation character:
    • The compiler rejects the backslash, and the character following the backslash automatically follows the previous line
    • In the continuation of the word, there can be no space after the backslash, the next line of the backslash can not have spaces
    • The continuation character is suitable for use when defining a macro code block

The meaning of the escape character: the C language escape character (\) is primarily used to denote a no-return display, or to represent regular characters
    • \ nthe carriage return line (the original meaning is the cursor line down)
    • \ t jumps horizontally to the next tab stop
    • \v Vertical Jump Lattice
    • \b Backspace
    • \ r Enter (the original meaning is the cursor back to the beginning of the bank)
    • \f Go paper Wrap
    • \ \ backslash character \
    • \ ' Single-quote character
    • \a Ringing Bell
    • \DDD the characters represented by one to three octal digits
    • \xhh the characters represented by one to two hexadecimal digits

Use of the transfer character: must appear between single or double quotes when backslash (\) is used as a transfer character

Case:

1#include <stdio.h>2 3 intMain ()4 {5     CharEnter ='\ n';6     7     Char* p ="\141\t\x62";8     9printf"%s", p);Tenprintf"%c", enter); One      A     return 0; -}

Summary:
    • The C-language backslash has both the continuation character and the transfer character.
    • Can appear directly in the program when used as a continuation character
    • When used as a transfer character, must appear between single or double quotation marks

C Advanced--continuation and escape characters 13

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.