C++基础知识

c++基础入门

1.第一个程序

//2022-12-25 23:30:18
#include<iostream>
using namespace std;

int main() {
	cout << "Hello world!" << endl;
	system("pause");//多这一行语句会在输出时可以多一步按任意键继续再结束
	return 0;
}


2.变量

//2022-12-25 23:40:43
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

int main() {
	int a ;
	//scanf("%d", &a);
	cin>>a;//关于创建变量必须赋初始值,应该不能片面的理解,我刚开始不赋值,用输入也是可以的
	cout << "a=" << a << endl;
	return 0;
}


3.常量

//2022-12-25 23:47:48
#define day 7
#include<iostream>
using namespace std;
//#define day 7

int main() {
//#define day 7
	//day = 8;	//笔记:宏常量不能修改
	cout << "一周一共有" << day << "天" << endl;
//#define day 7//不可以

	const int month = 12;
	//month = 13;	//错误,
	cout << "一年有" << month << "月" << endl;
}

/*笔记:
#define day 7
1.宏常量,只是说通常在文件上方定义,但是出现在任意位置都是可以的,确保不出现未定义就行
2.const表示一个常量不能够被修改
*/

4.sizeof

//2022-12-26 00:00:52
#include<iostream>
using namespace std;

int main() {

	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;
}

在这里插入图片描述

5.浮点型

//2022-12-26 10:25:17
#include<iostream>
using namespace std;

int main() {
	//后面加一个f是因为系统默认是双精度的,加了f以后一开始就直接可以定义为单精度
	float f1 = 3.14f;	
	cout << f1<<endl;

	double d1 = 3.1415926;
	cout << d1 << endl;	//3.14159,即使是双精度,系统默认输出的也只有6位有效数字

	cout << sizeof(float) << endl;	//4个字节
	cout << sizeof(double) << endl;	//8个字节

	//科学计数法
	float f2 = 3e2;
	cout << "f2=" << f2 << endl;	//300

	float f3 = 3e-2;
	cout << "f3=" << f3 << endl;	//0.03

	return 0;

}

6.字符型

//2022-12-26 10:48:22

#include<iostream>
using namespace std;

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

	//ch = "abcd";	//错误,不可以使用双引号
	//ch = 'abcde';	//错误,提示字符常量中的字符过多
	ch = 'abcd';
	cout << ch << endl;	//d
	ch = 'abc';
	cout << ch << endl;	//c
	ch = 'ab';
	cout << ch << endl;	//b
	ch = 'a';
	cout << ch << endl;	//a
	/*
	由以上可知,当字符数在4个以内,并不会报错,而是输出最后一个字符,
	所以说单引号内只能放一个字符是错误的
	*/
	
	cout << (int)ch << endl;	//97
	cout << int(ch) << endl;	//97
	/*
	这两种方式都可以查看字符的ASCII码
	*/

	ch = 97;	//将ASCII码直接赋值,也可以输出对应的字符
	cout << ch << endl;

	return 0;
}

/*
笔记:
字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放入到存储单元

*/

7.转义字符

在这里插入图片描述

//2022-12-26 11:33:49
#include<iostream>
using namespace std;

int main() {
	cout<<"\\\\"<<endl;	//关于\输出,必须是偶数个
	cout << "aaa\thello world" << endl;
	cout << "aaaa\thello world" << endl;
	cout << "aaaaa\thello world" << endl;
	cout << "a\thello world" << endl;
	cout << "1234567\thello world" << endl;
	//\t是水平制表符,占8个位置,不够时,就补空格

}

在这里插入图片描述

8.字符串

//2022-12-26 11:35:33

#include<iostream>
#include<string>	//2019版本的已经不用加入这个头文件也能运行成功了
using namespace std;

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

9.bool类型

//2022-12-27 18:17:42
#include<iostream>
using namespace std;

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;
}

10.数据的输入

//2022-12-27 19:29:04
#include<iostream>
using namespace std;

int main() {
	/*int a = 0;
	cin >> a;
	cout << a << endl;*/

	//double d = 0;
	//cin >> d;	//3.0
	//cout << d << endl;	//3,默认输出为整数

	bool flag;
	cin >> flag;	//输入字母,输出也是0,而不是非0就输出1,其实这种理解是错误的,
					//因为bool类型是无符号int类型,得输入数字才可以,就不能输入字母之类
	cout << flag;
	return EXIT_SUCCESS;	//这是一个宏,表示程序成功终止,对应的还有非成功终止
}

11.goto语句

在这里插入图片描述

12.函数的分文件编写

作用: 让代码结构更加清晰

函数分文件编写一般有4个步骤

  1. 创建后缀名为.h的头文件
  2. 创建后缀名为.cpp的源文件
  3. 在头文件中写函数的声明
  4. 在源文件中写函数的定义

swap.h

//swap.h文件
#include<iostream>
using namespace std;

//实现两个数字交换的函数声明
void swap(int a, int b);

swap.cpp

//swap.cpp文件
#include "swap.h"

void swap(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}

main.cpp

//main函数文件
#include "swap.h"
int main() {

	int a = 100;
	int b = 200;
	swap(a, b);

	system("pause");

	return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

太极生两鱼

要天天开心哦!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值