C++ stage 01 notes 02 [data type (integer, sizeof keyword, real (floating point), character, escape character, string, bool, data input)]

C++| Ingenious work from 0 to 1 introductory programming [video + courseware + notes + source code]

table of Contents

2 Data type

2.1 Integer

2.2 sizeof keyword

2.3 Real type (floating point type)

2.4 Character type

2.5 escape characters

2.6 String type

2.7 Boolean type bool

2.8 Data input


2 Data type

C++ stipulates that when creating a variable or constant, the corresponding data type must be specified, otherwise memory cannot be allocated to the variable.

2.1 Integer

Role : Integer variables represent integer data.

There are several ways to express integer types in C++. The difference lies in the memory space occupied :

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

 

#include<iostream>
using namespace std;

int main() {
	//整型
	//1、短整型 2^15==32768(-32768~32767)
	short num1 = 32768;

	//2、整型
	int num2 = 32768;

	//3、长整型
	long num3 = 10;

	//4、长长整型
	long long num4 = 10;

	cout << "num1 = " << num1 << endl;
	cout << "num2 = " << num2 << endl;
	cout << "num3 = " << num3 << endl;
	cout << "num4 = " << num4 << endl;

	system("pause");

	return 0;
}

2.2 sizeof keyword

Function: Use the sizeof keyword to calculate the memory size occupied by the data type .

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

Example:

#include <iostream>
using namespace std;

int main()
{
    //整型:short(2字节)int(4字节)long(4字节)long long(8字节)
    //可以利用sizeof关键字求出数据类型占用内存大小
    //语法:sizeof(数据类型 / 变量)
    short num1 = 10;
    cout << "short类型占用内存空间为:" << sizeof(short) << endl;
    cout << "short类型占用内存空间为:" << sizeof(num1) << endl;

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

    system("pause");

    return 0;
}
 

Integer conclusion : short <int <= long <= long long

2.3 Real type (floating point type)

Role : Used to represent decimals.

There are two types of floating-point variables :

  1. Single precision float

  2. Double

The difference between the two lies in the range of valid digits indicated.

type of data take up space Valid number range
float 4 bytes 7 significant digits
double 8 bytes 15~16 significant figures

The compiler treats a decimal as double-precision data by default! Without "f" after the data, the compiler will automatically do a conversion to convert double precision to single precision! Do one more conversion!

Usually an "f" is added after the float data type to tell the compiler that this is a float type data.

Example:

#include <iostream>
using namespace std;

int main()
{
    float f1 = 3.1415926f; //1、单精度 float
    double d1 = 3.1415926; //2、双精度 double

    //默认情况下,输出一个小数,会显示出6位有效数字
    cout << f1 << endl; //小数,最多6位有效数字
    cout << d1 << endl;

    //统计float和double占用内存空间
    cout << "float占用内存空间为:" << sizeof(float) << endl;   //4字节
    cout << "double占用内存空间为:" << sizeof(double) << endl; //8字节
    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;

    system("pause");

    return 0;
}

2.4 Character type

Function: The character variable is used to display a single character.

grammar:char ch = 'a';

Note 1: When displaying character variables, use single quotes to enclose the characters instead of double quotes .

Note 2: There can only be one character in a single quotation mark, not a string.

  • Character variables in C and C++ only occupy 1 byte.

  • The character variable does not store the character itself in the memory, but puts the corresponding ASCII code in the storage unit.

Example:

#include <iostream>
using namespace std;

int main()
{
	//1、字符型变量创建方式
	char ch = 'a';
	cout << ch << endl; //a

	//2、字符型变量所占内存大小
	cout << "char字符型变量所占内存:" << sizeof(char) << endl; //1

	//3、字符型变量常见错误
	//ch = "abcde"; //错误,不可以用双引号
	//ch = 'abcde'; //错误,单引号内只能引用一个字符

	//4、字符型变量对应的ASCII编码 a-97;A-65
	cout << (int)ch << endl; //查看字符a对应的ASCII码 (int)ch:将字符型强转为整型 97
	ch = 97; //可以直接用ASCII给字符型变量赋值
	cout << ch << endl;	//a

	system("pause");

	return 0;
}

ASCII code table:

