Pointer and string-character type, escape character, string escape

Source: Internet
Author: User

Pointer and string-character type, escape character, string escape

Character Type

Char (character) is an integer and a special type: character. This is because

① Literal number of characters in single quotes: 'A', '1'

② ''Is also a character

③ Use % c in printf and scanf to input and output characters.

1 # include <stdio. h> 2 int main () 3 {4 char c; 5 char d; 6 c = 1; 7 d = '1'; 8 printf ("c = % d, d = % d, d = % c \ n ", c, d, d); 9 return 0; 10}
// Running result: c = 1, d = 49, d = '1'

The example shows that the character '1' is not equal to the integer 1. The character '1' is equal to the integer 49. Character is also an integer.

Character Input and Output

How to input the '1' character to char c?

Scanf ("% c", & c); ----> 1

Scanf ("% d", & I); c = I; ----> 49

The ASCII code of '1' is 49, so when c = 49, it indicates '1'

1 # include <stdio. h> 2 int main () 3 {4 char c; 5 int d; 6 scanf ("% c", & c); 7 printf ("c = % d, c = '% C' \ n ", c, c); 8 scanf (" % d ", & d); 9 printf (" d = % d, d = '% C' \ n ", d, d); 10 return 0; 11}
// Run the result: Enter 1
Output c = 49, c = '1'
Enter 49
Output d = 49, d = '1'

For the character input, refer to the scanf parameter. % c indicates that the input is the character; % d indicates the integer, and the corresponding character.

Hybrid Input

Int I; char c;

Scanf ("% d % c", & I, & c );

Scanf ("% d % c", & I, & c );

What's the difference?

1 # include <stdio. h> 2 int main () 3 {4 int I; 5 char c; 6 scanf ("% d % c", & I, & c ); // there is a space in the middle 7 printf ("I = % d, c = % d, c = '% C' \ n", I, c, c ); 8 scanf ("% d % c", & I, & c); // no space in the middle 9 printf ("I = % d, c = % d, c = '% C' \ n ", I, c, c); 10 return 0; 11}
// Running result:
There are spaces. Enter 1a, 1 a, and 1 a to correctly read integers and characters.
No space. Only 1a can correctly read integers and characters. 1 a, 1 a reads the integer 1 and space characters. Cannot read character.

Character Calculation  

1 # include <stdio. h> 2 int main () 3 {4 char c = 'a'; 5 c ++; 6 printf ("% c", c); 7 return 0; 8}View Code

Characters can be converted to integer operations, and the structure is converted to characters.

English letters are ordered in an ASCII table;

Uppercase and lowercase letters are separated and not separated.

So:

① 'A'-'A' can get the distance between two segments

② A + 'a'-a can change an upper-case letter to A lower-case letter.

③ A + 'a'-'A' can change A lowercase letter to an uppercase letter.

 

Escape characters

It is used to express control characters or special characters that cannot be printed. It starts with a backslash "\" and is followed by another character. These two characters are combined to form one character.

For example, printf ("Enter the feet and inches respectively." "For example, \" 5 7 \ "indicates 5 feet 7 inch :")

Character Meaning Character Meaning
\ B Move back to one grid \" Double quotation marks
\ T To the next table \' Single quotes
\ N Line feed \\ Backslash itself
\ R Enter    

Different terminal shell translations may be different. Generally, \ B is used to roll back a grid without deleting the \ t tab, which is used for alignment. \ n generally performs the line feed and carriage return actions.

 

Related Article

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.