beejjorgensen / bgc

Beej's Guide to C Programming source
Other
467 stars 88 forks source link

5.7: Missing "printf()" (and some parentheses)? #108

Closed acid-bong closed 1 year ago

acid-bong commented 2 years ago

The example program is written this way:

sizeof(int); // Returns size of an `int`
sizeof p     // p is type int*, so returns size of `int*`
sizeof *p    // *p is type int, so returns size of `int`

Shouldn't it use printf() instead, so that readers can observe themselves what is getting returned (especially those who copypaste and then troubleshoot)? And shouldn't sizeof() always accept arguments in parentheses?

printf("%zu\n", sizeof(int)); // Returns size of an `int`
printf("%zu\n", sizeof(p));   // p is type int*, so returns size of `int*`
printf("%zu\n", sizeof(*p));  // *p is type int, so returns size of `int`
beejjorgensen commented 1 year ago

So the parens are only necessary if you have a type in there, like int or float. If it's an expression, like x+12 or p then they're extraneous (just like they're extraneous in an expression like x = (y + z)).

In the existing code, I should be consistent with semicolons. And technically it "evaluates to", not "returns".

But should I printf()? Yeah, I think so. It'll be in the next release. Thanks!