0-hello_world
学习一门语言一开始都要做的事,编写程序打印出hello world。(尽管什么都不会)
#include<iostream> using namespace std; int main() {
cout << "hello_world" << endl;
system("pause");
return 0; }
|
1-初识
1-1:注释
在代码中加一些说明和注释,方便自己和他人阅读理解代码。
单行注释:通常放在一行代码的上方或末尾,对该行代码进行说明,格式如下
多行注释:通常放在一段代码的上方,对该段代码进行说明,格式如下
示例如下:
#include<iostream> using namespace std;
/* 打印hello_world字符串的函数 */
int main() {
cout << "hello_world" << endl; //打印出hello_world字符串
system("pause");
return 0; }
|
1-2:变量与常量
变量:给一段指定的内存空间起名,方便操作这段内存。格式如下:
示例如下:
#include<iostream> using namespace std;
int main() { int a = 114514; //给a赋值为114514
cout << "a = " << a << endl; //打印出a = 114514
system("pause");
return 0; }
|
常量:记录程序中不可更改的数据。使用**#define和const定义常量**,格式分别如下
#define 常量名 常量值 //通常在文件上方定义,宏常量
|
const 数据类型 常量名 = 常量值; //通常在变量定义前加关键字const,将变量修饰为常量
|
示例如下:
#include<iostream> using namespace std; #define week 7 //定义一周的天数
int main() { const int day_hours = 24; //定义一天有多少小时
cout << "一周有" << week << "天" << endl; cout << "一天有" << day_hours << "小时" << endl;
system("pause"); return 0; }
|
1-3:关键字
C++中预先保留的单词,在定义变量或常量时候,不要用关键字。
1-4:标识符命令规则
- 标识符不能是关键字
- 标识符只能由字母、数字、下划线组成
- 第一个字符必须为字母或下划线
- 标识符中字母区分大小写
1-5:数据类型
1-5-1:整型
表示整数类型的数据。
- short(短整型) 2字节 -32768~32767
- int(整型) 4字节 -2147483648~2147483647
- long(长整型) 4/8字节 -2147483648~2147483647
- long long(长长整型) 8字节 -9223372036854775808~ 9223372036854775807
1-5-2:sizeof关键字
利用sizeof关键字可以统计数据类型所占内存大小。格式如下
1-5-3:浮点型
用于表示小数。分两种——单精度float、双精度double
- float(单精度) 4字节 7位有效数字
- double(双精度) 8字节 15~16位有效数字
1-5-4:字符型
用于显示单个字符。格式如下:
char ch = 'a'; //显示字符型变量时,用单引号,单引号内只能有一个字符
|
1-5-5:转义字符
用于表达一些不能显示出来的ASCLL字符,常用\n(换行),\t(水平制表,即跳到下一个TAB位置)等
1-5-6:字符串类型
表示一串字符。格式如下:
char 变量名[]="字符串值" string 变量名="字符串值"
|
1-5-7:bool类型
代表真或假的值,bool类型只有两个值true或false,只要是非0的值都为真
1-5-8:数据的输入
从键盘获取数据。格式如下:
1-5的示例
#include<iostream> #include<string> using namespace std;
int main() { //整型 int a = 10; short b = 10; long c = 10; long long d = 10; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; cout << "d = " << d << endl; //sizeof关键字 cout << "short所占内存空间: " << sizeof(short) << endl; cout << "int所占内存空间:" << sizeof(int) << endl; cout << "long所占内存空间:" << sizeof(long) << endl; cout << "long long所占内存空间:" << sizeof(long long) << endl; cout << "a所占内存空间:" << sizeof(a) << endl;
//浮点型 float num1 = 3.14f; double num2 = 3.14; cout << "num1 = " << num1 << endl; cout << "num2 = " << num2 << endl; cout << "float所占内存空间:" << sizeof(float) << endl; cout << "double所占内存空间:" << sizeof(double) << endl;
//字符型 char ch = 'a'; cout << "ch = " << ch << endl; cout << "char所占内存空间:" << sizeof(char) << endl; cout << "ch的ASCLL码为:" << (int)ch << endl;
//转义字符 //换行 cout << "hello world\n"; cout << "hello world" << endl; //反斜杠 cout << "\\" << endl; //水平制表 cout << "aaaa\thello world" << endl; cout << "aaa\thello world" << endl;
//字符串类型 char str1[] = "hello world"; cout << str1 << endl; string str2 = "hello world"; cout << str2 << endl;
//bool类型 bool flag=true; cout << flag << endl; cout << "bool类型所占内存空间为:" << sizeof(flag) << endl;
//数据的输入 int num = 0; cin >> num; cout << "num=" << num << endl;
char ch1; cin >> ch1; cout << "ch1=" << ch1 << endl;
string str3; cin >> str3; cout << "str3=" << str3 << endl;
system("pause"); return 0; }
|
1-6:运算符
1-6-1:加减乘除运算