[C language] Section 1 The performance of basic data

1. Data representation

There are two forms of data: constants and variables

  1. Constant

<1>Definition: During the running of the program, the value cannot be changed. The constant is the constant in mathematics. In the program, there is int a =100;float b=123.4; where 100 and 123.4 are constants.

<2>The commonly used constants are as follows:

1. Integer constants: such as 10010, 1234, 0, -123, etc. are integer constants

2. Real constants: decimal form (for example: 123.3) and exponential form (for example: 12.34e3), since the computer output cannot express the upper and lower angles, then It is stipulated that e or E is used to represent the exponent with base 10, where there must be a number in front of e or E, and an integer must be behind

3. Character constant:

<1> Ordinary characters: characters enclosed by apostrophes, such as ‘a’ ‘D’ ‘3’ ‘#’ cannot be ‘ab’ ’12’, but only inside apostrophes It is a character, and the character constant is stored in the binary form of ASCLL code in computer storage

<2>Escape character: starting with “”, means to convert the character after “” into another meaning, such as n in \
does not represent n, and the conversion is changed Another meaning is “line break” character

4. String constants: Enclose multiple characters with double apostrophes, and string constants are all characters within the double apostrophes, excluding the double apostrophes themselves ( For example: “ASD”, representing the string ASD)

5. Symbolic constants: Use #define directives such as #define PI 3.14 to indicate that starting from this line, all PI will be replaced with 3.14, and there is no semicolon at the end of this sentence Yes, symbol constants do not occupy memory, and this symbol will not exist after precompilation.

  1. variable

<1>. Definition: A variable represents a storage unit with a name and a specific attribute. It is used to store data, that is, to store the value of the variable. In the program During the running process, the value of the variable can be changed

<2>Description: Variables must be newly defined before use. When defining variables, specify the type and name of the variable (for example: int a; a = 12; where a represents the variable name, 12 represents the variable value), the variable name is actually a storage unit represented by a name, when compiling and linking the program, the editor will assign a memory address to each variable name; getting the value from the variable is actually Find the corresponding memory address through the variable name, and read data from the storage unit.

3. Constant variables

<1>. Definition: When defining a variable, add a keyword const in front (for example: const int a =3;), defined as a variable, in the variable During its existence, its value cannot be changed.

<2>The similarities and differences between constant variables and variables: constant variables have the basic properties of variables, have types, occupy storage units, but are not allowed to change their values.

<3>The difference between constant variables and symbolic constants (define)

#define PI 3.1 //Character constant
const int PI = 3.14;//Constant variable

<1> Character constants are only replaced by characters during precompilation. After precompilation, character constants do not exist (full replacement is 3.14). The names of symbol constants do not allocate storage units, but constant variables need to allocate storage spatial.

[Supplementary] Identifier

Naming rules: It can only be composed of numbers, letters, and underscores, and can only start with letters or underscores. The identifier cannot be a keyword in the C language, and it is case-sensitive.

2. Data type

When defining a variable, it is necessary to specify the type of the variable. The constant is also distinguished by type. The data is stored in the storage unit. The storage unit is composed of a limited number of bytes. The range of data stored in each storage unit is limited; The so-called type refers to the arrangement of data allocation storage units, including the length of the storage unit (how many bytes it occupies) and the storage form of the data. Different types allocate different lengths and storage forms

  1. Integer data

<1>Basic shaping (int)

(1) Generally occupy two or 4 bytes (32 bits) (determined by the editor), one byte is 8 binary numbers, that is, eight bits

(2) The storage method in the storage unit: it is generally stored in the form of the complement of an integer. The complement of a positive number is its binary form, and the complement of a negative number is the inversion of its binary form and plus 1 (the next article introduces the original code complement code inverse code)

(3) Format placeholder: %d

<2>short int

(1) The type named short int or short generally allocates 2 bytes (16 bits)

(2) Storage method: the same as int, the value range of two bytes is -32768~32767

(3) Format placeholder: %hd

<3>Long integer (long int)

(1) Type name: long int or long, generally allocated 4 bytes (32 bits)

(2) Format placeholder: %ld

<4>double long integer (long long int)

(1) Type name: long long int or long long, generally allocated 8 bytes (64 bits), which is a new type in c99, but many c language compilation systems have not implemented it

[Supplement]. Storage unit length

The c standard requires: sizeof(short)<=sizeof(int)<=sizeof(long)<=sizeof(long long), sizeof() is an operator for measuring the length of a type or variable (seeking byte operator)

  1. Symbol attributes of integer variables

The variable value is stored in the form of complement in the storage unit. The first binary system in the storage unit represents the sign. The range of the integer variable value includes negative numbers to positive numbers. Generally, the signed ones are [-a, a ], the unsigned is [0,2a]

<1>unsigned modifier

(1) In order to make full use of the scope of the variable, the variable can be defined as an unsigned type, and the modifier unsigned can be added in front of the data type to specify that the variable is an “unsigned integer”. If signed, it means a signed type , (the sign refers to the negative sign), if there is no modifier, it defaults to signed (for example, signed int a is equivalent to int a), if it is specified as unsigned, all binary bits in the storage unit are used to store the value itself, Without a sign, an unsigned variable cannot store negative numbers

<2>signed modifier

(1) The highest bit of signed modifier integer data in the storage unit represents the sign of the value (0 is positive, 1 is negative), and the other bits are used to store variable values

