C ++ (1): Data Type, Data Type

Source: Internet
Author: User

C ++ (1): Data Type, Data Type
A computer processes data, and data exists in a specific form (such as integers, floating-point numbers, and characters ). There are often some relationships between different data (for example, an integer array composed of several integers ). The data structure refers to the data organization form. For example, an array is a data structure. Different computer languages allow different data structures. To handle the same type of problem, the algorithms are different if the data structure is different. For example, sorting by 10 integers is different from sorting by an integer array containing 10 elements.

Figure 1.1 data types available for C ++
The data in C ++ includes constants and variables. Both constants and variables have types. The preceding data types can also form a more complex data structure. For example, you can use pointers and struct types to construct complex data structures such as tables, trees, and stacks.

Table 1.1 number of bytes and value range of numeric and numeric data

C ++ does not specify the precision, value range, and the number of bytes occupied in the memory of all types of data. C ++ compilation systems make arrangements based on their own circumstances. Table 1.1 lists Visual C ++ numeric and numeric data.

1) integer data can be divided into long int, General int, and short int ). Add long and short before int to indicate long and short integers respectively.

2) the storage of integer data is in the binary format. For example, if the binary format of the decimal integer 85 is 1010101, the storage format in the memory is shown in.

0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1
3) in front of the int and char Types, you can add the modifier signed (indicating "signed") or unsigned (indicating "unsigned "). If it is specified as signed, the value is stored as a complement, and the highest bit in the storage unit is used to represent the symbol of the value. If it is specified as unsigned, the value is unsigned. All binary bits are used to represent the value itself. For example, the short Integer Data occupies two bytes, as shown in figure 2.2.

Fig 1.2
When there is a symbol, the maximum value that can be stored is 215-1, that is, 32767, and the minimum value is-32768. When there is no symbol, the maximum value that can be stored is 216-1, that is, 65535, and the minimum value is 0. Some data does not have negative values. You can use unsigned to store positive values, which is twice as large as signed.

4) float data is classified into three types: float, double, and long double. in Visual C ++ 6.0, provide 6-digit valid numbers for float, 15-digit valid numbers for double, and the value range of float and double is different. 4 bytes for float and 8 bytes for double and long double.

5) in the type identifier column of the table, the Section in square brackets [] can save writing, for example, short and short int are equivalent, unsigned int and unsigned are equivalent.

The value of a constant cannot be changed. Generally, it can be determined from its literal form whether it is a constant. Constants include numeric constants (constants) and numeric constants. For example, 12, 0,-3 is an integer constant, 4.6, and-1.23 is a real-type constant. The characters between two single-marker Are character constants, such as '′, 'X ′. A constant that can be recognized literally is called a literal constant or a direct constant ".

A numerical constant is a constant. In C ++, numeric constants are distinguished by types, which can be identified literally.

Integer constant (integer) Type
As we know in the previous section, integer data can be divided into int, short int, long int, unsigned int, unsigned short, unsigned long, and other categories. Integer constants are also divided into the above categories. Why are numerical constants classified into different classes? This is because data types must be matched when values are assigned or function parameters are combined with real and false values.

So how can an integer constant be literally divided into the above categories?
  1. An integer. If the value ranges from-32768 ~ + Within the range of 32767, it is considered to be short int type. It can be assigned to short int, int, and long int variables.
  2. An integer. If the value exceeds the preceding range ~ + Within the range of 2147483647, it is considered to be of the long int type. You can assign it to an int or long int type variable.
  3. If the C ++ version of a computer system (such as Visual C ++) determines that int and long int data occupy the same length in the memory, then they can represent the same range of values. Therefore, an int type constant is also a long int type constant, which can be assigned to int or long int type variables.
  4. Constants do not have the unsigned type. However, a non-negative integer can be assigned to an unsigned integer variable as long as its range does not exceed the value range of the variable.

An integer constant can be expressed in three different ways:
  1. A decimal integer. For example, 1357,-432, and 0. If an integer constant is followed by a letter l or L, it is considered a long int type constant. Such as 123L, 421L, and 0L, which are often used in function calls. If the parameter of the function is long int, the real parameter must be of the long int type. In this case, 123 is used as the real parameter, but 123L is used as the real parameter.
  2. The octal integer. Add a number 0 at the beginning of a constant, which indicates that the constant is expressed in the form of an octal number. For example, 020 indicates that this is octal digit 20, that is, (20) 8, which is equivalent to the decimal digit 16.
  3. A hexadecimal integer. Add a number 0 and an English letter X (or x) at the beginning of the constant to indicate the constant in hexadecimal form. For example, 0X20 indicates that this is the hexadecimal number 20, that is, (20) 16, which is equivalent to the decimal number 32.
