One of the C ++ data types: constants and variables 1. Text constants: constants are called "text constants". "text" means that we can only refer to them as their values, A constant is an unchangeable value. At the same time, note that a text constant is unaddressable (that is, it is impossible for our program to obtain an expression like constant 20 & 20 ), although constants are stored somewhere in the memory, we cannot access the constant address. Constants are of the following types: 1. Character-type char: A byte representation, which usually represents a single character or a small integer. Character-type constants are represented by a pair of single quotes 'with a character. (1) printable character constants: The storage format of 'A' 2' and '''' character constants in memory depends on the ASC character table. (2) character constants cannot be printed. The '/N' linefeed' // 'backslash'/t' horizontal tab '/0' is null) character 2. Integer INT: the integer of the length of a machine word. Short integer short: the integer of half the length of a machine word. Long Integer: the integer of one or two machine characters. In 32-bit machines, int and long are usually the same. (1) The Char character type mentioned above can also be considered as a byte character integer. In the following small example, we can see that the memory format varies with the char data initialization methods. Char A =
'1'; Cout <A + 1 <Endl; // The output result is 50. For details, refer to the ASC table. The character constant '1' is in the memory of 49 char B =
1; Cout <B + 1 <Endl; // The output result is 2. In fact, character constants can also initialize int, long, and other data types, such as: int c = '1 '; cout <C + 1 <Endl; // The output result is also 50 and: Char A = '1'; cout <A <Endl; // The output result is 1 int A = '1'; cout <A <Endl; // The output result is 49. (2) Integer constants can be expressed in decimal, octal, and hexadecimal notation. For example, 20 (decimal) 024 (octal) 0x24 (hexadecimal) can also be written as 0x24, "X" is case insensitive.) (3) an integer constant can be signed or unsigned. For example, an 8-bit signed Char:-128 ~ 127 an 8-bit unsigned char: 0 ~ 255 note: the default integer constant is int type. We can use the suffix "L" or "L" to forcibly express the integer constant as long type, you can also add the suffix "U" or "U" to specify the number of unsigned values, for example, 128u, 1024ul, 1l, 8lu3, float: Single-precision floating point double with a word length: two-character-length double-precision floating-point-length double-precision long: three or four-character-length extended precision floating-point numbers (1) floating-point constants can be written as scientific notation or common decimal numbers (2) the default value of a floating-point constant is double. You can add the suffix "F", "F", "L", and then modify it to a single-precision floating-point number or an extended floating-point number. However, you can only modify the floating-point number in decimal format. Another difference with Integer constants is that floating point numbers are positive and negative, that is, they cannot use the suffix "U" or "U. For example, 3.14159f 0.1f 12.345l 0.0 3e1 1.0e-3 1.0l4, Boolean constant Boolean: true or false5, String constant: a special type, which is not a built-in or basic data type,
It is actually a String constant array, which is composed of the string text itself and the null that represents the end of the compiler.
(Null)
Character Composition. The actual format of the String constant "AB" in the memory is 'a 'B'/0'. If the program contains "two" "some, the C ++ compiler connects them together and adds an empty character at the end, that is, the string constant "twosome" is output and can be indicated by line breaks, you only need to add "/" to the line feed. For example, "ABC/DE/fgh" actually represents "abcdefgh". 1. Differences between the following literal constants: () 'A' l 'A' "A" L "A" text constant 'A' represents a single character, type: Char, l'a' also represents a single character 'a ', however, its type is wchar_t, because the preguide l represents a wide character. The literal constant "A" represents a string, which contains a single character 'a' and an empty character "/0', and l" A "also represents a string, which is an array represented by the constant width character. (B) 10, 10u, 10l, 10ul, 012, and 0xa all represent the decimal number 10. The difference is that the hexadecimal value is different and the specific type is different (c) 3.14 3.14f 3.14l indicates a floating point number of 3.14. The difference is that the specific type is different. 2. Which of the following statements are invalid? (a) "who goes with F/144 rgus? /014 "Legal,/144 represents a d character,/014 represents a strange character (B) 3.14e1l cannot use L to modify the floating point number represented by scientific notation (c) "two" L "some" is valid, but the result is meaningless (d) 1024f is valid (e) 3.14ul is invalid. You cannot use U to modify floating point number (f) "multiple linecomment" is invalid, the new line uses/2, variable 1, and introduction variable to provide us with a memory area with a name, which can be read, written, and processed by the program. Each symbol variable in C ++ is associated with a specific data type, which determines the size and layout of the memory, the value that can be stored in the memory zone and the operation set that can be applied to it. We can describe variables as objects. Similarities and differences between variables and constants: 1. Similarities: There are storage areas and related types. 2. Differences: constants cannot be addressable. variables can be addressable. Each variable has two values associated with it: (1) data value: stored in a memory address and used as the right value. (2) address value: the memory address that stores the data value, which is used as the left value. For example, CH = CH-'0'; ch on the right indicates that the data value is read, while ch on the left indicates that the address value is read. 0 = 1; // The constant cannot be used as the left value salary + salary * 0.1 = newsalary; // The arithmetic expression cannot be used as the left value. The definition of a variable will cause memory allocation. A variable definition corresponds to a memory region, so the same variable cannot be defined repeatedly. However, it can be declared repeatedly as required by the program, for example, extern string stringname; such a statement should be put in the header file. For example: // file module0.c // defines the filename object string filename ;//... assign a value to filename // file module1.c // you need to use the filename object // Oh: Compilation failed: // In module1.c, filename does not define ifstream input_file (filename ); // file module1.c // you need to use the filename object // declare filename, that is, let the program know it, // but do not introduce the second definition extern string filename; ifstream input_file (filename); 2. variable definition: it consists of a type identifier followed by a name and ends with a semicolon: Double salary; double wage; int month; int Day; int year; unsigne D long distance; can also be written as: Double salary, wage; int month, day, year; unsigned long distance; for initialization: when defining variables in the global domain, if you do not initialize them, the system will automatically initialize to 0. If it is defined in a local domain or dynamically allocated by new, the system will initialize to a random number. C ++ supports two types of initialization: (1) Explicit int ival = 1024; string project = "Fantasia"; (2) Implicit int ival (1024 ); string Project ("Fantasia"); in addition, the comma-separated list of identifiers can also provide an explicit initial value for each object, such as: doubld salary = 9999.99, wage = salary + 0.01; also, each built-in data type supports a special constructor syntax, which can initialize the object to 0, for example, int ival = int (); // 0 doubld dval = double (); // 0.0 vector <int> ivec (10); // 10 0 test questions: 1. Which of the following statements are invalid? Correct them (a) int car = 1024, auto = 2048; (B) int ival = ival; // no practical significance (c) int ival (INT (); (d) Do Uble salary = wage = 9999.99; // wage is not defined before use. Add a double wage = 9999.99 statement before this statement. (e) CIN> int input_value; // when using Cin, it cannot be defined at the same time. 2. Differences between the following student and name instances (a) extern string name; string name ("exercise 3.5a"); (B) extern vector <string> students; the first line is the name declaration, telling the compiler name to represent a string type, no memory allocation is performed. The second line is the definition of name, which tells the compiler that name represents a string type and allocates memory. 3. Which of the following statements are invalid? Correct them (a) int double = 3.14159; // invalid reserved words (B) vector <int> _; (c) string namespace; // invalid. Reserved Words (d) string catch-22 are used; // contains an invalid-(e) Char character or_2 = '1 '; // The name cannot start with a number (f) float = 3.14f; 4. What is the difference between the global object definition and the local object definition (if you think there is a difference) string global_class; int global_int; int main () {int local_int; string local_class ;//...} both string objects are initialized by their default constructor. In addition, the global variable global_int is initialized to 0, while the local variable local_int is not initialized and is of any value. However, when using VC. Net for debugging, if the output does not initialize the local variable, the compilation fails, prompting you to use the variable without initialization. But the output does not have the initialization of the global variables, there is no problem, because the system helps to initialize to 0.