115

Why is sizeof considered an operator and not a function?

What property is necessary to qualify as an operator?

Jonathan Leffler
760k145 gold badges962 silver badges1.3k bronze badges
asked Sep 8, 2009 at 11:56

11 Answers 11

211

Because the C standard says so, and it gets the only vote.

As consequences:

  • The operand of sizeof can be a parenthesised type, sizeof (int), instead of an object expression.
  • The parentheses are unnecessary: int a; printf("%d\n", sizeof a); is perfectly fine. They're often seen, firstly because they're needed as part of a type cast expression, and secondly because sizeof has very high precedence, so sizeof a + b isn't the same as sizeof (a+b). 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 is not evaluated at runtime (sizeof a++ does not modify a).
  • 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.

A function would differ on all those points. There are probably other differences between a function and a unary operator, but I think that's enough to show why sizeof could not be a function even if there was a reason to want it to be.

answered Sep 8, 2009 at 11:58
Sign up to request clarification or add additional context in comments.

6 Comments

Wow, just what I was thinking!
I believe things are more complex nowadays due to variable-length arrays (VLA). IIRC, the standard would even allow sizeof to have side effects if there is a VLA in the expression.
@glglgl No, that doesn't make any sense. 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.g. here
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.) And I hate the way that comment Markdown works differently from Q&A Markdown!
printf("%d\n", sizeof a) is not perfectly fine. %d is the wrong specifier. It has undefined behavior.
|
29

It can be used as a compile-time constant, which is only possible if it's an operator rather than a function. For instance:

union foo {
 int i;
 char c[sizeof(int)];
};

Syntactically if it weren't an operator then it would have to be a preprocessor macro since functions can't take types as arguments. That would be a difficult macro to implement since sizeof can take both types and variables as an argument.

answered Sep 8, 2009 at 12:02

1 Comment

+1 but note that it is not a compile-time constant when the argument is a VLA - variable length array.
7

Because the C standard says so, and it gets the only vote.

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. Conversely, if neither the domain nor the codomain of a function contain elements more complicated than real numbers, that function is likely to be referred to simply as a function. Trigonometric functions such as cosine are examples of the latter case.

Additionally, when functions are used so often that they have evolved faster or easier notations than the generic F(x,y,z,...) form, the resulting special forms are also called operators. Examples include infix operators such as addition "+" and division "/", and postfix operators such as factorial "!". This usage is unrelated to the complexity of the entities involved.

(Wikipedia)

answered Sep 8, 2009 at 12:05

1 Comment

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

Because it's not a function. You can use it like that:

int a;
printf("%d\n", sizeof a);

Function does have entry point, code, etc. Function is to be run at runtime (or inlined), sizeof has to be determined at compile-time.

answered Sep 8, 2009 at 12:00

Comments

3

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
answered Sep 8, 2009 at 12:02

Comments

2

Because it is a compile-time operator that, in order to calculate the size of an object, requires type information that is only available at compile-time. This doesn't hold for C++.

answered Sep 8, 2009 at 12:08

Comments

1

There is small diference from function - value of sizeof is resolved on compile time, but not at runtime!

answered Sep 8, 2009 at 12:02

1 Comment

Except for VLA - variable length array - arguments.
1

sizeof operator is compile time entity not runtime and don't need parenthesis like a function. When code is compiled then it replace the value with the size of that variable at compile time but in function after function gets execute then we will know the returning value.

answered Oct 27, 2010 at 12:11

Comments

0

For better understanding, consider this C code,

#include<stdio.h>
int hello(){
 printf("hello");
 return 0;
}
int main(){
 int a = 1;
 hello();
 printf("%d", sizeof(a));
 return 0;
}

Which converts to the following Assembly code (gcc 15.2),

.LC0:
 .string "hello"
hello():
 push rbp
 mov rbp, rsp
 mov edi, OFFSET FLAT:.LC0
 mov eax, 0
 call printf
 mov eax, 0
 pop rbp
 ret
.LC1:
 .string "%d"
main:
 // initialize main
 push rbp
 mov rbp, rsp
 sub rsp, 16
 // int a = 1; 
 mov DWORD PTR [rbp-4], 1
 // hello();
 call hello() // Function call for hello, jumps to hello() above.
 // print size of a
 mov esi, 4 // size of a is 4 bytes here, no sizeof() function here
 mov edi, OFFSET FLAT:.LC1
 mov eax, 0
 call printf
 mov eax, 0
 leave
 ret

It can be clearly seen that there is no function call for sizeof(). The size is evaluated during the compilation by compiler, not during runtime.

An example for a function call is hello(), which uses the call instruction.

answered Nov 3, 2025 at 5:00

1 Comment

godbolt.org. This is the website used for converting above c program to assembly code.
-1

sizeof() operator is a compile time would be occurrence. It can be used to determine the parameters or arguments.

Josh Darnell
11.5k9 gold badges40 silver badges66 bronze badges
answered Aug 6, 2012 at 10:18

Comments

-4

Sizeof(), I think obviously it's both a function and an operator. Why? Because a function holds parentheses for entry at the stage of entry. But mainly also an operator cause operators are action character, therefore sizeof is an action statement that acts on the operand in the parentheses.

1 Comment

The standard already states clearly that sizeof is an operator

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.