文章目录

前言一、基础知识1. C++书写HelloWorld2.注释3.变量的使用-变量的意义4.常量5.关键字6.标识符明明规则

二、数据类型1.整型2. sizeof关键字3. 实型(浮点型)4.字符型5.转义字符6.字符串型7.布尔类型bool8.数据的输入

总结

前言

Visual Studio 2022详细安装使用调试教程C语言编译器,C++编译器

只是为方便学习,不做其他用途,在此发布C++基础入门部分配套讲义,原作者为黑马程序 配套视频: https://www.bilibili.com/video/BV1et411b73Z

一、基础知识

1. C++书写HelloWorld

#include

using namespace std;

int main()

{

cout << "hello world" << endl;

system("pause");

return 0;

}

2.注释

注释分为单行注释和多行注释

#include

using namespace std;

//1、单行注释

//2、多行注释

/*

main是一个程序的入口

每个程序都必须有这么一个函数

有且仅有一个

*/

int main()

{

//下行代码的含义就是在屏幕中输出Hello world

cout << "hello world" << endl;

system("pause");

return 0;

}

.写多个main程序出现问题 将第一个程序的“main”改成“main1” 或者参照博客C++ Visual Studio中同一个项目包含多个有main函数的源文件怎么分别运行

具体流程:

右键点击该项目名称,点击“属性”;在属性页中,点击“常规”选项卡,将“从生成中排除”的属性值设置为“是”,点击“确定”,进行保存;其他需要排除运行的文件,依次进行上述操作,只保留要运行的文件(被排除的文件的小图标会发生改变)

3.变量的使用-变量的意义

变量存在的意义:方便我们管理内存空间

变量创建的语法: 数据类型 变量名 = 变量初始值 int a = 10;

#include

using namespace std;

int main()

{

//输出helloworld

// cout << "hello world" << endl;

//变量创建的语法: 数据类型 变量名 = 变量

int a = 10;

cout << "a = " << a << endl;

system("pause");

return 0;

}

4.常量

#include

using namespace std;

//常量的定义方式

//1、#define 宏常量

//2、const修饰的变量

//1、#define 宏常量

#define Day 7

int main()

{

//Day = 14; //错误,Day是常量,一旦修改就会报错

cout << "一周总共有" << Day << "天" << endl;

//2、const修饰的变量

const int month = 12;

//month = 24; //错误,const修饰的变量也称为常量

cout << "一年总共有" << month << "月" << endl;

system("pause");

return 0;

}

5.关键字

作用:关键字是C++中预先保留的单次(标识符)

在定义变量或者常量的时候,不要用关键字,否则会产生歧义

#include

using namespace std;

int main()

{

//创建变量: 数据类型 变量名称 = 变量初始值

//不要用关键字给变量或者变量起名称

//int int = 10; 错误,第二个int是关键字,不可以作为变量的名称

system("pause");

return 0;

}

6.标识符明明规则

作用:C++规定给标识符(变量、常量)命名时,有一套自己的规则

标识符不可以是关键字;标识符是由字母、数字、下划线构成;标识符第一个字符只能是字母或下划线;标识符是区分大小写的。

#include

using namespace std;

//标识符命名规则

//1、标识符不可以是关键字

//2、标识符是由字母、数字、下划线构成

//3、标识符第一个字符只能是字母或下划线

//4、标识符是区分大小写的

int main()

{

//1、标识符不可以是关键字

// int int = 10;

//2、标识符是由字母、数字、下划线构成

int abc = 10;

int _abc = 20;

int _123abc = 20;

//3、标识符第一个字符只能是字母或下划线

//int 123abc = 40; 错误

//4、标识符是区分大小写的

int aaa = 100;

//cout << AAA << endl; //AAA 和 aaa不少同一个名称

//建议: 给变量起名的时候,最好能够做到见名知意

int num1 = 10;

int num2 = 20;

int sum = num1 + num2;

cout << sum << endl;

system("pause");

return 0;

}

二、数据类型

C++规定在创建一个变量或者常量时,必须要制顶出相应的数据类型,否则无法给变量分配内存 数据类型的存在意义:给变量分配合适的内存空间

1.整型

超过数据类型的范围,会出错:

#include

using namespace std;

int main()