Floating point representation

A floating point number can be expressed in two different ways:
1) decimal form. Such as 21.456 and-7.98. It generally consists of an integer and a decimal part. You can omit one of them (such as 78. Or. 06,. 0), but not both. The C ++ compilation system processes all floating point numbers expressed in this form according to double-precision constants, accounting for 8 bytes in memory. If a real number is followed by a letter F or f, it indicates that this number is a single-precision floating point number, such as 1234F,-43f, which occupies 4 bytes. If the letter L or l is added, the number is long double, which occupies 12 bytes in GCC and 8 bytes in Visual C ++ 6.0.

2) exponential form (that is, floating point form ). A floating point number can be written as an index. For example, 3.14159 can be expressed as 0.314159 × 101, 3.14159 × 100 × 10-31.4159 × 10-2. In the program, it should be represented as: 0.314159e1, 3.14159e0, 31.20.9e-1, 314.159e-2. the letter e indicates that the subsequent number is the power of 10, for example, e12 indicates 1012. The general form is:
Digit number part index part

Among the above data, 0.314159, 3.14159, 31.4159, and 314.159 are numerical parts. As you can see, because of the existence of the exponent part, the same floating point number can be expressed in different exponent forms, and the decimal point position in the digit part is floating. For example:
A = 0.314159e1;
A = 3.14159e0;
A = 31.4259e-1;
A = 314.159e-2;
In the preceding four values, floating point numbers of different forms are used, but their functions are the same.

In the program, whether the floating point is written as a decimal point or an exponential point, it is stored in the memory in an exponential form (that is, a floating point form. For example, whether written in the program as 314.159 or 314.159e0, 31.100009e1, 3.14159e2, 0.314159e3, etc., the memory is stored in a normalized exponential form, as shown in Figure 2.3.

Fig 1.3
The number must be less than 1. At the same time, the first digit after the decimal point must be a non-0 digit. For example, it cannot be 0.0314159. Therefore, 314.159 and 314.159e0, 31.100009e1, 3.14159e2, and 0.314159e3 are expressed as 0.314159 × 103 in the memory. The storage unit is divided into two parts: one is used to store the numbers and the other is used to store the indexes. For ease of understanding, in Figure 2.3, decimal representation is used. In fact, in the storage unit, the binary number is used to represent the decimal part, and the power of 2 is used to represent the exponential part.

Numeric constants expressed in exponential form are also processed as double-precision constants.
Character constant 1) common character constant
A character enclosed by a single apostrophes is a scalar constant. For example, 'A', '#', '%', and 'D' are valid character constants, occupying one byte in memory. Note:
  • A character constant can only contain one character. For example, 'AB' is invalid.
  • Character constants are case-sensitive letters. For example, 'A' and 'A' are two different character constants.
  • An underscore (') is a delimiter, not a part of a character constant. For example, cout <'a'; outputs a letter "a" instead of three characters "'A '".

2) escape character constants
In addition to the preceding character constants, C ++ also allows a special character constant, a sequence of characters starting. For example, '\ n' indicates a "line feed" character. "Cout <'\ n';" outputs a line feed, which is the same as "cout <endl. This "Control Character" cannot be displayed on the screen. In the program, it cannot be represented by a common character, but only in special form.

3) Storage Format and usage of character data in memory
When a character constant is stored in a memory unit, the ASCII code corresponding to the character is not actually put into the memory unit. If the value of the character variable c1 is 'A' and the value of c2 IS 'B', the variable contains the 'A' ASCII code 97, 'B''s ASCII Code 98, 2.4 (a) is actually stored in binary in the memory, as shown in 2.4 (B.

Fig 1.4
Since character data is stored in ASCII code, its storage format is similar to that of integer storage. In this way, the generic data and integer data in C ++ can be used universally. A character data can be assigned to an integer variable. Otherwise, an integer data can be assigned to a character variable. You can also perform arithmetic operations on character data, which is equivalent to performing arithmetic operations on their ASCII codes.

[Example 1.1] assign the character to the integer variable.

