Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      sizeof operator

      From cppreference.com
      <c‎ |language
       
       
       
       

      Queries size of the object or type.

      Used when actual size of the object must be known.

      Contents

      [edit]Syntax

      sizeof(type) (1)
      sizeofexpression (2)

      Both versions return a value of typesize_t.

      [edit]Explanation

      1) Returns the size, in bytes, of theobject representation oftype
      2) Returns the size, in bytes, of the object representation of the type ofexpression. No implicit conversions are applied toexpression.

      [edit]Notes

      Depending on the computer architecture, abyte may consist of 8 or more bits, the exact number provided asCHAR_BIT.

      sizeof(char),sizeof(signedchar), andsizeof(unsignedchar) always return1.

      sizeof cannot be used with function types, incomplete types (includingvoid), orbit-field lvalues.

      When applied to an operand that hasstructure orunion type, the result is the total number of bytes in such an object, including internal and trailing padding. The trailing padding is such that if the object were an element of an array, the alignment requirement of the next element of this array would be satisfied, in other words,sizeof(T) returns the size of an element of aT[] array.

      Iftype is aVLA type and changing the value of itssize expression would not affect the result ofsizeof, it is unspecified whether or notthe size expression is evaluated.

      (since C99)

      Except if the type ofexpression is aVLA,(since C99)expression is not evaluated and thesizeof operator may be used in an integerconstant expression.

      If the type ofexpression is avariable-length array type,expression is evaluated and the size of the array it evaluates to is calculated at run time.

      (since C99)

      Number of elements in anyarrayaincluding VLA(since C99) may be determined with the expressionsizeof a/sizeof a[0]. Note that ifa has pointer type (such as after array-to-pointer conversion of function parameter type adjustment), this expression would simply divide the number of bytes in a pointer type by the number of bytes in the pointed type.

      [edit]Keywords

      sizeof

      [edit]Example

      Sample output corresponds to a platform with 64-bit pointers and 32-bit int

      Run this code
      #include <stdio.h> int main(void){short x;// type argument:printf("sizeof(float)          = %zu\n",sizeof(float));printf("sizeof(void(*)(void))  = %zu\n",sizeof(void(*)(void)));printf("sizeof(char[10])       = %zu\n",sizeof(char[10]));//  printf("sizeof(void(void))     = %zu\n", sizeof(void(void))); // Error: function type//  printf("sizeof(char[])         = %zu\n", sizeof(char[])); // Error: incomplete type // expression argument:printf("sizeof 'a'             = %zu\n",sizeof'a');// type of 'a' is int//  printf("sizeof main            = %zu\n", sizeof main); // Error: Function typeprintf("sizeof &main           = %zu\n",sizeof&main);printf("sizeof\"hello\"         = %zu\n",sizeof"hello");// type is char[6]printf("sizeof x               = %zu\n",sizeof x);// type of x is shortprintf("sizeof (x+1)           = %zu\n",sizeof(x+1));// type of x+1 is int}

      Possible output:

      sizeof(float)          = 4sizeof(void(*)(void))  = 8sizeof(char[10])       = 10sizeof 'a'             = 4sizeof &main           = 8sizeof "hello"         = 6sizeof x               = 2sizeof (x+1)           = 4

      [edit]References

      • C23 standard (ISO/IEC 9899:2024):
      • 6.5.3.4 The sizeof and alignof operators (p: TBD)
      • C17 standard (ISO/IEC 9899:2018):
      • 6.5.3.4 The sizeof and _Alignof operators (p: TBD)
      • C11 standard (ISO/IEC 9899:2011):
      • 6.5.3.4 The sizeof and _Alignof operators (p: 90-91)
      • C99 standard (ISO/IEC 9899:1999):
      • 6.5.3.4 The sizeof operator (p: 80-81)
      • C89/C90 standard (ISO/IEC 9899:1990):
      • 3.3.3.4 The sizeof operator

      [edit]See also

      C++ documentation forsizeof operator
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=c/language/sizeof&oldid=154510"

      [8]ページ先頭

      ©2009-2025 Movatter.jp