C-language character data (characters)

Source: Internet
Author: User

Representation of the character type data

Character data is a character.

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 character

Meaning of escape character

ASCII code

\ n

Carriage return line break

10

\ t

Jumps horizontally to the next tab position

9

\b

Backspace

8

\ r

Enter

13

\f

Go paper To change pages

12

\\

backslash character "\"

92

\‘

Single-Quote character

39

\”

Double quote character

34

\a

Ming Ling

7

\ddd

The character represented by the number of octal digits

\xhh

The character represented by a hexadecimal digit

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.

The use of escape characters.

1#include <stdio.h>2 intMainvoid){3     inta,b,c;4A=5; b=6; C=7;5printf"AB c\tde\rf\n");6printf"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.

Assigns an integer to the character variable.

1#include <stdio.h>2 intMainvoid){3     Charb;4A= -;5b=121;6printf"%c,%c\n", A, b);7printf"%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.

1#include <stdio.h>2 intMainvoid){3     Charb;4A='a';5b='b';6a=a- +;7b=b- +;8printf"%c,%c\n%d,%d\n", a,b,a,b);9     return 0;Ten}

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:

  

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.