ASCII value Control character ASCII value character ASCII value character ASCII value character
0 NUT 32 (space) 64 @ 96
1 SOH 33 ! 65 A 97 a
2 STX 34 " 66 B 98 b
3 ETX 35 # 67 C 99 c
4 ROT 36 $ 68 D 100 d
5 ENQ 37 % 69 E 101 e
6 ACK 38 & 70 F 102 f
7 BEL 39 , 71 G 103 g
8 BS 40 ( 72 H 104 h
9 HT 41 ) 73 I 105 i
10 LF 42 * 74 J 106 j
11 VT 43 + 75 K 107 k
12 FF 44 , 76 L 108 l
13 CR 45 - 77 M 109 m
14 SO 46 . 78 N 110 n
15 AND 47 / 79 O 111 O
16 ACCORDING TO 48 0 80 P 112 p
17 DCI 49 1 81 Q 113 q
18 DC2 50 2 82 R 114 r
19 DC3 51 3 83 S 115 s
20 DC4 52 4 84 T 116 t
21 NAK 53 5 85 U 117 u
22 SYN 54 6 86 V 118 v
23 TB 55 7 87 W 119 w
24 CAN 56 8 88 X 120 x
25 IN 57 9 89 AND 121 and
26 SUB 58 : 90 FROM 122 from
27 ESC 59 ; 91 [ 123 {
28 FS 60 < 92 / 124 |
29 GS 61 = 93 ] 125 }
30 RS 62 > 94 ^ 126 `
31 US 63 ? 95 _ 127 OF THE

ASCII 码大致由以下两部分组成:

  • ASCII 非打印控制字符:ASCII 表上的数字 0-31 分配给了控制字符,用于控制像打印机等一些外围设备。

  • ASCII 打印字符:数字 32-126 分配给了能在键盘上找到的字符,当查看或打印文档时就会出现。

2.5 转义字符

作用:用于表示一些不能显示出来的ASCII字符

现阶段我们常用的转义字符有:\n\\\t

转义字符 含义 ASCII码值(十进制)
\a 警报 007
\b 退格(BS) ,将当前位置移到前一列 008
\f 换页(FF),将当前位置移到下页开头 012
\n 换行(LF) ,将当前位置移到下一行开头 010
\r 回车(CR) ,将当前位置移到本行开头 013
\t 水平制表(HT) (跳到下一个TAB位置) 009
\v 垂直制表(VT) 011
\\ 代表一个反斜线字符"" 092
' 代表一个单引号(撇号)字符 039
" 代表一个双引号字符 034
\? 代表一个问号 063
\0 数字0 000
\ddd 8进制转义字符,d范围0~7 3位8进制
\xhh 16进制转义字符,h范围09,af,A~F 3位16进制

示例:

#include <iostream>
using namespace std;

int main()
{
    cout << "\\" << endl; //第一个“\”告诉编译器,使用者要输出一个特殊符号!
    cout << "\tHello" << endl;
    cout << "\n"
         << endl;

    //换行符“\n”
    cout << "hello world~\n";

    //反斜杠“\\”
    cout << "\\" << endl;

    //水平制表符“\t”,作用:可以整齐地输出数据
    cout << "aaaa\thello world!" << endl; //“\t”占用8个空格,空格有多长取决于a占用多少字符
    cout << "aa\thello world!" << endl;//“aaa”占用3个空格,“aaaa”占用4个空格
    cout << "aaaaa\thello world!" << endl;//水平制表符作用:输出时候的对其效果

    cout << "aaaa hello world!" << endl;
    cout << "aa  hello world!" << endl;
    cout << "aaaaa hello world!" << endl;


    system("pause");

    return 0;
}

2.6 字符串型

作用:用于表示一串字符。

两种风格

  1. C风格字符串:char 变量名[] = "字符串值";【注意:C风格的字符串要用双引号括起来。】
  2. C++风格字符串:string 变量名 = "字符串值";【注意:C++风格字符串,需要加入 头文件 #include<string>

示例:

#include <iostream>
#include <string> // 用C++风格字符串时,要包含这个头文件
using namespace std;

int main()
{
   //1.C风格字符串:char 变量名[] = "字符串值";
   //注意事项1:char 字符串名 []
   //注意事项2:等号后面,要用双引号包裹字符串
   char str1[] = "hello world!";
   cout << str1 << endl; //hello world!

   //2.C++风格字符串:string 变量名 = "字符串值";
   //注意事项1:包含一个头文件#include <string>
   string str2 = "hello world!";
   cout << str2 << endl; //hello world!

   return 0;
}

2.7 布尔类型 bool

作用:布尔数据类型代表真或假的值。

bool类型只有两个值:

  1. true --- 真(本质是1)

  2. false --- 假(本质是0)

bool类型占1个字节大小。

示例:

#include <iostream>
using namespace std;

int main()
{
	//1、创建bool数据类型
	bool flag = true;	  //true代表真
	cout << flag << endl; // 1

	flag = false;		  //false代表假
	cout << flag << endl; // 0
	//bool本质上,1代表真、0代表假

	//2、查看bool数据类型所占内存空间
	cout << "bool类型所占内存空间:" << sizeof(bool) << endl; //1
	cout << "size of bool = " << sizeof(bool) << endl;		  //1

	system("pause");

	return 0;
}

2.8 数据的输入

作用:用于从键盘获取数据。

关键字:cin

语法: cin >> 变量

示例:

#include <iostream>
#include <string> //string头文件
using namespace std;

int main()
{
	//1、整型 输入
	int a = 0;
	cout << "请给整型变量a赋值:" << endl; //请输入整型变量
	cin >> a;
	cout << "整型变量a = " << a << endl;

	//2、浮点型 输入
	double d = 0;
	cout << "请给浮点型变量d赋值:" << endl; //请输入浮点型变量
	cin >> d; // “>>”右移运算符
	cout << "浮点型变量d = " << d << endl;

	float f = 3.14f;
	cout << "请给浮点型变量f赋值:" << endl; //请输入浮点型变量
	cin >> f;
	cout << "浮点型变量f = " << f << endl;

	//3、字符型 输入
	char ch = 0;
	cout << "请给字符型变量ch赋值:" << endl; //请输入字符型变量
	cin >> ch;
	cout << "字符型变量ch = " << ch << endl;

	//4、字符串型 输入
	string str;
	cout << "请给字符串型变量str赋值:" << endl; //请输入字符串型变量
	cin >> str;
	cout << "字符串型变量str = " << str << endl;

	//5、布尔类型 输入
	bool flag = true;
	cout << "请给布尔型变量flag赋值:" << endl; //请输入布尔型变量
	cin >> flag; //bool类型:只要是非0的值,都代表真
	cout << "布尔型变量flag = " << flag << endl;

	return EXIT_SUCCESS;
}

加油~

Guess you like

Origin blog.csdn.net/weixin_44949135/article/details/115031207