Device Monitoring Studio - Monitor, log and analyze data coming through PC ports and connections
Download Device Monitoring Studio Hide this button

sizeof() Operator

This operator returns the size, in bytes, of the enclosed identifier.

Syntax:

sizeof(id)

id may be either a built-in type, typedef'ed type, user-defined type or field reference. Taking the size of user-defined dynamic type is incorrect and will result in compile error.

The following code fragment illustrates the use of the sizeof() operator:

struct A
{
    int a;
    int b;
    int c[4];
};

struct B
{
    int a;
    int b[a];
};

struct C
{
    B b;

    const SizeOfInt = sizeof(int);      // correct, evaluates to 4
    const SizeOfA = sizeof(A);          // correct, evaluates to 24 (size of static user-defined type A)
    const SizeOfB = sizeof(B);          // incorrect, results in compile-time error, B is dynamic type
    var SizeOfb = sizeof(b);          // correct, will be calculated at run-time
};