C ++编程中的sizeof()运算符操作数
sizeof()运算符
在C和C++编程语言中,sizeof()运算符用于获取数据类型的大小,表达式/变量的大小。它接受参数(数据类型或表达式)并返回运算符的大小。
sizeof()操作数可以是:
数据类型
表达式/变量
1)数据类型作为运算sizeof()符的操作数
#include <iostream>
using namespace std;
int main(){
cout << "size of char: " << sizeof(char) << endl;
cout << "size of short: " << sizeof(short) << endl;
cout << "size of int: " << sizeof(int) << endl;
cout << "size of long: " << sizeof(long) << endl;
cout << "size of float: " << sizeof(float) << endl;
cout << "size of double: " << sizeof(double) << endl;
cout << "size of long double: " << sizeof(long double) << endl;
return 0;
}输出结果
size of char: 1 size of short: 2 size of int: 4 size of long: 8 size of float: 4 size of double: 8 size of long double: 16
2)表达式/变量作为运算sizeof()符的操作数
#include <iostream>
using namespace std;
int main(){
int a = 10;
float b = 10.23f;
double c = 10.23;
char name[] = "Hello world!";
//sizeof(变量)
cout << "size of a: " << sizeof(a) << endl;
cout << "size of b: " << sizeof(b) << endl;
cout << "size of c: " << sizeof(c) << endl;
cout << "size of name: " << sizeof(name) << endl;
//sizeof(表达式)
cout << "size of 10+10: " << sizeof(10 + 10) << endl;
cout << "size of a+1: " << sizeof(a + 1) << endl;
cout << "size of a++: " << sizeof(a++) << endl;
cout << "size of \"Hello\": " << sizeof("Hello") << endl;
return 0;
}输出结果
size of a: 4 size of b: 4 size of c: 8 size of name: 13 size of 10+10: 4 size of a+1: 4 size of a++: 4 size of "Hello": 6
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短