C++学习笔记(1):常量、整型和实型、字符和字符串
2021/11/14 11:40:05
本文主要是介绍C++学习笔记(1):常量、整型和实型、字符和字符串,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
文章目录
- 一、常量define和const
- 二、整型(short、int、long、long long)和实型(float、double)
- 三、字符和字符串
一、常量define和const
#include <iostream> using namespace std; // 常量:用于记录程序中不可更改的数据 //1 #define 宏常量 常量值 #define Day 7 int main() { // Day = 8;//会报错 //2 const修饰变量 const int month = 12; return 0; }
二、整型(short、int、long、long long)和实型(float、double)
整型:用于表示整数
#include <iostream> using namespace std; int main() { //整型:1字节=8位 //1 short 2字节(-2^15-2^15-1)即-32768-32767 short num1 = 32769; cout<<"short num1="<<num1<<endl;//-32767 超出范围 //2 int 4字节 (-2^31-2^31-1) //3 long win32 4字节 ;win64 8字节;(-2^31-2^31-1) //4 long long 8字节(-2^63-2^63-1) return 0; }
实型:用于表示小数
#include <iostream> using namespace std; int main() { // 1 单精度 float float f1 = 3.1415926f; // 默认输出6位有效数字 cout << "f1 = "<<f1<<endl;//f1 = 3.14159 cout << "f1 size= "<<sizeof(f1)<<endl;//f1 size= 4 // 2 双精度 double double d1 = 3.1415926; // 默认输出6位有效数字 cout << "d1 = "<<d1<<endl;//d1 = 3.14159 cout << "d1 size= "<<sizeof(d1)<<endl;//d1 size= 8 return 0; }
三、字符和字符串
#include <iostream> //需要引用<string> #include <string> using namespace std; int main() { // 字符型变量:用于显示单个字符,占用一个字节,只能用单引号 char ch = 'a'; cout << "ch = "<<ch<<endl;//ch = a cout << "Ascall(a) = "<<int(ch)<<endl;//Ascall(a) = 97 char ch1 = 'A'; cout << "Ascall(A) = "<<int(ch1)<<endl;//Ascall(a) = 65 // 字符串变量 string str1 = "Hello world!"; cout <<str1<<endl;//Hello world! return 0; }
这篇关于C++学习笔记(1):常量、整型和实型、字符和字符串的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-07fastcgi 是什么-icode9专业技术文章分享
- 2024-10-07fastcgi 的详细使用教程介绍-icode9专业技术文章分享
- 2024-10-07git如何更新单个文件到本地-icode9专业技术文章分享
- 2024-10-07如何使用ASM(Abstract Syntax Tree Manipulation)技术来修改第三方AAR依赖中的函数-icode9专业技术文章分享
- 2024-10-07Activity 跳转时间耗时很长怎么优化解决-icode9专业技术文章分享
- 2024-10-07Androud Toast 有哪些常用的第三方组件-icode9专业技术文章分享
- 2024-10-07在viewmodel中怎么使用 mmkv?-icode9专业技术文章分享
- 2024-10-07MMKV.defaultMMKV() 是单例模式吗?-icode9专业技术文章分享
- 2024-10-04el-table 开启定时器下,表格的选中状态会消失是什么原因-icode9专业技术文章分享
- 2024-10-03如何安装和初始化飞牛私有云 fnOS?-icode9专业技术文章分享