# Include <iostream> using namespace std; int main () {int I, j; // I and j are Integer Variables I = 'a '; // assign a character constant to the integer variable I j = 'B '; // assign a character constant to the integer variable j cout <I <''<j <'\ n'; // output the values of the Integer Variables I and j, '\ n' is the linefeed return 0 ;}
Output at execution time
65 66
I and j are specified as integer variables. However, in rows 5th and 6th, the characters 'A' and 'B' are assigned to I and j respectively, which are equivalent to the following two values:
I = 65; j = 66;
Because the ASCII codes of 'A' and 'B' are 65 and 66. In lines 5th and 6th of the program, 65 and 66 are directly stored in the memory units of I and j. Therefore, 65 and 66 are output.

We can see that, under certain conditions, generic data and integer data can be used universally. However, note that the character data only occupies one byte, and it can only store 0 ~ An integer in the range of 255.

[Example 1.2] perform arithmetic operations on character data and integers. The following program converts lowercase letters to uppercase letters.

<span style="background-color: rgb(255, 255, 255);">#include <iostream>using namespace std;int main( ){  char c1,c2;  c1='a';  c2='b';  c1=c1-32;  c2=c2-32;  cout<<c1<<' '<<c2<<endl;  return 0;}</span>
The running result is
'A''s ASCII code is 97, while 'A''s ASCII code is 65, 'B' is 98, and 'B' is 66. The ASCII code table shows that each lowercase letter is 32 larger than the ASCII code of the corresponding uppercase letter. The C ++ operator directly performs arithmetic operations on data and values. 'A'-32 gets the integer 65, 'B'-32 get the integer 66. Store 65 and 66 in c1 and c2. Since c1 and c2 are character variables, when cout is used to output c1 and c2, obtain characters A and B (the ASCII code of A is 65, and the ASCII code of B is 66 ).

The portion of a String constant enclosed by a double marker is a String constant, for example, "abc", "Hello! "," A + B "," Li ping "are string constants. The String constant "abc" occupies 4 bytes (instead of 3 bytes) in the memory, as shown in Figure 2.5.

Fig 1.5
The compilation system automatically adds '\ 0' at the end of the string as the string end sign. '\ 0' is not a part of a string. It serves only as the end sign of the string. For example
Cout <"abc" <endl;
3 Characters of abc are output, excluding '\ 0 '.

Note: "a" and 'A' indicate different meanings. "a" is a String constant and 'A' is a character constant. The former occupies two bytes, and the latter occupies one byte. Analyze the following program snippets:
Char c; // define a character variable
C = 'a'; // correct
C = "a"; // error. c can only contain one character

Think about: How many characters does the String constant "abc \ n" contain? It is not 5 characters but 4 characters. "\ n" is an escape character. But it occupies 5 bytes in memory (including a "\ 0" character ). When the compilation system encounters "\", it will recognize it as a Escape Character and use it together with the subsequent character as an escape character.

If the characters after "\" cannot form a valid escape character (for example, "\ c") with "\", an error message is displayed during compilation. If you want to use the "\" character as a character in the string, it should be written as "abc \ n". The character consists of five characters, namely, a, B, c, \, n. If you have the following output statements:
Cout <"abc \ n" <endl;
The output is as follows:
Abc \ (and then line feed)
Same execution
Cout <"I say \" Thank you! \ "\ N ";
The output is:
I say "Thank you! "

If the last character in a string is "\", it indicates that it is a line break. The character in the next line is a part of the string and there is no space between the two strings. For example
Cout <"We must study C \ // The spaces and line breaks after the last" \ "of the line do not work
++ Hard! "; // The character in the row must be followed by the character" \ "at the end of the last line.
We must study C ++ hard!
For programming and reading convenience, in C ++ programming, a symbol name is often used to represent a constant, called a symbolic constant, that is, a constant that appears in the form of an identifier.

[Example 1.3] use of symbolic constants.

