News for hackers. Stuff that matters.
Re:Stop using strncpy already by primitur on Thursday April 04, 2013 @03:41AM (#5491485)
Pop quiz: is sizeof a function?

(Hint, no, its an operator. Like -- and &. Do you prefer this form: --(someint);

Re:Stop using strncpy already by Aloisius on Thursday April 04, 2013 @03:53AM (#5491514)
Regardless, 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.

Re:Stop using strncpy already by dibbit on Thursday April 04, 2013 @07:34AM (#5492035)
There 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 ..
Re:Stop using strncpy already by maximilianburke on Thursday April 04, 2013 @04:50AM (#5491635)
The languages state that sizeof requires parentheses when the expression is the name of a type -- sizeof int is an error, sizeof(int) is not.
Re:Stop using strncpy already by dchichkov on Thursday April 04, 2013 @04:44AM (#5491622)
Just 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.

Re:Stop using strncpy already by pitkali on Thursday April 04, 2013 @06:38AM (#5491870)
Pop quiz: what are possible results of running sizeof(char)? It's just another thing to note ;)
Re:Stop using strncpy already by jibsen on Thursday April 04, 2013 @05:02AM (#5491656)
I think perhaps confusion on the use of parenthesis comes from the fact that you need them for types and not for objects.
Re:Stop using strncpy already by pitkali on Thursday April 04, 2013 @06:37AM (#5491869)
I 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.
Re:Stop using strncpy already by Dylan16807 on Thursday April 04, 2013 @06:32AM (#5491855)
It's an operator but nobody should be forced to remember that it's an operator when it doesn't matter.
Re:Stop using strncpy already by dibbit on Thursday April 04, 2013 @07:35AM (#5492042)
It 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.
Re:Stop using strncpy already by Dylan16807 on Thursday April 04, 2013 @01:11PM (#5493764)
1. 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.