C-language character data (characters)

Source: Internet
Author: User

Character data is a character.

Representation of the character type data

Character data is one character enclosed in single quotation marks. For example:
' A ', ' b ', ' = ', ' + ', '? '
Are all valid character data types.

In the C language, character data has the following characteristics:

    • Character data can only be enclosed in single quotes, not double quotes or other parentheses.
    • Character data can only be a single character and cannot be a string.
    • The character can be any character in the character set. But numbers are defined as character types and cannot participate in numerical operations. such as ' 5 ' and 5 are different. ' 5 ' is a character-type data and cannot participate in the operation.
Escape character

The escape character is a special character. The escape character begins with a backslash "\" followed by one or several characters. The escape character has a specific meaning, which differs from the original meaning of the character, so it is called an "escape" character. For example, the "\ n" in the format string of the printf function in the previous examples is an escape character, meaning "carriage return". Escape characters are primarily used to denote control codes that are not easily represented by ordinary characters.

commonly used escape characters and their meanings
escape characters escape character meaning ASCII code
\ n carriage return line ten
\ t horizontal Skip to next tab position 9
\b backspace 8
\ r Enter +
\f paper Wrapping n
\ backslash "\" 92 /td>
\ ' single-quote 3
\ " double-quote" 4
\a ringing Bell 7
\ddd The characters represented by the octal digits  
\xhh The characters represented by hexadecimal digits  


Broadly speaking, any one of the characters in the C-language character set can be represented by an escape character. The \DDD and \xhh in the table are presented for this purpose. DDD and HH are octal and hexadecimal ASCII codes, respectively. If \101 represents the letter "A", \102 denotes the letter "B", \134 represents a backslash, \xoa represents a newline, etc.

Use of the "example 3-8" escape character.

  1. #include<stdio.h>
  2. int main(void){
  3. int a, b, C;
  4. A=5; b=6; c=7;
  5. printf("ab C\ tde\ rf\ n");
  6. printf("Hijk\ tL\bM\ n");
  7. return 0;
  8. }
Character variables

The type specifier for a character variable is char. Character variable type definitions have the same formatting and writing rules as Integer variables. For example:
Char A, B;

How to store character variables in memory and how to use them

Each character variable is allocated a byte of memory space, so only one character can be stored. The character value is stored in the memory unit of the variable in the form of an ASCII code.

such as X's decimal ASCII code is 120,y's decimal ASCII code is 121. Give the ' x ' and ' Y ' values to the character variable A, B:
A= ' x ';
b= ' Y ';
In fact, the binary code for 120 and 121 is stored in A and B two units:


So you can also think of them as integer quantities. 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 be output as an integer, and the integer amount is also allowed to be output as a character quantity.

The integer amount is two bytes, the character is a single byte, and when the integer quantity is processed by character type, only the low eight bytes are involved in processing.

Example 3-9 assigns an integer to the character variable.

  1. #include<stdio.h>
  2. int main(void){
  3. Char a, b;
  4. A=+;
  5. b=121;
  6. printf("%c,%c\ n", a, b);
  7. printf("%d,%d\ n", a, b);
  8. return 0;
  9. }


This program defines a A, B is a character type, but is assigned an integer value in an assignment statement. From the results, the output form of a/b value depends on the format character in the printf function format string, when the format character is "C", the corresponding output of the variable value is a character, when the format symbol is "D", the corresponding output of the variable value is an integer.

"Example 3-10"

  1. #include<stdio.h>
  2. int main(void){
  3. Char a, b;
  4. A=' a ';
  5. b=' B ';
  6. A=a-32;
  7. b=b-32;
  8. printf("%c,%c\%d,%d\ n", a, b, a, b);
  9. return 0;
  10. }


In this case, A, B is described as a character variable and given a character value, and the C language allows character variables to participate in numeric operations, that is, the ASCII code of the characters to participate in the operation. Because the ASCII code of the uppercase and lowercase letters is 32 apart, the lower case letters are converted to uppercase after the operation. Then output in integer and character type respectively.

String

A string is a sequence of characters enclosed by a pair of double quotation marks. For example: "China", "C program", "$12.5" and so on are all valid strings. Unlike strings and characters, there are essentially the following differences between them:

    • Characters are enclosed in single quotation marks, and strings are enclosed in double quotation marks.
    • A character can be a single character only, and a string may contain one or more characters.
    • You can assign a character data to a character variable, but you cannot assign a string to a character variable. There is no corresponding string variable in the C language, i.e. there is no such keyword, and a variable is declared as a string. However, a character array can be used to hold a string, which is described in the array chapter.
    • Character occupies a single byte of memory space. The number of bytes in memory is equal to the number of bytes in the string plus 1. Adds a byte that holds the character "s" (ASCII code is 0). This is the flag of the end of the string.


For example, the string "C program" takes up bytes in memory:


The character ' a ' and the string "a" are all only one character, but the situation in memory is different.

' A ' occupies one byte in memory and can be expressed as:


"A" occupies two bytes in memory and can be expressed as:

C-language character data (characters)

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.