4 ms·
Pop quiz: is sizeof a function? (Hint, no, its an operator. Like -- and &. Do you prefer this form: --(someint);
by primitur 14y ago
Pop quiz: is sizeof a function?
(Hint, no, its an operator. Like -- and &. Do you prefer this form:
--(someint);
- Aloisius 14y agoRegardless, it still looks better with parenthesis and removing them serves no real benefit other than proving that I know it is an unary operator. Feel free to grep through your /usr/include for sizeof and see which way is more common.
- dibbit 14y agoThere actually is a good reason not to use parens with sizeof, and it is in fact a means of introducing subtle bugs to your code if you don't know the difference: sizeof some_struct; //computed at *compile* time sizeof(some_struct); // also computed at *compile* time, but looks like its a runtime call Anything that looks like something but isn't actually that thing, in my opinion - especially with a language like C - is room for programmer enlightement ..
- dchichkov 14y agoJust FYI - code: sizeof buffer - sizeof(buffer[0]) is looking unconventional. Couple of reasons... You generally want to avoid +1 / -1 in your code. You don't want to write sizeof() without parenthesis, - it reduces readability.
- pitkali 14y agoPop quiz: what are possible results of running sizeof(char)? It's just another thing to note ;)
- maximilianburke 14y agoThe languages state that sizeof requires parentheses when the expression is the name of a type -- sizeof int is an error, sizeof(int) is not.
- jibsen 14y agoI think perhaps confusion on the use of parenthesis comes from the fact that you need them for types and not for objects.
- pitkali 14y agoI like the consistency of using parenthesis always. Just like some people insist of putting them in logical statements even if they do not affect the order of evaluation in that particular case.
- Dylan16807 14y agoIt's an operator but nobody should be forced to remember that it's an operator when it doesn't matter.
- dibbit 14y agoIt does matter. sizeof is computed at compile time, it is not computed at runtime. something() is a runtime invocation .. sizeof() 'looks' like that, but isn't.
- Dylan16807 14y ago1. A lot of math gets computed at compile time. 2. sizeof is not necessarily done at compile time. C99 allows variable-sized automatic arrays, forcing it to store and later look up the value at runtime if you use sizeof.