Why sizeof is an operator and not function




















Create account Log in. Namespaces Page Discussion. Views View Edit History. From cppreference. Keywords Escape sequences. Namespace declaration. Namespace aliases. Fundamental types Enumeration types Function types. Compound types Union types.

Default initialization Value initialization Zero initialization Copy initialization Direct initialization. Expressions Value categories Order of evaluation. Operators Operator precedence. Class declaration Constructors this pointer. Access specifiers friend specifier. Class template Function template. Inline assembly. The example output corresponds to a system with bit pointers and bit int. Run this code. C documentation for sizeof.

Compiler support. Freestanding and hosted. Language support library. Similar Questions. Why sizeof to function returns 1 in C? How to find size of structure without using sizeof operator in C? C: Why redefining of variable is not an error? Email me at this address if a comment is added after mine: Email me if a comment is added after mine.

Email me at this address if my answer is selected or commented on: Email me if my answer is selected or commented on. As consequences: The operand of sizeof can be a parenthesised type, sizeof int , instead of an object expression. But they aren't part of the invocation of sizeof, they're part of the operand.

You can't take the address of sizeof. The expression which is the operand of sizeof can have any type except void, or function types. Indeed, that's kind of the point of sizeof. Improve this answer. Steve Jessop Steve Jessop k 34 34 gold badges silver badges bronze badges.

Wow, just what I was thinking! I believe things are more complex nowadays due to variable-length arrays VLA. In that context, int is nothing fancy - just a name of a type inside parentheses. Parentheses here are a part of the syntax of sizeof - they are required when taking the size of a type, but not required when taking the size of an expression. See e. The standard uses two notations for sizeof : sizeof unary-expression and sizeof type-name — so in the C11 standard it is not deemed to be a 'cast' but a parenthesized type name.

The net result is much the same. For comparison, a cast expression is type-name cast-expression. Show 1 more comment. John Kugelman John Kugelman k 65 65 gold badges silver badges bronze badges.

And the standard is probably correct because sizeof takes a type and In general, if either the domain or codomain or both of a function contains elements significantly more complex than real numbers, that function is referred to as an operator.

This probably explains the motivation of the C standard and other programming languages in using the terms "operator" and "function" as they do.

Because it's not a function. Because: when you pass a value to a function, the size of the object is not passed to the function, so a sizeof "function" would have no way of determining the size in C, functions can only accept one type of argument; sizeof needs to accept all sorts of differnet things variables as well as types! You can't pass a type to a function in C calling a function involves making a copy of the arguments and other unnecessary overhead.

Artelius Artelius Dewfy Dewfy Except for VLA - variable length array - arguments. Josh Darnell 11k 9 9 gold badges 36 36 silver badges 63 63 bronze badges. The standard already states clearly that sizeof is an operator — phuclv. Sign up or log in Sign up using Google.



0コメント

  • 1000 / 1000