# Include <iostream> using namespace std; # define PRICE 30 // note that this is not a statement. Do not add the int main () {int num, total; num = 10; total = num * PRICE; cout <"total =" <total <endl; return 0 ;}
In the program, use the preprocessing command # define to specify the PRICE in the unit of this program to represent the constant 30. After that, any PRICE that appears in the unit of this program represents 30 and can be operated like a constant, the program running result is
Total = 300
Note that a symbolic constant has a name but is not a variable. Its value cannot be changed or assigned a value within its scope (in this example, the main function. If you use the value assignment statement "PRICE = 40;", it is wrong to assign a value to the PRICE. The benefit of using a symbolic constant is that it has a clear meaning and can be "changed and changed" when a constant needs to be changed ". For example:
# Define PRICE 35

In fact, the variables have been used multiple times in the previous example. The amount of variable that can be changed during the program running is called a variable. A variable should have a name that occupies a certain storage unit in the memory and stores the value of the variable in the storage unit. Note the differences between the variable name and variable value, as shown in Figure 2.6.

Figure 1.6 The variable name rule first introduces the concept of identifier. Like other advanced languages, a sequence of valid characters used to identify object names such as variables, symbolic constants, functions, arrays, and types is called identifier ). In short, an identifier is a name. A variable name is a type of identifier. The name of a variable must follow the naming rules of the identifier.

C ++ requires that an identifier can only contain letters, numbers, and underscores. The first character must be a letter or underscore. The following lists legal identifiers and valid variable names:
Sum, average, total, day, month, Student_name, tan, BASIC, li_ling

The following are invalid identifiers and variable names:
M. D. John, $123, #33, 64, Ling li, C ++, Zhang-ling, U. S..

Note: In C ++, uppercase and lowercase letters are considered to be two different characters. Therefore, sum and SUM are two different variable names. Generally, the variable name is represented by lowercase letters, which is consistent with people's daily habits to increase readability. Note that the variable name cannot be the same as the C ++ keyword, system function name, and class name. In foreign software development work, it is often used to add a letter before the variable to indicate the type of the variable. For example, iCount indicates that this is an integer variable, and cSex indicates that this is a variable of variable type.

C ++ does not specify the length (number of characters) of the identifier, but each specific C compilation system has its own rules. Some systems take 32 characters. The characters exceeding the 32 characters are not recognized.
Defining variables in C ++ requires that all variables used be forcibly defined, that is, they must be "defined first and then used", as in example 2.2 and example 2.3. The general form of variable definition is:
Variable type variable table column;
A variable name table column refers to a sequence of one or more variable names. For example:
Float a, B, c, d, e;
Define a, B, c, d, and e as single-precision variables. Note that each variable is separated by a comma and the last is a semicolon. You can specify the initial value when defining a variable. For example:
Float a = 83.5, B, c = 64.5, d = 81.2, e; // The initial values are specified for variables a, c, d, and B and d.

C language requires that the definition of the variable be placed before all the execution statements, while C ++ relaxed the restriction and only required to be defined before the first use of the variable. That is to say, it can appear in the middle of the statement, such:
Int a; // defines variable a (defined before using)
A = 3; // execute the statement and assign a value to
Float B; // defines variable B (defined before B is used)
B = 4.67; // execute the statement and assign a value to B
Char c; // define the variable c (defined before using c)
C = 'a'; // execute the statement and assign A value to c.

C ++ requires that variables be forcibly defined for the following purposes:
1) if it is not defined in advance, it is not used as the variable name, which ensures that the variable name in the program is used correctly. For example, if you write
Int student;
The statement is incorrectly written as statent. For example
Statent = 30;
Check that statent is not defined during compilation and handle the error as an error. Output the "variable statent not declared" information to help you discover errors and avoid errors when using the variable name.

2) each variable is specified as a definite type, and corresponding storage units can be allocated to it during compilation. If a and B are specified as int type, the general compilation system allocates 4 bytes each and stores the data in integer mode.

3) specify that each variable belongs to a specific type, which makes it easy to check whether the calculation of this variable is legal during compilation. For example, integer variables a and B can perform the remainder operation:
A % B
% Is the remainder, and the remainder of a/B is obtained. If a and B are specified as real variables, the remainder operation is not allowed. Related error information is provided during compilation.
Assigning an initial value to a variable allows you to assign it an initial value when defining a variable, which is called variable initialization. The initial value can be a constant or an expression with a definite value. For example
Float a, B = 5.78*3.5, c = 2 * sin (2.0 );
Indicates that a, B, and c are defined as single-precision floating point variables, B is initialized to 5.78*3, and c is initialized to 2 * sin (2.0). After compiling the connection, obtain the sin (2.0) value of the sine function from the standard function library. Therefore, variable c has a definite initial value. Variable a is not initialized.

