Basic C language tutorial (my C journey started) [6]

Source: Internet
Author: User

14. Basic data type: balanced (on) 1. Introduction to character type (char)Balanced ( Char) Is used to store characters ( Character), Such as English letters or punctuation. Strictly speaking, char is actually an integer ( Integer type), Because the char type is actually an integer, not a character. A computer uses a specific integer to indicate a specific character. Which of the following codes is commonly used in the United States? ASCII(American Standard Code for Information Interchange American Standard Code ). For example, ASCII uses 65 to represent uppercase letters A. Therefore, the storage letter A actually stores an integer 65. Note: many IBM mainframes use another encoding, the Extended Binary-Coded Decimal Interchange Code, which is a Decimal Interchange Code ); computers in different countries may use different codes. ASCII ranges from 0 to 127, so 7 bits are enough to represent all ASCII. Char generally occupies 8-bit memory units, indicating more than enough ASCII characters. Many systems provide ExtensionASCII(Extended ASCII), and the required space is still within 8 bits. Note that the extended ASCII encoding methods provided by different systems may be different!Many Character SetBeyond the 8-bit representation range (such as the Chinese Character Set), use this character set Basic Character SetIn the system, char may be 16-bit or even 32-bit. In short, C ensures that the space occupied by char is sufficient to store the space used by the System Basic Character SetEncoding. The C language defines the number of bits in a byte as the number of bits in char. Therefore, a single byte may be 16 or 32 bits, not only 8 bits. 2. Declare struct VariablesThe declaration of struct variables is the same as that of other types of variables: char good; char better, best; the above Code declares three struct variables: good, better, and best. 3. Character constants and initializationWe can use the following statement to initialize the character variable: char ch = 'a'; this statement initializes the ch value to the encoding value of. In this statement, 'A' is Character constant. In C, single quotes are used to enclose character constants. Let's take another example: char fail;/* declare a character variable */fail = 'F';/* correct */fail = "F";/* error! "F" is a String constant */double quotation marks are used to enclose characters. String constantSo the third statement is incorrect. We will discuss the string in subsequent tutorials. Now let it go. Because the character is stored as a number, we can use a number to initialize the character variable, or assign a value to the character variable: char ch = 65; /* bad style */in ASCII, the encoding is 65, so for systems using ASCII, this statement is equivalent to char ch = 'a ';. In A non-ASCII system, 65 is not necessarily A, but may be any other character. Therefore, A number is used to initialize character variables, or it is a bad style to give character variables, because the portability is too bad! However, you can use A character constant (such as 'A') to initialize A character variable or assign A value to the character variable. The character variable must obtain the encoding value of the expected character. For example, char ch = 'a'; In any encoding system, ch can get the encoding value corresponding to character. This is because the compiler automatically converts 'A' to the encoding value corresponding to. Therefore, we should use character constants to initialize character variables, or assign values to character variables, rather than numbers. Interestingly, C uses the int type to process character constants, rather than the char type. For example, in an ASCII system that uses a 32-bit int, the following code uses char ch = 'C'; the encoding value 67 of 'C' is stored in a 32-Bit Memory Unit; however, ch is still stored in 8-bit memory units, but its value is changed to 67. Therefore, we can define odd character constants like 'good. Because the encoding value of each character occupies 8-bit memory units, this constant can be stored in 32-bit memory units. However, if you use this character constant to initialize the character variable or assign a value to the character variable, the result is that the character variable can only get the last eight digits of the character constant. That is to say, the following code char ch = 'good'; ch gets the value of 'D. In the future, we will discuss ASCII in the absence of special instructions. .
15. Basic data type: balanced (medium)

