C Language Basics Tutorial (my c tour started) [Six]_c language

Source: Internet
Author: User
Tags control characters printable characters
14. Basic Data type: Character type (top)
1. character type (char) Introduction
character type ( char) is used to store characters ( character), such as English letters or punctuation. Strictly speaking, Char is also an integer type , because the char type stores an integer instead of a character. The computer uses a specific integer encoding to represent a particular character. The commonly used encoding in the United States is ASCII(American Standard Code for Information Interchange US Information Interchange standard encoding). For example: ASCII uses 65来 to represent capital letter A, so storing the letter a actually stores an integer 65. Note: Many IBM mainframes use another encoding-EBCDIC (Extended binary-coded Decimal Interchange Code- Extended Binary Coded Decimal Interchange Code) The codes used by computers in different countries may be completely different.
The range of ASCII is 0 to 127, so 7 bits (bit) are sufficient to represent all ASCII. Char typically occupies a 8-bit memory unit, representing more than ASCII. Many systems offer extended ASCII(Extended ASCII), and the required space is still within 8 digits. Note that the encoding of extended ASCII provided by different systems may be different!
Many character sets are beyond the 8-bit range (such as the character set), and in a system that uses this character set as the basic character set , char may be 16-bit, or even 32-bit. In summary,C guarantees that the char footprint is large enough to store the encoding of the basic character set used by the system. The C language defines the number of digits of char for a byte (byte), so one byte may be 16 bits, or 32 bits, and not just 8 bits.
2. Declaring a character type variable
character variables are declared in the same way as other types of variables:
char good;
char better, best;
The above code declares three character variables: good, better, and best.
3. Character constants quantity and initialization
We can use the following statement to initialize a character variable:
char ch = ' A ';
This statement initializes the value of CH to the encoded value of a. In this statement, ' A ' is the constants quantity of the word. C language, the use of single quotes to bring the characters to form a character constants quantity. Let's look at another example:
char fail; / * Declare a character variable * *
Fail = ' F '; / * correct * *
Fail = "F"; * * wrong! "F" is a string constant * *
The third statement is incorrect because the character is enclosed in double quotes to form a string constant . We'll discuss the string in a later tutorial, and now put it down.
Because characters are essentially stored as numbers, we can initialize character variables directly using numbers, or assign values to character variables:
char ch = 65; * Bad style * *
In ASCII, the encoding of a 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, it may be any other character, so use numbers to initialize character variables, or give character variables a bad style, because portability is too bad! However, by using the character constants (for example, ' A ') to initialize the characters variable, or to assign a value to a character variable, the character variable will get the encoded value of the character we expect. For example:
char ch = ' A ';
CH can get the encoded value of character A, regardless of the system that uses any encoding. This is because the compiler automatically converts ' a ' to the encoded value of a. Therefore, we should use character constants to initialize character variables, or assign values to character variables instead of numbers.
Interestingly, C uses the int type to handle the character constants, not the char type. For example, in an ASCII system using 32-bit int, the following code
char ch = ' C ';
The encoded value of ' C ' is 67 stored in a 32-bit memory unit, but CH is still stored in a 8-bit memory cell, but its value becomes 67. Therefore, we can define the constants quantity of odd words like ' good '. Because the encoded value of each character occupies a 8-bit memory unit, this constant can just be stored in a 32-bit memory unit. However, the result is that the character variable can only get the last 8 digits of the constants amount of characters by initializing the character variable with this character constants, or by assigning a value to the character variable. In other words, the following code
char ch = ' good ';
CH Gets the value of ' d '.
later, in the absence of special instructions, we are talking about ASCII .
15. Basic Data type: Character type (medium)

