Home  >  Article  >  Backend Development  >  What is the storage form of char type data in memory in C language?

What is the storage form of char type data in memory in C language?

青灯夜游
青灯夜游Original
2021-01-28 18:29:3116068browse

In C language, the storage form of char type data in memory is "ASCII code". In C language, putting a character constant into a character variable does not actually put the character itself into the memory unit, but puts the ASCII code corresponding to the character into the storage unit.

What is the storage form of char type data in memory in C language?

The operating environment of this tutorial: windows7 system, c99 version, Dell G3 computer.

In C language, the storage form of char type data in memory is "ASCII code".

Tutorial recommendation: "c language tutorial video"

c language char type

Character data type is character

1. Representation of character data

Character data is a character enclosed in single quotes . For example:
'a', 'b', '=', ' ', '?' are all legal character data.
In C language, character data has the following characteristics:

Character data can only be enclosed in single quotes, not double quotes or other brackets.

Character data can only be a single character, not a string. The

character can be any character in the character set. But after a number is defined as a character type, it cannot participate in numerical operations. For example, '5' and 5 are different. '5' is character data and cannot participate in operations.

Escape character

The escape character is a special character. The escape character starts with a backslash "\" and is followed by one or more characters. Escape characters have specific meanings that are different from the original meaning of the characters, so they are called "escape" characters.

Escape characters are mainly used to represent control codes that are difficult to express using ordinary characters.

##\\Backslash character"\"\'##\”Characters represented by 1 to 3 octal numbersCharacters represented by 1 to 2 hexadecimal digits

Commonly used escape characters and their meanings

Escape characters

Meaning of escape characters

ASCII code

\n

Enter and line feed

10

\t

Jump horizontally to the next tab position

9

\b

Backspace

8

##\ r

Enter

13

\f

Paper feed

12

##92

Single quote character

39

Double quote character

34

##\a
ring

7

##\ddd

\xhh

2. Character type specifier

The type specifier of a character variable is char. The format and writing rules defined for character variable types are the same as those for integer variables. For example:
char a,b;

3. The storage form and usage of character variables in memory

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

For example, the decimal ASCII code of x is 120, and the decimal ASCII code of y is 121. Assign 'x' and 'y' values ​​to character variables a and b:
a='x';
b='y';
actually stores the binary codes of 120 and 121 in units a and b:
Example: Assign an integer to a character variable.

#include<stdio.h>
int main(void)
{
    chara,b;
    a=120;
    b=121;
    printf("%c,%c\n",a,b);
    printf("%d,%d\n",a,b);
    return0;
}

In this program, a and b are defined as character types, but integer values ​​are assigned in the assignment statement. From the results, the output form of a and b values ​​depends on the format character in the printf function format string. When the format character is "c", the corresponding output variable value is the character character. When the format character is When "d" is used, the corresponding output variable value is an integer.

It can be seen from this:

(1) The lowercase ASCII code is 32 larger than the uppercase ASCII code

(2) The following ASCII code is larger than the previous ASCII code

(3) To change '3' to 3, you need '3'-'0'= 3

4. String

A string is a sequence of characters enclosed by a pair of double quotes. For example: "CHINA", "C program", "$12.5", etc. are all legal strings.

Strings and characters are different. The main differences between them are as follows:

Characters are enclosed in single quotes, and strings are enclosed in double quotes. bracketed. The

character can only be a single character, while a string can contain one or more characters.

You can assign character data to a character variable, but you cannot assign a string to a character variable.

characters occupy one byte of memory space. The number of memory bytes occupied by a string is equal to the number of bytes in the string plus 1. The character "\0" (ASCII code is 0) is stored in the added byte. This is the end of string sign.

For example, the bytes occupied by the string "C program" in memory are: 10 bytes

Character 'a' Although both have only one character and the string "a", the situation in the memory is different.
'a' occupies one byte in memory, "a" occupies two bytes in memory,

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of What is the storage form of char type data in memory in C language?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn