網站首頁 編程語言 正文
前言
C++不像python,創建變量的時候必須指定類型,這樣才能給變量分配一個合適的內存空間。
1 整型
作用:整型變量表示的是整型類型的數據
整型的數據類型有4種(最常用的是int),其區別在于所占內存空間不同:
#include<iostream> using namespace std; int main() { //整型 //1.短整型 short num1 = 32768; //2.整型 int num2 = 10; //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; }
因為短整型取值范圍為-32768-32767,所以注意數值溢出,當數值溢出時,取補碼。
當如下定義時:
short num1 = 32768
輸出為
num1=-32768
2 sizeof關鍵字
作用:利用siezeof關鍵字可以統計數據類型所占內存大小語法:
sizeof{數據類型/變量}
#include<iostream> using namespace std; int main() { //利用sizeof求出數據類型占用大小 short num1 = 10; int num2 = 10; long num3 = 10; long long num4 = 10; cout << "short占用內存空間為:" << sizeof(short) << endl; cout << "num1占用內存空間為:" << sizeof(num1) << endl; cout << "int占用內存空間為:" << sizeof(int) << endl; cout << "num2占用內存空間為:" << sizeof(num2) << endl; cout << "long占用內存空間為:" << sizeof(long) << endl; cout << "num3占用內存空間為:" << sizeof(num3) << endl; cout << "long long占用內存空間為:" << sizeof(long long) << endl; cout << "num4占用內存空間為:" << sizeof(num4) << endl; system("pause"); return 0; }
3 實型(浮點型)
作用:用于表示小數
浮點型變量分為兩種:
- 單精度float, 雙精度double
- 區別在于表示的有效數字范圍不同。
在使用時,使用方法通常為
float f1 = 3.14f
如果不加f,默認是double型變量:
#include<iostream> using namespace std; int main() { //默認情況下,輸出一個小數,會顯示最多6位有效數字 //1.單精度 float f1 = 3.1415926f; cout << "f1=" << f1 << endl; //2.雙精度 double d1 = 3.1415926; cout << "d1=" << d1 << endl; //占用內存查看 cout << "float占用內存空間為:" << sizeof(float) << endl; cout << "double占用內存空間為:" << sizeof(double) << endl; //科學計數法 float f2 = 3e2f; cout << "f2=" << f2 << endl; float f3 = 3e-2f; cout << "f3=" << f3 << endl; system("pause"); return 0; }
4 字符型
作用:字符變量用于顯示單個字符
語法:char ch=‘a’;
1.C和C++中字符型變量只占用1個字節。
2.字符型變量并不是把字符本身放到內存中存儲,而是將對應的ASCII編碼放入到存儲單元。
注:用單引號不要用雙引號;單引號內只能有一個字符,不可以是字符串。
#include<iostream> using namespace std; int main() { //字符型變量創建方式 char ch = 'a'; cout << ch << endl; //字符型變量所占內存大小 cout << "char字符型變量所占內存:" << sizeof(char) << endl; //字符型變量常見錯誤 // char ch2="b"; // char ch2='abc'; //字符型變量對應ASCII編碼 cout <<"字符A的ASCII碼值為:"<<(int)'A' << endl; cout << "變量ch的ASCII碼值為:" << (int)ch << endl; system("pause"); return 0; }
5 轉義字符
作用:用于表示一些不能顯示出來的ASCII字符
常用的就下面這些,其余可自行百度
語法:使用cout時直接加在字符串中。
#include<iostream> using namespace std; int main() { //換行符 \n cout << "hello world\n"<<endl; //反斜杠 \ cout << "\\" << endl; /*水平制表符 \t 使用空格補齊位置,使得所占位置為8的倍數 輸入正好為8個時,輸出會多8個空格 */ cout << "aaaa\ttheworld" << endl; cout << "aaa\ttheworld" << endl; cout << "aaaaaaaa\ttheworld" << endl; cout << "aaaaaaaaa\ttheworld" << endl; cout << "aaaaaaaaaaa\ttheworld" << endl; system("pause"); return 0; }
6 字符串型
作用:用于表示一串字符。
兩種風格
1.C風格的字符串:char 變量名[ ] = “字符串值”; ——注意加[ ],不加[ ]的時候默認的是字符。
2.C++風格字符串:string 變量名 = “字符串值”;——注意加頭文件#include
#include<iostream>#include<string>using namespace std;int main(){//1.C風格字符串,注意加中括號[]char str[] = "hello world";cout << str << endl;//2.C++風格字符串,注意加頭文件#include<string>string str2 = "hello world";cout << str2 << endl;system("pause");return 0;}#include<iostream> #include<string> using namespace std; int main() { //1.C風格字符串,注意加中括號[] char str[] = "hello world"; cout << str << endl; //2.C++風格字符串,注意加頭文件#include<string> string str2 = "hello world"; cout << str2 << endl; system("pause"); return 0; }
7 布爾類型 bool
作用:布爾數據類型代表真或假的值
bool類型只有兩個值:
1.true 真
2.false 假
bool類型占一個字節
#include<iostream>#include<string>using namespace std;int main(){//1.創建bool數據類型bool flag = true;cout << flag << endl;flag = false;cout << flag << endl;//本質是1就是真,0就是假。//2.查看bool類型所占內存空間cout <<"bool類型所占內存空間為:" << sizeof(bool) << endl;system("pause");return 0;}#include<iostream> #include<string> using namespace std; int main() { //1.創建bool數據類型 bool flag = true; cout << flag << endl; flag = false; cout << flag << endl; //本質是1就是真,0就是假。 //2.查看bool類型所占內存空間 cout <<"bool類型所占內存空間為:" << sizeof(bool) << endl; system("pause"); return 0; }
8 數據的輸入
作用:從鍵盤獲取數據
關鍵字:cin
語法:
cin >> 變量
#include<iostream> #include<string> using namespace std; int main() { //1.整型 int a = 0; //盡量初始化,如果不初始化在使用或者打印它時都會報錯。 cout << "請給整型變量a賦值:" << endl; cin >> a; cout << "整型變量a=" << a << endl; //2.浮點型 float f = 0.f; cout << "請給浮點型變量f賦值:" << endl; cin >> f; cout << "浮點型變量f=" << f << endl; //3.字符型 char ch = ' '; cout << "請給字符型變量ch賦值:" << endl; cin >> ch; cout << "字符型變量f=" << ch << endl; //4.字符串型 string str = "abc"; cout << "請給字符串型變量str賦值:" << endl; cin >> str; cout << "字符串型變量str=" << str << endl; //5.布爾型,用數字表示真假,只要輸入不是0,那么就是1 bool flag = false; cout << "請給布爾型變量flag賦值:" << endl; cin >> flag; cout << "布爾型變量flag=" << flag << endl; system("pause"); return 0; }
總結
原文鏈接:https://blog.csdn.net/qq_49030008/article/details/122796292
相關推薦
- 2022-02-11 安裝element UI (全局引入與按需引入)
- 2022-03-15 this.$cookie.set(‘token‘, data.token) token賦值失效
- 2022-04-14 解決Mac環境下zsh: command not found:
- 2022-08-20 Redis+AOP+自定義注解實現限流_Redis
- 2022-08-29 Python使用re模塊實現正則表達式操作指南_python
- 2022-04-01 6個實用的Python自動化腳本詳解_python
- 2022-08-19 WPF使用Geometry繪制幾何圖形_C#教程
- 2022-03-23 C++?Boost?PropertyTree解析INI文件詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支