博客
关于我
C++基础(一)数据类型
阅读量:274 次
发布时间:2019-03-01

本文共 1195 字,大约阅读时间需要 3 分钟。

常量的定义

1. #define        2.const      

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

#include 
using namespace std;#define Day 7int 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 ^ 2float f3 = 3e-2;        // 3 * 10 ^ -2cout << 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 
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

 

转载地址:http://ofzo.baihongyu.com/

你可能感兴趣的文章
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>
no connection could be made because the target machine actively refused it.问题解决
查看>>
No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
查看>>
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named 'pandads'
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No static resource favicon.ico.
查看>>