{

//整型

//1、短整型 (-32768 ~32767)

short num1 = 10;

//2、整型

int num2 = 10;

//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. sizeof关键字

作用:利用sizeof关键字可以统计数据类型所占内存大小语法:sizeof(数据类型/变量) 示例:

#include

using namespace std;

int main()

{

//整型: short (2) int (4) long (4) long long (8)

//可以利用sizeof求出数据类型占用内存大小

//语法: sizeof(数据类型/变量)

short num1 = 10;

cout << "short所占用的内存空间为:" << sizeof(short) << endl;

int num2 = 10;

cout << "short所占用的内存空间为:" << sizeof(int) << endl;

long num3 = 10;

cout << "short所占用的内存空间为:" << sizeof(long) << endl;

long long num4 = 10;

cout << "short所占用的内存空间为:" << sizeof(long long) << endl;

//整型大小比较

//short < int < long <= long long

system("pause");

return 0;

}

3. 实型(浮点型)

作用:用于表示小数

浮点型变量分为两种:

单精度float双精度double

两者的区别在于表示的有效数字范围不同。

数据类型占用空间有效数字范围float4字节7位有效数字double8字节15~16位有效数字

默认情况下输出一个小数,会显示出6位有效数字

#include

using namespace std;

int main()

{

//1、单精度 float

//2、双精度 double

//默认情况下输出一个小数,会显示出6位有效数字

float f1 = 3.1415926f;

cout << "f1 = " << f1 << endl;

double f2 = 3.1415926;

cout << "f2 = " << f2 << endl;

//统计float和double占用内存空间

cout << "float 占用内存空间为:" << sizeof(float) << endl;

cout << "double 占用内存空间为:" << sizeof(double) << endl;

//科学计算法

float d1 = 3e2; //3*10^2

cout << "d1 = " << d1 << endl;

float d2 = 3e-2; //3*0.1^2

cout << "d2 = " << d2 << endl;

system("pause");

return 0;

}

4.字符型

作用:字符型变量用于显示单个字符语法:char ch = ‘a’;                注意1:创建字符型变量的时候,要用单引号,不能用双引号注意2:创建字符型变量时候,单引号内只能有一个字符,不可以是字符串        C和C++中字符型变量所占内存大小为1个字节字符型变量并不是把字符本身放到内存中存储,而是对应的ASSCII编码放入到存储单元

#include

using namespace std;

int main()

{

//1、字符型变量创建方式

char ch = 'a';

cout << ch << endl;

//2、字符型变量所占内存大小

cout << "char字符型变量所占内存:" << sizeof(char) << endl;

//3、字符型变量常见错误

//char ch2 = "b"; //创建字符型变量的时候,要用单引号

//char ch2 = 'abcdef'; //创建字符型变量时候,单引号内只能有一个字符

//4、字符型变量对应ASSII码

cout << (int)ch << endl; //查看字符a对应的ASSII码

//a -- 97

//A -- 65

system("pause");

return 0;

}

常见的字符变量对应的ASSII码a – 97、A – 65

5.转义字符

作用:用于表示一些不能显示出来的ASSCII字符现阶段我们常用的转义字符有:\n \ \t

#include

using namespace std;

int main()

{

//转义字符

//换行符 \n

cout << "hello world" << endl;

cout << "hello world\n";

//反斜杠 \\

cout << "\\" <

//水平制表符 \t 作用可以整齐的输出数据

cout << "aaaa\thelloworld" << endl;

cout << "aa\thelloworld" << endl;

cout << "aaaaaa\thelloworld" << endl;

system("pause");

return 0;

}

6.字符串型

作用:用于表示一串字符

#include

using namespace std;

#include //用C++风格字符串时候,要包含这个头文件 注意:vs2022版本不用这行代码也不报错,和版本有关

int main()

{

//1、C风格字符串

//注意事项: char 细分串名 []

//注意事项2 等号后面 要用双引号 包含起来字符串

char str[] = "hello world";

cout << str << endl;

//2、C++风格字符串

// 包含一个头文件 #include 注:vs2022版本不用写头文件

string str2 = "hello world2";

cout << str2 << endl;

system("pause");

return 0;

}

7.布尔类型bool

作用:布尔数据类型代表真或假的值bool类型只有两个值

true真本质是1false假本质是0

bool类型占一个字节大小

#include

using namespace std;

int main()

{

//1、创建bool数据类型

bool flag = true; //true代表真

cout << flag << endl;

flag = false; //false代表假

cout << flag << endl;

//本质上 1代表真 0代表假

//2、查看bool类型所占内存空间

cout << "bool类型所占内存空间:" << sizeof(bool) << endl;

system("pause");

return 0;

}

8.数据的输入

作用:用于从键盘获取数据关键字: cin语法: cin >> 变量

#include

using namespace std;

int main()

{

//1、整型

// 多行注释快捷键 ctrl+K+C

//int a = 0;

//cout << "请给整型变量a赋值:" << endl;

//cin >> a;

//cout << "整型变量a = " << a << endl;

//2、浮点型

//float f = 3.14f;

//cout << "请给浮点型变量f赋值:" << endl;

//cin >> f;

//cout << "整型变量f = " << f << endl;

//3、字符型

//char ch = 'a';

//cout << "请给字符型变量ch赋值:" << endl;

//cin >> ch;

//cout << "字符型变量ch = " << ch << endl;

//4、字符串型

//string str = "hello";

//cout << "请给字符串型变量str赋值:" << endl;

//cin >> str;

//cout << "字符串型str = " << str << endl;

//5、布尔类型

bool flag = false;

cout << "请给布尔型变量flag赋值:" << endl;

cin >> flag; //bool类型 只有是非0的值都代表真

cout << "字符串型flag = " << flag << endl;

system("pause");

return 0;

}

               

总结

提示:这里对文章进行总结: 例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

推荐阅读

评论可见,请评论后查看内容,谢谢!!!评论后请刷新页面。