If no initial value is assigned to the variable, the initial value of the variable is an unpredictable value, that is, the current content in the storage unit is unknown. For example, if a and B are not assigned values, execute the output statement.
Cout <a <"" <B <"" <c <endl;
The output result may be
1.48544e-38 15 1.81858 (the running conditions may vary)
Initialization is not completed in the compilation phase (only static storage variables and external variables described in Chapter 4th are initialized in the compilation phase ), instead, the initial value is assigned when the function is executed while the program is running, which is equivalent to executing an assignment statement. For example:
Int a = 3;
It is equivalent to the following two statements:
Int a; // specify a as an integer variable.
A = 3; // value assignment statement, which assigns 3 to
Assign the same initial value to multiple variables, which must be specified separately and cannot be written
Float a = B = c = 4.5;
And should be written
Float a = 4.5, B = 4.5, c = 4.5;
Float a, B, c = 4.5;
A = B = c;

When defining a variable in C ++, if the keyword const is added, the value of the variable cannot be changed during the running of the program. This variable is called constant variable ). For example:
Const int a = 3; // use const to declare that the value of this variable cannot be changed and the value is always 3

When defining a constant variable, you must initialize it (that is, specify its value) at the same time, and then its value cannot be changed. Constant variables cannot appear on the left of the value assignment. For example, the preceding line cannot be written as follows:
Const int;
A = 3; // constant variables cannot be assigned values.
You can use an expression to initialize a constant variable, as shown in figure
Const int B = 3 + 6, c = 3 * cos (1.5); // The value of B is 9, and the value of c is 3 * cos (1.5)

However, it should be noted that because the system standard mathematical function cos is used, the header file "cmath" (or math. h) include in the Program unit. You can add the following # include command at the beginning of the Program unit:
# Include <cmath>
# Include <math. h>

The value of a variable can be changed. How can the value be a fixed amount or variable? In fact, from the perspective of Computer Implementation, the feature of a variable is that there is a storage unit named after the variable name. In general, the content in the storage unit can change. For a constant variable, there is nothing more than a limit on the variable: the value in the storage unit cannot be changed. Therefore, a common variable is also called a read-only-variable ).

Use the # define command to define the symbol constant and the constant variable defined by the const command. A symbolic constant only replaces a string with a symbol. During pre-compilation, all symbolic constants are replaced with the specified string, which has no type, there is no storage unit named as a symbolic constant in the memory. A constant variable has the characteristics of a variable. It has a type and a storage unit named after it exists in the memory. The length can be measured using the sizeof operator. The only difference from a common variable is that the value of the specified variable cannot be changed. Using the # define command to define a symbolic constant is the method used by C language. C ++ retains it for compatibility with C. C ++ programmers generally prefer to use const to define constant variables. Although the two methods are different, from the perspective of use, we can all think that an identifier represents a constant.

In C language, where should I use the words that define the data type?

When you need to define and allocate buckets for variables, constants, and functions, You need to define data types.
C/C ++ Data Type C language contains five basic data types: void, integer, float, double, and char.
Type description
Void empty type
Int integer
Float floating point type
Double floating point type
Char character type

C ++ defines two basic data types: bool and wchar_t.
Type description
Boolean Type, value: true or false
Wchar_t wide character type

Type Modifier
Some basic data types can be signed, unsigned, short, and long. When the type modifier is used independently, the default type is int. The following table lists all possible data types:

Unsigned char
Signed char
Unsigned int
Signed int
Short int
Unsigned short int
Signed short int
Long int
Signed long int
Unsigned long int
Long double

Type and value range
The size of the basic data type and the data range that can be expressed are related to the compiler and hardware platform. "cfloat" (or "float. h ") header files often define the maximum and minimum values of the data that can be expressed by the basic data type. you can also use sizeof to obtain the type size (number of bytes ). however, many platforms use standard data types, such. int and float usually occupy 32 bits, char occupies 8 bits, and double usually occupies 64 bits. bools is usually implemented in 8 bits.

C Language Data Type

There are four basic types of variable in C;
They are: char, int, double and float.

Char is the most basic unit addressable by the machine; typically a single octet (one byte). This is an integral type.

Int is the most natural size of integer for the machine; typically a whole 16, 32 or 64-bit (2, 4, or 8 bytes respectively) addressable word.

Float is a single-precision floating point value.
Double is a double-precision floating point value.

Related Article

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.