初识c++
认识c++数据类型
数据类型
c++规定在创建变量或者是常量的时候,必须要指定相应的数据类型,否则无法给变量分配内存。
整型
作用:整型变量表示的是整型类型的数据。
区别在于所占的内存空间不同:
- 实例:
#include<iostream>
using namespace std;
int main() {
//整型
//短整型(-32768~32767)
short num1 = 10;
//整型
int num2 = 10;
//长整型
long num3 = 10;
//长长整型
long long num4 = 10;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
cout << "num3 = " << num3 << endl;
cout << "num4 = " << num4 << endl;
system("pause");
return 0;
}
num1 = 10
num2 = 10
num3 = 10
num4 = 10
请按任意键继续. . .
sizeof关键字
- 作用:利用sizeof关键字可以统计数据类型所占的内存大小。
- 语法:sizeof(数据类型 / 变量)
- 实例:
#include<iostream>
using namespace std;
int main() {
//short(2) int(4) long(4) long long(8)
//语法:sizeof(数据类型/变量名)
short num1 = 10;
cout << "short占用的内存空间:" << sizeof(short) << endl;
int num2 = 29;
cout << "int 所占用的内存空间:" << sizeof(num2) << endl;
long num3 = 90;
cout << "long所占用的内存空间:" << sizeof(long) << endl;
long long num4 = 78;
cout << "long long 所占用的内存空间:" << sizeof(num4) << endl;
//整型大小比较
//short < int <= long <= long long
system("pause");
return 0;
}
short占用的内存空间:2
int 所占用的内存空间:4
long所占用的内存空间:4
long long 所占用的内存空间:8
请按任意键继续. . .
实型(浮点型)
- 作用:用于表示小数
- 浮点型变量分为两种:
单精度:float
双精度:double - 实例:
#include<iostream>
using namespace std;
int main() {
//1.单精度:float
//2.双精度:double
//默认情况下,输出一个小数,会显示6位有效数字。
float f1 = 3.1415926f;
cout << "f1 = " << f1 << endl;
double d1 = 3.1415926;
cout << "d1 = " << d1 << endl;
//统计float和double占用的空间
cout << "float占用的内存空间:" << sizeof(float) << endl;
cout << "double占用的内存空间:" << sizeof(double) << 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;
}
f1 = 3.14159
d1 = 3.14159
float占用的内存空间:4
double占用的内存空间:8
f2 = 300
f3 = 0.03
请按任意键继续. . .
字符型
- 作用:字符型变量用于显示单个字符
- 语法:char ch = ‘a’;
注意:在显示字符型变量时,用单引号将字符括起来,不要用双引号。
注意:单引号内只可以有一个字符,不可以是字符串。
- c和c++中字符型变量只能占用一个字节。
- 字符型变量并不是把字符本身放到内存中存储,而是将对应的ASCII编码放入到存储单元中。
- 实例:
#include<iostream>
using namespace std;
int main() {
//字符型变量创建的方式
char ch = 'a';
cout << ch << endl;
//字符型变量所占的内存大小
cout << "char字符型变量所占的内存:" << sizeof(char) << endl;
//字符型变量常见的错误
//char ch2 = "b";//创建字符型变量的时候,要用单引号。
//char ch2 = 'sjkdjfk';//创建字符型变量的时候,单引号中只能有一个字符。
//字符型变量对应的ASCII编码
cout << (int)ch << endl;//a-97 A-65
system("pause");
return 0;
}
a
char字符型变量所占的内存:1
97
请按任意键继续. . .
具体ASCII码表请访问:ASCII码表
转义字符
- 作用:用于表示一下不能显示出来的ASCII字符
- 现阶段我们常用的转义字符:\n换行
\\
返回一个“\” \t制表符 - 实例:
#include<iostream>
using namespace std;
int main() {
//转义字符
//换行符\n
cout << "hello world\n";//等价于cout << "hello world" << endl;
//反斜杠\\
//cout << "\\" << endl;
//水平制表符\t 作用:可以整齐的输出字符,一共有8个占位
cout << "aaa\thello world" << endl;
cout << "aaaa\thello world" << endl;
cout << "a\thello world" << endl;
system("pause");
return 0;
}
hello world
aaa hello world
aaaa hello world
a hello world
请按任意键继续. . .
字符串型
- 作用:用于表示一串字符
- 两种风格:
1.C语言风格:char 变量名[ ] = “字符串值”; - 实例:
#include<iostream>
using namespace std;
int main() {
//C语言风格
/*
注意事项:char 字符串名[] = "字符串的值";
等号后面要用双引号包含字符串
*/
char str[] = "hello world";
cout << str << endl;
system("pause");
return 0;
}
hello world
请按任意键继续. . .
C语言的风格的字符串要用双引号引起来
2.c++风格字符串:string 变量名 = “字符串值”;
- 实例:
#include<iostream>
using namespace std;
#include<string>
int main() {
//c++风格
//要包含一个头文件#include<string>
string str1 = "hello world";
cout << str1 << endl;
system("pause");
return 0;
}
hello world
请按任意键继续. . .
c++风格的字符串,需要加入头文件
#include<string>
布尔类型bool
- 作用:布尔类型代表真或者假
- 语法:bool 变量名 = true/false;
- bool类型只有两个值:
true–>真(本质是1)
false–>假(本质是0)
bool类型占1个字节的大小 - 实例:
#include<iostream>
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(flag) << endl;
system("pause");
return 0;
}
1
0
bool类型所占空间:1
请按任意键继续. . .
数据的输入
- 作用:用于从键盘获取数据
- 关键字:cin
- 语法:cin >> 变量;
- 实例:
#include<iostream>
#include<string>
using namespace std;
int main() {
//int
int a = 0;
cout << "请给整型变量a赋值" << endl;
cin >> a;
cout << "a = " << a << endl;
//float
float f = 3.14f;
cout << "请给浮点型变量f赋值:" << endl;
cin >> f;
cout << "浮点数f = " << f << endl;
//char
char ch = 'a';
cout << "请给ch赋值:" << endl;
cin >> ch;
cout << "ch = " << ch << endl;
//string
string str = "hello";
cout << "请给字符串str赋值:" << endl;
cin >> str;
cout << "str = " <<str<< endl;
//bool
bool flag = false;
cout << "请给bool类型的flag赋值:" << endl;//除了0表示假,其他都表示真。
cin >> flag;
cout << "flag = " << flag << endl;
system("pause");
return 0;
}
请给整型变量a赋值
10
a = 10
请给浮点型变量f赋值:
2.34
浮点数f = 2.34
请给ch赋值:
a
ch = a
请给字符串str赋值:
akgkjk
str = akgkjk
请给bool类型的flag赋值:
100
flag = 1
请按任意键继续. . .
下节:c++运算符