运算符sizeof_C ++编程中的sizeof()运算符操作数

运算符sizeof

sizeof()运算符 (sizeof() operator)

In the C and C++ programming language, sizeof() operator is used to get the size of a data type, size of an expression/variable. It accepts a parameter (either data type or expression) and returns the size of the operator.

在C和C ++编程语言中, sizeof()运算符用于获取数据类型的大小, 表达式/变量的大小。 它接受一个参数(数据类型或表达式)并返回运算符的大小。

sizeof() operands can be:

sizeof()操作数可以是:

  1. Data type

    数据类型

  2. Expression/Variable

    表达式/变量

1)数据类型作为sizeof()运算符的操作数 (1) Data type as an operand of sizeof() operator)

#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;
}

Output

输出量

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()运算符的操作数 (2) Expression/Variable as an operand of sizeof() operator)

#include <iostream>
using namespace std;

int main()
{
    int a = 10;
    float b = 10.23f;
    double c = 10.23;
    char name[] = "Hello world!";

    //sizeof(variable)
    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(expression)
    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;
}

Output

输出量

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


翻译自: https://www.includehelp.com/cpp-tutorial/sizeof-operator-operands.aspx

运算符sizeof

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值