From 754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 7 Apr 2024 13:41:34 -0500 Subject: new repository --- devdocs/c/language%2Fsizeof.html | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 devdocs/c/language%2Fsizeof.html (limited to 'devdocs/c/language%2Fsizeof.html') diff --git a/devdocs/c/language%2Fsizeof.html b/devdocs/c/language%2Fsizeof.html new file mode 100644 index 00000000..afac1a41 --- /dev/null +++ b/devdocs/c/language%2Fsizeof.html @@ -0,0 +1,69 @@ +

sizeof operator

Queries size of the object or type.

+

Used when actual size of the object must be known.

+

Syntax

+ + +
sizeof( type ) (1)
sizeof expression (2)

Both versions return a value of type size_t.

+

Explanation

+1) Returns the size, in bytes, of the object representation of type +
+2) Returns the size, in bytes, of the object representation of the type of expression. No implicit conversions are applied to expression.

Notes

Depending on the computer architecture, a byte may consist of 8 or more bits, the exact number provided as CHAR_BIT.

+

sizeof(char), sizeof(signed char), and sizeof(unsigned char) always return 1.

+

sizeof cannot be used with function types, incomplete types (including void), or bit-field lvalues.

+

When applied to an operand that has structure or union 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 a T[] array.

+ + +

If type is a VLA type and changing the value of its size expression would not affect the result of sizeof, it is unspecified whether or not the size expression is evaluated.

+
(since C99)

Except if the type of expression is a VLA,(since C99)expression is not evaluated and the sizeof operator may be used in an integer constant expression.

+ + +

If the type of expression is a variable-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 any array a including VLA(since C99) may be determined with the expression sizeof a / sizeof a[0]. Note that if a 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.

+

Keywords

sizeof

+

Example

+

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

+
#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 type
+    printf("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 short
+    printf("sizeof (x+1)           = %zu\n", sizeof(x + 1)); // type of x+1 is int
+}

Possible output:

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

References

See also

+
C++ documentation for sizeof operator
+

+ © cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
+ https://en.cppreference.com/w/c/language/sizeof +

+
-- cgit v1.2.3