C Language Learning Tutorial Chapter II-Data types, operators, expressions (iv)

Source: Internet
Author: User
Tags character set empty integer numeric lowercase printf

Character Type Quantity

The character type includes the character constants quantity and the character variable.

Word Constants Quantity
Character constants is a character enclosed in single quotes. For example ' A ', ' b ', ' = ', ' + ', '? ' is the legal character constants quantity. In C language, the character constants quantity has the following characteristics:
1. Character constants can only be enclosed in single quotes, not in double quotes or other parentheses.
2. The character constants quantity can only be a single character, cannot be a string.
3. The character can be any character in the character set. But when the numbers are defined as character types,
Cannot participate in numeric operations. such as ' 5 ' and 5 are different. ' 5 ' is a constants amount of characters and cannot participate in operations.

The

Escape character
Escape character is a special constants amount of characters. The escape character begins with a backslash "\" followed by one or more characters. The escape character has a specific meaning, which is different from the original meaning of the character, so it is called the "escape" character. For example, the "\ n" used in the format string of a previous example printf function is an escape character, meaning "carriage return line wrapping." Escape characters are used primarily to represent control code that is not easily represented by generic characters.
commonly used escape characters and their meanings
escape character escape
\ n/a carriage feed
\ t landscape jumps to the next tab position
\v vertical jumps
\b Backspace
\ r return
\f take page change
\ backslash "\"
\ ' Single quotation mark
\a ring
\ddd the character
\xhh 1~2 hexadecimal number represented by the number of 1~3 digits
Broadly speaking, any character in the C language character set can be represented by an escape character. The \DDD and \xhh in table 2.2 were proposed for that purpose. DDD and HH are octal and hexadecimal ASCII codes respectively. such as \101 expression word? quot; A ", \102 represents the letter" B ", \134 represents a backslash, \xoa represents a newline, and so on. Use of the escape character
Void Main ()
{
int a,b,c;
a=5; b=6; c=7;
printf ("%d\n\t%d%d\n%d%d\t\b%d\n", a,b,c,a,b,c);
}
This program exercises the use of the escape characters
A, B, and C for integers 5->a,6->b,7->c
calls printf display program run results
printf ("%d\n\t%d%d\n%d%d\t\b%d \ n ", a,b,c,a,b,c);
The program after the first column output a value of 5 is "\ n", so the carriage return line change; then again "\ t", so skip to the next tab position (set the tab position interval is 8), then output B value 6, empty two and then output C value 7 and then "\ n", so return to the line; then empty Shige and then output a value of 5 , then empty Sange and Output B's value 6; again, "\ T" jumps to the next tab position (aligned with 6 on the previous line), but the next escape character "\b" returns a grid, so it's next to 6 and then outputting the C value 7.

The value of a

character variable
character variable is the constants quantity, which is a single character. The type descriptor for a character variable is char. The character variable type description has the same formatting and writing rules as an integer variable.
For example:
Char a,b; Each character variable is allocated a byte of memory space, so only one character can be stored. Character values are stored in the memory unit of the variable in ASCII form. such as X's
Decimal ASCII code is 120,y's decimal ASCII code is 121. Assign ' x ' and ' Y ' values to the character variable a,b: a= ' x '; b= ' y '; it actually holds 120 and 121 binary codes in a,b two units: a 0 1 1 1 1 0 0 0
B 0 1 1 1 1, 0 0 1
So you can also think of them as integral. The C language allows you to assign a character value to an integer variable, and also allows you to assign an integer value to a character variable. In the output, the character variable is allowed to output according to the integer amount, and the integer is also allowed to be output by the character quantity. The integer is two bytes, the character is a single byte, and when the integer is processed by character type, only the low eight-bit byte is involved.
Main ()
{
Char a,b;
a=120;
b=121;
printf ("%c,%c\n%d,%d\n", a,b,a,b);
}
A B
a <--
B <---121
Show program results

This program describes A,b as a character type, but assigns an integer value to the assignment statement. From the result, the output form of the A,b value depends on the format character in the printf function format string, and when the format character is "C", the variable value for the output corresponds to the character, and when the format character is "D", the variable value of the corresponding output is an integer.
void Main ()
{
Char a,b;
A= ' x ';
b= ' Y ';
a=a-32;
b=b-32;
printf ("%c,%c\n%d,%d\n", a,b,a,b);
}
A,b is described as a character variable and given a character value
Change lowercase letters to uppercase letters
Output in integer and character type
In this case, the a,b is described as a character variable and given a character value, and the C language allows the character variable to participate in the numeric operation, that is, the ASCII code of the character. Because the uppercase and lowercase ASCII code differs by 32, the lowercase letters are converted to uppercase letters after the operation. The integer and character outputs are then respectively.
[Practice]//charint a=49;
Char b;
Char D;
b=a+10;
d=a+b; ' Vtable
a,2,49
b,1, Random
d,1, Random
of Vtable
' Vupdate
1,49
2, Random
3, Random
2, '; '
3, ' l '
of Vupdate
of Practice
[Practice]//char c1,c2;
C1= ' a '; c2= ' B ';
c1=c1-32;c2=c2-32; ' Vtable
c1,1, Random
c2,1, Random
of Vtable
' Vupdate
1, random; 2, random
1, ' a '; 2, ' B '
1, ' A '; 2, ' B '
of Vupdate
of Practice

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.