c++数据类型+基本运算语法
具体内容在代码注释
#include <iostream>
#include <string>
using namespace std;
int main() {
/*
(一)变量
变量存在的意义:方便我们管理内存空间
数据类型 变量名 = 变量的初始值;
找到一个内存空间,大小为数据类型的大小,然后将它重命名为 变量名 最后再给他赋值一个值
*/
/*
(二)变量的取名:
1.不能是关键字
2.只能由数字、字母、下划线
3.第一个字符必须为字母或者下划线
4.标识符中字母区分大小写
5.见名知义
*/
int abc = 10;
int _abc = 10;
int abc123 = 10;
int _123abc = 10;
//int 12a=10 error
/*
(三)常量:
用于记录程序中不可更改的数据
#define宏常量
const修饰的变量
关键字:
系统所用,个人不能用
*/
/*
(四)数据类型:给变量分配合适的内存空间
常见的数据类型:
1.整型:
int 4byte (-2^31-2^31-1)
short 2byte (-32789-32767)
long 4byte (-2^31-2^31-1)
long long 8byte (-2^63-2^83-1)
sizeof:求出数据类型的内存大小
sizeof(数据类型/变量名)
2.实型(浮点型):表示小数
两类:
(1)float 单精度:4字节 7位有效数字
(2)double 双精度:8字节 15-16位有效数字
但是要注意 默认情况下 输出小数时,会显示6为有效数字
3.字符型:用于显示单个字符
char ch='a' 1字节
字符型变量并不是把字符本书放到内存中存储,而是相对应的ASCII编码放入到内存存储单元中
常见ASCII a = 97,A = 65
4.转义字符
\a 警报
\b 退格
\t 水平制表 (八个位置),有个对齐的效果 看起来很整齐
\n 换行
\r 回车,将当前位置移到本行开头
\\ 反斜杠
\' 代表一个单引号
\" 代表一个双引号
\? 代表一个问号
4.字符串型:用于表示一串字符
1)C风格字符串: char 变量名 = "字符串值"
2) string 变量名 = "字符串" 需要引入头文件 <string>
5.boolean类型:
大小 1 字节
true 真 1
false 假 0
*/
cout << "-----------------浮点型------------------------" << endl;
float f1 = 3.14f;//不加f为双精度
double f2 = 3.1415876688655;
float f3 = 3e2;//科学计数法 2*10^2
float f4 = 3e-2;//科学计数法 3*10^-2
cout << "f1= " << f1 << endl;
cout << "f2= " << f2 << endl;
cout << "--------------------字符型-----------------------" << endl;
char ch1 = 'a';//切记 只能是单引号,只能有一个字符
cout << int(ch1) << endl;//查看ascii的值
cout << "----------------------字符串-------------------------" << endl;
char str1[] = "hello world";
string str2 = "hello world";
cout << "str1=" << str1 << endl;
cout << "str2=" << str2 << endl;
cout << "----------------------------boolean-----------------------" << endl;
bool flag = true;
cout << flag << endl;
flag = false;
cout << flag << endl;
cout << "--------------sizeof测大小 ---------------------" << endl;
const int a = 10;
short b = 10;
long c = 10;
long long d = 10;
cout << " int a " << sizeof(a) << endl;//4
cout << " short b " << sizeof(b) << endl;//2
cout << " long c " << sizeof(c) << endl;//4
cout << " long long d " << sizeof(d) << endl;//8
cout << " float f1 " << sizeof(f1) << endl;
cout << " double f2 " << sizeof(f2) << endl;
cout << " char ch1 " << sizeof(ch1) << endl;
cout << "bool flag " << sizeof(flag) << endl;
cout << "-------------------------------------------------" << endl;
cout << "a=" << a << endl;
cout << "hellow world" << endl;
system("pause");
return 0;
}
标准输入和输出
#include <iostream>
#include <string>
using namespace std;
/*
标准输入输出:
cout<<
cin>>
*/
int main() {
//1.整型:
int a = 0;
cout << "请输入变量的a的值" << endl;
cin >> a;
cout << "a = " << a << endl;
//2.浮点型
float f = 3.13f;
cout << "请给f赋值" << endl;
cin >> f;
cout << "f= " << endl;
//3.字符型:
char ch = 'a';
cout << "请给ch赋值" << endl;
cin >> ch;
cout << "ch=" << endl;
//4.字符串
string str = "hello";
cout << "请给str赋值" << endl;
cin >> str;
cout << "str=" << endl;
//5.布尔值:
bool flag = false;
cout << "请给flag赋值" << endl;
cin >> flag;
cout << "flag=" << endl;
system("pause");
return 0;
}
运算符操作:
#include <iostream>
#include <string>
using namespace std;
/*运算符:C++中除了 0 都为真
1.+ - * /:
2.% 两个小数不能进行取余运算。
3.++递增
前置递增 ++i:先+1 在运算
后置递增 i++ 先运算 再+1
4.赋值运算
=
+=
-=
*=
/=
%=
5.比较运算符
==、!=
<、>
<=,>=
返回结果为Boolean值
6.逻辑运算符:
! && ||
*/
int main() {
int a = 10;
int b = 12;
cout << "---------------①算术运算符---------------" << endl;
//+:
cout << "a+b=" << a + b << endl;
//-:
cout << "a-b=" << a - b << endl;
//*:
cout << "a*b=" << a * b << endl;
// /:
cout << "a/b=" << a / b << endl;//除数不能为0,会报错
//%:求余数
cout << "a%b" << a % b << endl;
cout<<a++<<endl;
cout << "++b" << endl;
cout << "----------------4.赋值运算--------------------------" << endl;
a += 2;//a=a+2
a -= 2;//a=a-2
a *= 2;//a=a*2
a /= 2;//a=a/2;
a %= 2;//a=a%2;
cout << "------------------------比较运算符----------------------" << endl;
cout << "a=" << a << endl;
cout << "a==b" << (a == b) << endl;
cout << "a!=b" << (a != b) << endl;
cout << "a<b" << (a < b) << endl;
cout << "a>b" << (a > b) << endl;
cout << "-------------------------逻辑运算符----------------" << endl;
cout << "!a" << !a << endl;
cout << "!!a" << !!a << endl;
cout << "a&&b" << (a && b) << endl;
cout << "a||b" << (a || b) << endl;
system("pause");
return 0;
}