C++基础(一)数据类型

常量的定义

1. #define        2.const      

注意     #define   的形式不要在结尾写分号了

#include <iostream>
using namespace std;

#define Day 7

int main() 
{
	const int month = 12;
	cout <<"一周有 " << Day << "天"<< endl;
	cout << "一年有 " << month << "个月" << endl;
	system("pause");
	return 0;
}

数据类型

1. 整形  

short         短整型       2字节(16bit)

int             整形           4字节

long         长整型   win4字节  linux32 4字节 linux64 8字节

long long 长长整形      8字节

sizeof 的用法     sizeof(数据类型/变量)

short m = 12345;
cout << sizeof(int) << sizeof(m) << endl;

2.浮点型  默认情况先输出一个小数 会显示6位有效数字

float        单精度        4字节

double    双精度        8字节

注意: float 定义的变量后面需要添加一个f

float f1 = 1.34f;
double d1 = 3.14;
cout << f1 << d1 << endl;

科学计数法

float f2 = 3e2;         // 3 * 10 ^ 2
float f3 = 3e-2;        // 3 * 10 ^ -2
cout << f2 << f3 << endl;

3.字符型 单个字母

char ch = 'a';

注意 字符型只占用1个字节

将对应的ASCII编码存入对应的存储单元

a----97   A----65

char ch1 = 'a';
char ch2 = 'A';
cout << ch1 << ch2 << endl;
cout << (int)ch1 << (int)ch2 << endl;

4.转义字符

\n    换行     \t    水平制表符 (控制8个空格 对齐)

5.字符串类型

c语言风格    char str[] = "godv";

c++              string str = "godv";         注意需要包含头文件    #include<string>

#include <string>

char str[] = "godv";
string str1 = "godv";
cout << str << str1 << endl;

6.布尔类型 bool

bool flag = true;   本质上0为false        其他都为true

占用 1 字节

bool flag = true;
bool flag1 = false;
cout << flag << flag1 << endl;    // 1   0

猜你喜欢

转载自blog.csdn.net/we1less/article/details/108561293