C++ sizeof 运算符的使用
生活随笔
收集整理的這篇文章主要介紹了
C++ sizeof 运算符的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
sizeof? 是C++ 中 唯一的一個帶字母的運算符,
sizeof 的功能:
返回指定的數據類型或者表達式值的數據類型在內存中所占的字節數.
sizeof? 運算符的使用
1 sizeof(類型)
2 sizeof(表達式)
demo 說明下2中用法
#include <iostream>
using namespace std;
int main()
{cout <<sizeof(int)<< endl; //打印結果為4,說明int類型占4個字節cout <<sizeof(float)<< endl;//打印結果為4,說明float類型占4個字節cout <<sizeof(char)<< endl;//打印結果為1,說明char類型占1個字節cout <<sizeof(99)<< endl;//打印結果為4,說明99占4個字節cout <<sizeof("A")<< endl;//打印結果為2,說明字母A占2個字節return 0;};
當然我們可以使用使用sizeof 計算數組的長度等,
下面是計算數組的長度
#include <iostream>
using namespace std;
int main()
{int aar[] = {1, 2, 3, 4, 5, 6};cout << sizeof(aar) / sizeof(aar[0]) << endl;return 0;
};
總結
以上是生活随笔為你收集整理的C++ sizeof 运算符的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 求长开头成语接龙!
- 下一篇: 溜冰鞋的一般价格是多少钱一双