catalogue
Basic data types that C + + can handle
String type (see Chapter 6 for details)
Value range of each basic type
Variable: an amount whose value is variable during the operation of a program
Title: read in and display integers
Topic: variable initialization
Basic data types that C + + can handle
l) integer type;
l) floating point number type;
l) character type;
l) boolean type.
Data in program
l = constant
n. data written directly in the source program;
n) its value cannot be changed during the whole program operation.
l) variable
n) data allowed to change during program operation.
Integer type
l) basic integer types:
int
l) by symbol
n = symbolic
signed
n = unsigned
unsigned
l) according to the data range
n = short integer
short
n = long integer
long
n = long integer
long long
l. the ISO C + + standard does not specify the number of bytes and value range of each data type, but only specifies that the order of bytes between them meets the following requirements:
(signed/unsigned)signed char ≤(unsigned) short int ≤(unsigned) int ≤(unsigned) long int ≤ long long int
Character type
char
l) encoding that accommodates a single character;
l} essentially, it stores integers.
Floating Point Types
l) single precision
float
l) double precision
double
l) extended accuracy
long double
String type (see Chapter 6 for details)
l , there are string constants
l# there is no string variable in the basic type
l) use character array to store string (C-style string)
l) String class in standard C + + class library (c + + style String)
Boolean type
bool
l) there are only two values:
true(True) false((false)
l} is often used to represent the results of relational comparison, equality comparison or logical operation
Value range of each basic type
constant
l) the amount whose value cannot be changed during the whole process of program operation;
l) values directly represented by symbols (text);
l} for example, 12, 3.5 and 'A' are constants.
integer constant
l) integer in text form;
l = decimal
n) there are several numbers from 0 to 9, but the number part cannot start with 0. The positive sign before the positive number can be omitted.
l = octal
n = leading 0 + several numbers from 0 to 7.
l = hexadecimal
n leading 0x + several numbers from 0 to 9 and letters from a to f (both upper and lower case).
l} suffix
n. suffix L (or L) indicates that the type is at least long, suffix LL (or LL) indicates that the type is long long, and suffix U (or U) indicates unsigned type.
Floating point constant
l) real numbers in text form;
l. general form:
n - for example, 12.5, - 12.5, etc.
l) index form:
n - for example, 0.345E+2, - 34.4E-3;
n , integer part and decimal part can be omitted.
The floating point constant is double by default. If the suffix f (or F) can make it float, for example, 12.3f.
character constants
l) a character enclosed in single quotation marks, such as: 'a','D ','? ',' $';
l + + characters that are not displayed in the C + + program escape list
C style string constant
Example:
The type of character constant or string constant can be changed by adding prefix. The prefix and its meaning are shown in the following table:
Variable: an amount whose value is variable during the operation of a program
l) variable definition
N. data type... Variable name 1, variable name 2, Variable name n;
l) initialization
n. C + + language provides a variety of initialization methods;
n) for example:
int a = 0; int a(0); int a = {0}; int a{0};
The initialization method using braces is called list initialization. Information loss is not allowed during list initialization. For example, initializing an int variable with a double value will cause data loss.
symbolic constants
l) the form of constant definition statement is:
n
const
Data type specifier: constant name = constant value;
Or:
n , data type specifier , const , constant name = constant value;
For example, you can define a symbolic constant representing PI:
n
const float PI = 3.1415926;
l) symbolic constants must be initialized during definition, and their values cannot be changed in the middle of the program.
Program example
Title: read in and display integers
l) main knowledge points:
n = constant
u) the value of the data written directly in the source program cannot be changed during the operation of the whole program. Such data is called a constant.
n = variable
u) read from external devices (such as keyboard and hard disk) of the computer during operation. The value of these data can be changed during program operation. Such data is called variables
n) input data from the keyboard
You can read data from a standard input device (usually a keyboard) through the > > operation of the object cin of the iostream class
n storage of data
u) in order to store data, memory space needs to be allocated for these data in advance.
The definition of u# variable is to allocate memory space when naming the variable.
l) source code
#include <iostream> using namespace std; int main() { int radius; cout<<"Please enter the radius!\n"; cin>>radius; cout<<"The radius is:"<<radius<<'\n'; cout<<"PI is:"<<3.14<<'\n'; cout<<"Please enter a different radius!\n"; cin>>radius; cout<<"Now the radius is changed to:" <<radius<<'\n'; return 0; }
Title: naming constants
l) main knowledge points: symbolic constants
l) source code
#include <iostream> using namespace std; int main() { const double pi(3.14159); int radius; cout << "Please enter the radius!\n"; cin >> radius; cout << "The radius is:" << radius << '\n'; cout << "PI is:" << pi << '\n'; cout << "Please enter a different radius!\n"; cin >> radius; cout << "Now the radius is changed to:" << radius << '\n'; cout << "PI is still:" << pi << '\n'; //cin>>pi; return 0; }
l) operation results:
Please enter the radius! 2 The radius is:2 PI is:3.14159 Please enter a different radius! 3 Now the radius is changed to:3 PI is still:3.14159
Topic: variable initialization
l) main knowledge points: initialization of variables
n. although the value of a variable can be obtained at runtime, it can also be initialized when defining a variable, and initialization should be advocated;
n) uninitialized variables whose values may be random. If you misuse a variable that is not initialized and does not give a definite value, you will cause an error.
l) source code
#include <iostream> using namespace std; int main() { const double pi(3.14159); int radius(0); cout << "The initial radius is:" << radius << '\n'; cout << "PI is:" << pi << "\n"; cout << "Please enter a different radius!\n"; cin >> radius; cout << "Now the radius is changed to:" << radius << '\n'; cout << "PI is still :" << pi << '\n'; return 0; }