目录
1 C++初识
1.1第一个C++程序
Projuct001 框架
#include<iostream>
using namespace std;
int main()
{
system("pause");
return 0;
}
具体例子
#include<iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
system("pause");
return 0;
}
1.2注释
#include<iostream>
using namespace std;
int main1()
{
//变量创建的语法:数据类型 变量名=初始值
//不用用关键字给变量或常量起名称
//int int= 10; //错误,第二个int是关键字,不可以作为变量的名称
int a = 10;
cout << "a=" << a << endl;
system("pause");
return 0;
}
1.3变量
#include<iostream>
using namespace std;
int main()
{
//变量创建的语法:数据类型 变量名=初始值
int a = 10;
cout << "a=" << a << endl;
system("pause");
return 0;
}
1.4 常量
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;//用const修饰的变量也称为常量
//month = 24;//错误,const修饰的变量也称为常量
cout << "一年总共有:" << month << "个月份" << endl;
system("pause");
return 0;
}
1.5关键字
1.6 标识符命名规则
#include<iostream>
using namespace std;
/*标识符命名规则
1、标识符不可以是关键字
2、标识符是由字母、数字和下划线构成
3、数字不能作为起名的第一个字符
4、标识符中字母区分大小写
*/
int main()
{
//1、标识符不能是关键字
//int int=10;
//2、标识符由字母数字下划线组成
//3、数字不能作为起名的第一个字符
int abc = 10;
int _abc = 20;
int _123abc = 30;
//int 123abc = 30;
//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;
}
2 数据类型
2.1 整型
#include<iostream>
using namespace std;
int main()
{
//整型
//1、短整型
short num1 = 10;
//2、整型
int num2 = 10;
//3、长整型
long num3 = 10;
//4、长长整型
long long num4 = 10;
cout << "num=" << num1 << endl;
cout << "num=" << num2 << endl;
cout << "num=" << num3 << endl;
cout << "num=" << num4<< endl;
system("pause");
return 0;
}
2.2 sizeof关键字
#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;
int num2 = 10;
cout << "short占用的内存空间为:" << sizeof(num2) << endl;
long num3 = 10;
cout << "short占用的内存空间为:" << sizeof(long) << endl;
long long num4 = 10;
cout << "short占用的内存空间为:" << sizeof(num4) << endl;
system("pause");
//整型大小比较
//short<int<=long<=long long
return 0;
}
2.3实型(浮点型)
#include<iostream>
using namespace std;
int main()
{
//1、单精度
//2、双精度
//默认情况下,输出一个小数会显示6位有效数字
float f1 = 3.1415926f;//3.14后加一个f,避免系统默认将小数默认为双精度;
cout << "f1=" << f1 << endl;
double d1 = 3.1415926;
cout << "d1=" << d1 << endl;
//统计float和double占用的内存空间
cout << "float占用的内存空间为:" << sizeof(float) << endl;//4字节
cout << "double占用的内存空间为:" << sizeof(double) << endl;//8字节
//科学计数法
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字符型
#include<iostream>
using namespace std;
int main()
{
//1、字符型变量创建方式
char ch = 'a';
cout << ch << endl;
//2、字符型变量所占内存大小
cout << "字符型变量所占内存:" << sizeof(char) << endl;
//3、字符型变量常见错误
//char ch2 = "b"; //错误,创建字符型变量时要用单引号
//char ch2 = 'abcdef'; //错误,创建字符型变量时单引号内只能有一个字符
//4、字符型变量对应ASCII码
//a-97
//A-65
cout << (int)ch << endl;//把字符型强制转换为整型,打印出来97,即为a对应的ASCII码
system("pause");
return 0;
}
2.5 转义字符
#include<iostream>
using namespace std;
int main()
{
//转义字符
//换行符 \n
cout << "hello world\n";
//反斜杠 \\
cout<<"\\" << endl; //第一个反斜杠告诉编译器,要输出一个特殊的符号,这个符号是反斜杠,所以要输出两个特殊的符号
//水平制表符 \t
cout << "aaaa\thelloworld" << endl;
cout << "aa\thelloworld" << endl;
cout << "aaaaaaa\thelloworld" << endl;//作用:可以整齐输出数据
system("pause");
return 0;
}