Non-printable characters (Nonprinting Characters)Some ASCII characters cannot be printed. For exampleReturn,Start another line,Alert. The C language provides two methods to express thisNon-printable characters. The first method is ASCII encoding. For example, in ASCII encoding, 7 is used to indicate the alarm: char beep = 7; the second method is to use a special symbol sequence, which is calledEscape charactersEscape sequences). See the following table:(Escape charactersDescription\ A Alert (ansi c) \ B Backspace \ f Form feed \ n Newline \ r Carriage return) \ t Horizontal tab \ v Vertical tab \ Backslash (\) \ 'single quote (')) \ "Double quotes ("))\? Question mark (?) ) \ 0oo Octal digit (Octal value (o represents an Octal digit) \ xhh Hexadecimal digit (Hexadecimal value (h represents a Hexadecimal digit )) when assigning values to variables, escape characters must be enclosed in single quotes. For example: char nl = '\ n'; Next we will learn in detail the meaning of each transfer character.\(Alert) is added by ANSI C89 to generateAudibleOrVisualizedAlarm. \ A resultsDepends on hardware. In general, output \ a will generateBEEP. However, in some systems, output \ a does not produce any effect, or only displays a special character. The standard clearly states that \ a should not change the currentActive location(Active position). The active position refers to the position where the display device (display, typewriter, printer, etc.) displays the next character. Taking a display as an example, the active position refers to the position of the cursor, and the output \ a will not cause the cursor to move.\ B,\ F,\ N,\ R,\ T, And\ VAll are output device controllers. Return sign (\ BReturns the active position of the current row. Page feed (\ F) Enables the active position to jump to the beginning of the next page. Note: The tab feed can be used to control the printer page feed, but it does not lead to a screen feed on the PC. Line feed (\ N) Enables the active position to jump to the beginning of the next line. Carriage Return (\ RReturns the beginning of the current row to the active position. Horizontal tab (\ T) To move several active locations (usually eight ). Vertical tab (\ V) To replace several rows with the active bit. Note:\ VCan be used to control the printer for several lines, but will not cause the display line feed of the PC.\\,\', And\"So that we can\,'And"Used as a character constant. To print the following sentence: "\ is called 'backslash '. "We need to use the following statement: printf (" \ "\ is called \ 'backslash \'. \"");\ 0ooAnd\ XhhIt is two special forms of ASCII code. If you want to use the octal ASCII code to represent characters, you can add\And then enclosed in single quotes. For example, beep = '\ 007 ';/* \ 007\ */The 0 headers can be omitted, that is, the values written as '\ 07' or '\ 7' are the same. If the value is 0 or 7, it is treated as an eight-digit number. Starting from C89, C provides the method of representing character constants in hexadecimal notation: Write an x behind the anisy bar, and then write 1 to 3 hexadecimal numbers. Example: nl = '\ xa ';/* \ Xa\ N */Note: When using ASCII codes, be sure to differentiateThe ASCII code of Number 4 is 52, and '4' indicates character 4, not number 4. In addition, although '\ n' and' \ xa ',' \ a' and '\ 007' are equivalent, however, we should try to use '\ n' and' \ a' instead of '\ xa' and '\ 007 '. This is because the former is easy to understand, easy to remember, and more portable. The latter is only valid for systems that use ASCII codes.AndNumeric characters. Example: character

16. Basic data type: dense (lower)

I. character outputPrintf function usage% CThe output character. Because the characters are accessed in the form of a 1-byte integer% DThe output is an integer. For example:/* This program outputs characters and the integer encoding of the characters */# include<Stdio. h>Int main (void) {char ch; printf ("Please enter a character. \ n "); scanf (" % c ", & ch);/* enter a character */printf (" The code for % c is % d. \ n ", ch, ch); return 0;} compile and execute this program to view the execution result. Enter the characters and press Enter. The printf function outputs the ch value twice. The first time it is output in the form of a character (because the format Delimiter is % c ), the second output is in the form of a decimal INTEGER (because the format Delimiter is % d ). Note: The format Delimiter is only used to specify the data output format, rather than how data is stored.2. Character Type symbolsIn some compilers, char defaultsSigned(Signed ). For this type of compiler, the char represents a range of-128 to 127. In other compilers, char defaultsUnsigned(Unsigned ). For this type of compiler, char usually ranges from 0 to 255. In general, the usage instructions of the compiler indicate that it regards char as signed or unsigned by default. Starting from C89, we can use the signed and unsigned keywords to modify char. In this way, no matter whether the compiler defaults to char to be signed or unsigned, we can use signed char to represent a signed char, or unsigned char to represent a signed char.

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.