non-printable characters ( nonprinting Characters )
Some ASCII characters are not printable. such as backspace , another row , alert , and so on. The C language provides two ways to represent this type of non-printing character .
The first approach is to use ASCII encoding. For example, in ASCII encoding, 7 is used to indicate an alert:
char beep = 7;
The second approach is to use a special sequence of symbols, known as escape characters escape sequences. See the following table:(
escape character meaning
\a Alert (Alert (ANSI C))
\b Backspace (Backspace)
\f Change page (Form feed)
\ n line Wrap (newline)
\ r Enter (carriage return)
\ t Horizontal tab (horizontal tab)
\v Vertical tab (Vertical tab)
\ n Reverse Ramp (backslash (\))
\ ' single quotation mark (quote ('))
\ double quotation mark (double quote ("))
\? Question mark (question mark (?))
Number of \0oo octal (octal value (o represents a octal number))
\XHH hexadecimal number (hexadecimal value (h represents a hexadecimal number))
When assigning a value to a variable, the escape character must be enclosed in single quotes. For example:
char nl = ' \ n ';
Let's learn more about the meaning of each transfer character.
\a(alerts) are added by ANSI C89 to produce audible or visual alerts. The effect that \a produces depends on the hardware . Generally speaking, output \a will produce a ringing . However, in some systems, the output \a does not produce any effect, or simply displays a special character. The standard clearly states that \a should not change the current active position (active position). The active position refers to the display device (monitor, typewriter, printer, etc.) showing the position of the next character. Take the display as an example, the active position is the position where the cursor is located, the output \a does not cause the cursor to move position.
\b, \f, \ n, \ r, \ t, and \v are output device control characters. Backspace ( \b) causes the active position of the current row to be backed up one position. The page break ( \f) causes the active position to jump to the beginning of the next page. Note: The page break can be used to control the printer's page change, but will not cause the PC's display to change pages. The newline character ( \ n) causes the active position to jump to the beginning of the next line. The carriage return ( \ r ) returns the active position to the beginning of the current row. The horizontal tab ( \ t) moves the active position several positions (usually 8). The Vertical tab ( \v) changes the active position to several rows. Note: \v can be used to control the printer for several lines, but will not cause the PC's display to wrap.
\ \, \, and \ enable us to use \, ' and ' as the Word constants quantity. If you want to print the following sentence:
"\ is called ' backslash '."
We need to use the following statement:
printf ("\" \ is called \ ' backslash\ '. \ "");
\0oo and \xhh are two special representations of ASCII code. If you want to represent characters with octal ASCII, you can precede the octal number with \ and then enclose it in single quotes. For example:
Beep = ' \007 '; / * \007 Representative \a * *
The leading 0 can be omitted, that is to say, ' \07 ' or ' \7 '. Regardless of whether or not the beginning of 0, 7 will be treated as octal number.
Starting with C89, C provides a way to denote the amount of word constants in hexadecimal: Write an x behind the backslash, and then write 1 to 3 hexadecimal digits. For example:
NL = ' \xa '; / * \xa rep \ n * *
Note: When using ASCII code, be aware that the ASCII code that distinguishes the number 4 is 52, and ' 4 ' represents the character 4, not the number 4. Furthermore, although ' \ n ' and ' \xa ', ' \a ' and ' \007 ' are equivalent, 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 a higher transplant. The latter is only valid for systems that use ASCII code. and numeric characters . For example: Character
16. Basic Data type: Character type (bottom)

One, character output
The printf function uses %c to represent output characters. Because the character is accessed as a 1-byte integer, the output is an integer if %d is used. For example:
/* This program output characters and the character of the integer encoding * *
#include <stdio.h >
int main (void)
{
Char ch;
printf ("Please enter a character.\n");
scanf ("%c", &ch); / * Enter a character by the user /*
printf ("The Code for%c is%d.\n", CH, ch);
return 0;
}
Please compile and execute this program to view its execution results. Remember to press ENTER after entering characters.
The printf function outputs the value of CH two times, the first time as a character output (because the format qualifier is%c) and the second time as a decimal integer (because the format qualifier is%d). Note: The format qualifier is only used to specify the output form of the data, not to specify how the data is stored.
Symbol of character type
In some compilers, char defaults to a signed (signed). For this type of compiler, the representation range for char is usually-128 to 127. In other compilers, char defaults to unsigned (unsigned). For this type of compiler, the representation range for char is usually 0 to 255. In general, the compiler's use instructions indicate whether it defaults to using char as signed or unsigned.
Starting with C89, we can use the keyword signed and unsigned to decorate char. So, regardless of whether the compiler default char is signed or unsigned, we can either represent the signed char with signed Char or an unsigned char with unsigned 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.