2.1 Data Processing (a)

2.1.1 Integer

Action : integer variable represents the integer type data

C ++ integral type can be represented in the following ways, except that a different memory space occupied :

type of data take up space Ranges
short (short integer) 2 bytes (16 bits) (-2^15 ~ 2^15-1)
int (integer) 4 bytes (32 bits) (-2^31 ~ 2^31-1)
long (long int) 4 bytes for the Windows, Linux is a 4-byte (32-bit), 8-byte (64-bit) (-2^31 ~ 2^31-1)
long long (long plastic) 8 bytes (64 bits) (-2^63 ~ 2^63-1)

2.1.2 sizeof keyword

Role: the use of sizeof keyword statistical data types share memory size

grammar: sizeof( 数据类型 / 变量)

Example:

int main() {

    cout << "short 类型所占内存空间为: " << sizeof(short) << endl;
    cout << "int 类型所占内存空间为: " << sizeof(int) << endl;
    cout << "long 类型所占内存空间为: " << sizeof(long) << endl;
    cout << "long long 类型所占内存空间为: " << sizeof(long long) << endl;

    return 0;
}


Integer Conclusion : == short <int <= long <= long long ==

2.1.3 Float

Role : for the fractional

Float variables divided into two types:

  • Single-precision float
  • Double double

Both the difference is that different effective numerical range expressed.

type of data take up space Effective range of numbers
float 4 bytes 7 significant digits
double 8 bytes 15 to 16 significant digits

Example:

int main() {

    float f1 = 3.14f;
    double d1 = 3.14;

    cout << f1 << endl;
    cout << d1<< endl;

    cout << "float  sizeof = " << sizeof(f1) << endl;
    cout << "double sizeof = " << sizeof(d1) << endl;

    //科学计数法
    float f2 = 3e2; // 3 * 10 ^ 2 
    cout << "f2 = " << f2 << endl;

    float f3 = 3e-2;  // 3 * 0.1 ^ 2
    cout << "f3 = " << f3 << endl;

    return 0;
}

2.1.4 Character

Action: string variables for displaying a single character

grammar:char ch = 'a';

Note 1: When displaying character variables, characters enclosed in single quotes, not double quotes

Note 2: single quotes only one character, not a string

  • C and C ++ variable character occupies only 1 byte.
  • Character variable is not the character itself is stored into memory, but the corresponding ASCII encoded into the storage unit

Example:

int main() {
    
    char ch = 'a';
    cout << ch << endl;
    cout << sizeof(char) << endl;

    //ch = "abcde"; //错误,不可以用双引号
    //ch = 'abcde'; //错误,单引号内只能引用一个字符

    cout << (int)ch << endl;  //查看字符a对应的ASCII码
    ch = 97; //可以直接用ASCII给字符型变量赋值
    cout << ch << endl;

    return 0;
}

ASCII code table

2.1.5 ### escape character

Role: used to represent some of ASCII characters can not be displayed

At present, we have a common escape characters: \n \\ \t Escape character table

Example:

int main() {
    
    
    cout << "\\" << endl;
    cout << "\tHello" << endl;
    cout << "\n" << endl;

    return 0;
}

2.1.6 String

Role : a string of characters used to represent

Two styles

  1. C-style strings :char 变量名[] = "字符串值"

    Example:

    int main() {
    
     char str1[] = "hello world";
     cout << str1 << endl;
    
     system("pause");
    
     return 0;
    }

Note: C-style string to use in double quotes

  1. C ++ style strings :string 变量名 = "字符串值"

    Example:

    int main() {
    
     string str = "hello world";
     cout << str << endl;
    
     return 0;
    }

    Note: C ++ style strings, need to add headers #include <string>

2.7 Boolean type bool

Role: Boolean data type represents a true or false value

bool type has only two values:

  • true --- true (essentially 1)
  • false false --- (essentially 0)

bool accounting type == 1 == byte size

Example:

int main() {

    bool flag = true;
    cout << flag << endl; // 1

    flag = false;
    cout << flag << endl; // 0

    cout << "size of bool = " << sizeof(bool) << endl; //1

    return 0;
}

Note: C ++ style strings, need to add headers #include <stdbool>

Guess you like

Origin www.cnblogs.com/wyd-blogs/p/12467191.html