#include<iostream>
using namespace std;
int main()
{
//变量创建语法: 数据类型 变量名 = 变量初始值
int a = 10;
cout << "a = " << a << endl;
system("pause");
return 0;
}
#include<iostream>
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;
//month = 24; //错误,const修饰的变量也是常量
cout << "一年有:" << month << "个月份" << endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main03()
{
//创建变量:数据类型 变量名称 = 变量初始值;
//不要用关键字给变量或者常量起名
//int int = 10; // 错误, 第二个是int关键字,不可以作为变量的名称
system("pause");
return 0;
}
#include<iostream>
using namespace std;
//标识符命名规则
//标识符不可以关键字
//标识符由字母、数字、关键字构成
//标识符第一个字母只能是字母或者下划线
//标识符区分大小写
int main04()
{
//标识符不可以关键字
//int int = 10;//错误
//标识符由字母、数字、关键字构成
int abc = 10;
int _abc = 20;
int _123abc;
//标识符第一个字母只能是字母或者下划线
//int 123_abc;//错误
//标识符区分大小写
int aaa = 100;
//cout << AAA << 100 << endl; //错误 AAA 与aaa 不是同一个名称
// 建议:给变量起名最好见名知义
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
cout << sum << endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main05()
{
//整型
//1.短整型(-32768 ~ 32768)
short num1 = 10;
short num1 = 32768;
//2.整型
int num2 = 32768;
//3.长整型
long num3 = 10;
//4.长长整型
long long num4 = 10;
cout << "num1 = " << num1 << endl;
cout << "num2 = " << num2 << endl;
cout << "num3 = " << num3 << endl;
cout << "num4 = " << num4 << endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
//整型
//1.短整型 short (2字节) 2.整型 int(4字节) 3.长整型 long(win 4字节 linux 32-4 64 - 8) 4.长长整型 long long (8字节)
//可以利用sizeof求出数据类型占用内存大小
//语法 : sizeof(数据类型或者变量)
short num1 = 10;
cout << "short占用内存空间" << sizeof(num1) << endl;
int num2 = 32768;
cout << "int占用内存空间" << sizeof(num2) << endl;
long num3 = 10;
cout << "long占用内存空间" << sizeof(num3) << endl;
long long num4 = 10;
cout << "long long占用内存空间" << sizeof(num4) << endl;
//整型大小 short < int <= long <= long long
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
//浮点型
//1.单精度 float
//2.双精度 double
float f1 = 3.1415926f;
cout << "f1 = " << f1 << endl;
double d1 = 3.1415926;
cout << "d1 = " << f1 << endl;
//统计float 核 double占用内存空间
cout << "float占用内存空间" << sizeof(f1) << endl;//4字节
cout << "double占用内存空间" << sizeof(d1) << endl;//8字节
//科学计数法
float a = 3e2f;//3 * 10 ^2
cout << "a = " << a << endl;
float b = 3e-4;
cout << "b = " << b << endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
//1.字符型常量创建方式
char ch = 'a';
cout << "ch = " << ch << endl;
//2.字符型变量占用内存空间
cout << "char占用内存空间" << sizeof(char) << endl;//1字节
cout << "char占用内存空间" << sizeof(ch) << endl;
//3.字符型变量常见错误
//char ch2 = "b";// 错误 创建字符型变量时,要用单引号
//char ch3 = 'abcdfd';//错误 创建字符型变量时,单引号内部只能有一个值
//4.字符型变量对应ASCII编码
//a 对应 97 A对应 65
cout << (int)'A' << endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
//转义字符
//换行符 \n
cout << "helloword\n";
//反斜杠
cout << "\\\n";
//水平制表符 \t
cout << "aaa\thelloword" << endl;
cout << "aaaa\thelloword" << endl;
cout << "aa\thelloword" << endl; // \t 作用是可以整齐的输出数据
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1.C风格字符串
//char ch[] = "字符串值";
//注意事项1 char 字符串名[]
//注意事项2 等号后边要用双引号 包含起来字符
char ch[] = "helloword";
cout << ch << endl;
//C++风格字符串 使用时必须加入头文件 #include<string>
//string 变量名 =“字符串值”
string str = "helloworld";
cout << str << endl;
system("pause");
return 0;
}
#include<iostream>
using namespace std;
int main()
{
//1.创建布尔数据类型
bool flag = true; //true 代表真
cout << flag << endl;
bool flag1 = true; //flase 代表假
cout << flag1 << endl;
//本质上 1代表真 0 代表假
//2.布尔类型所占内存空间
cout << "布尔类型所占空间" << sizeof(bool) << endl;
system("pause");
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
//1.整型
int a = 0;
cout << "请给整型变量a赋值:" << endl;
cin >> a;
cout << a << endl;
//2.浮点型
float f = 0;
cout << "请给浮点型变量a赋值:" << endl;
cin >> f;
cout << f<< endl;
//3.字符型
char ch = 'a';
cout << "请给整型变量ch赋值:" << endl;
cin >> ch;
cout << ch << endl;
//4.字符串型
char ch1[] = "helloworld";
string str = "dkjakld";
cout << "请给字符串ch1[]赋值:" << endl;
cin >> ch1;
cout << ch1 << endl;
cout << "请给字符串str赋值:" << endl;
cin >> str;
cout << str << endl;
//5.布尔类型
bool flag = false; //布尔类型只要输入非0,都代表真
cout << "请给布尔类型赋值:" << endl;
cin >> flag;
cout << flag << endl;
system("pause");
return 0;
}
。
注:1.两个数相除,除数不能为0
2.两个小数可以相除,运算结果也可以是小数
1.cout << a2%b2 <<endl; // 取模运算时 ,除数也不能为0
2.两个小数比可以取模运算
3.只有两个整数才可以取模运算