For example: the value range of a signed character type (char is eight bits of a byte) is -128-127 because the highest bit of a signed character type is a sign bit, 0 represents a positive number; 1 represents a negative number. The maximum value of a signed character type is 01111111, and this binary value is equal to 2^0^2^1+2^2+2^3+2^4+2^5+2^6=127. For unsigned, the maximum value is 11111111, which is 255

[Description]:

(1). Only integer data (including character type) can add signed and unsigned modifiers, and real data (floating point numbers) cannot be added

(2). Symbol placeholder: %u

(3). Negative values cannot be assigned to variables modified by unsigned, otherwise an error will occur

For example:

unsigned short a =-1;
printf("%d",a);
/*
First write the complement of a as 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 and store it in a (refer to the conversion of the inverse of the source code)
Since a is an unsigned type, the highest bit also represents a numerical bit, and it is converted to a decimal output bit 65535
*/

3. Character data

Since characters are stored in the form of their codes (integer), c99 regards character data as a type of integer, but character data has its own characteristics in use

Character and character code

<1> Characters and character codes are not arbitrary characters, they can be recognized by programs, and they are all system character sets. Most systems use the ASCLL character set, which basically contains 127 characters

  1. Letters: a-z A-Z

  1. Numbers: 0-9

  1. Special symbols: 29, including ! ” & amp; ‘ ( ) * + , – / : ; < = > ? [ \ ] ^ _ ` { | } ~

  1. Space character: space, tab, vertical tab, line feed, form feed

  1. Symbols that cannot be displayed: empty (null) character (indicated by ‘\0’), warning (\a), backspace (\b), carriage return (\r)

[Explanation]: The maximum ASCLL code is 127 (some systems have extended 128-255 bits), which can be represented by 7 binary numbers (when ASCLL is 127, the binary number is 1111111, and all 7 bits are 1), so in the c language, Specifies to use a byte (8 bits) to store a character, and the first position in the byte is 0

[Example] The integer 1 and the character ‘1’ are different concepts. The character ‘1’ only represents a character with the shape of ‘1’, which occupies one byte. When needed, it is output as it is and stored in the memory in the form of ASCLL code. , while the integer 1 is stored in the form of an integer, stored in the form of two’s complement, occupying 2 or 4 bytes

Character variables

A character variable is a character variable defined with the type specifier char, for example char a = ‘c’; means to define a character variable a and assign it the value ‘c’, the character is enclosed in single quotes

<1> char a = ‘c’, a is a character variable, essentially a byte integer variable, it is generally used to store characters, so it is called a character variable , but you can assign an integer between 0-127 to a character variable

printf("%d,%c",'a','a');
// Output a character a as a decimal integer and a character
//%c represents a character placeholder, output in the form of characters
//Output in integer, corresponding to the number of ASCLL code, the ASCLL code corresponding to a is 65 code
//The output result is: 65, a

<2>Storage of character variables in memory

Convert its corresponding ASCLL code into binary form for storage

For example: the ASCLL code of the character ‘i’ is 49, because the ASCLL code corresponding to the character is an integer, its complement is its binary number, and the corresponding binary number stored in memory is 0011 0001 (1 byte)

<3>Storage space and value range of character data

[Description]

When using a signed character variable, the allowed stored value is -127~128, but the code of the character cannot be negative, so only the part 0~127 is actually used

floating point data

Floating-point data is used to represent real numbers with decimals. In C language, real numbers are stored in storage units in the form of exponents.

Because the position of the decimal point can be changed () floating, and then the index increases and decreases, so it is called a floating point number

<1>Floating-point type: float (single-precision floating-point number), double (double-precision floating-point number), long double (long double-precision floating-point number)

<2>float type: occupies 4 bytes (32 bits), and the value is stored in the storage unit in the form of a standard binary number exponent. When storing, the real type is divided into two parts: decimal and exponent. Storage, the number before the decimal point in the decimal part is 0

  • Sign bit (Sign): 0 means positive, 1 means negative

  • Exponent (Exponent): Used to store exponent data in scientific notation, and use shift storage

  • Mantissa: fractional part

<3>Placeholder: %f

<4>float data can be accurate to 6 decimal places

<5>Double type: In order to expand the range of data, use 8 bytes to store double type and get 15 significant figures. Convert to double type

[Description]

In C language, when outputting double type (double-precision real type) and float type (single-precision real type), 6 decimals are output by default (less than six digits are filled with 0, and more than six digits are rounded and truncated).

If you want 16 digits after the decimal point, write it as %.16lf, which will not be rounded automatically. Double is an approximate value, and there is usually no way to do it precisely. Usually it can be accurate to 5 or 6 digits after the decimal point, that is to say, if it exceeds 5 or 6 digits, it may not be allowed

<6> Floating-point constants: All real numbers that appear in the form of decimals or exponents are floating-point constants, which exist in the form of exponents in memory, and the c editor treats floating-point numbers as double Type processing, allocate 8 bytes

float a =3.1415//The system handles it as double
float b =3.1344f//The system processes with single precision

Note: Each variable has a definite type. The variable occupies memory and is a concrete entity. Data can be stored in the storage unit it occupies, but the type is abstract, does not occupy the storage unit, and cannot be used store data

int a; a = 3;//Correct, a is an int variable that stores data
int a =3//error, the type cannot be assigned because it does not occupy memory space