akalenuk / wordsandbuttons

A growing collection of interactive tutorials, demos, and quizzes about maths, algorithms, and programming.
https://wordsandbuttons.online
The Unlicense
486 stars 18 forks source link

"main()" should be "int main(void)" and other quibbles #12

Closed Keith-S-Thompson closed 5 years ago

Keith-S-Thompson commented 5 years ago

For all five examples in so_you_think_you_know_c.html, main() should be int main(void).

In C90 (which is 19 years old and has been replaced by two later editions), a function definition with empty parentheses is an obsolescent feature.

In C99 and later, the "implicit int" rule was dropped, and omitting an explicit return type from a function definition is invalid (I think it's a syntax error, or it might be a constraint violation).

On to other quibbles:

In the second example, the relative sizes of short int and int are not guaranteed, only that the range of int includes at least the range of short int. In the presence of padding bits, a conforming implementation could have sizeof (short int) > sizeof (int). The implementation-defined signedness of plain char is also relevant; it's possible to have sizeof (int) == 1 and CHAR_MAX > INT_MAX (if plain char is unsigned and CHAR_BIT is at least 16).

In the third example, the numeric value of ' ' is implementation-defined (and you used LEFT SINGLE QUOTATION MARK characters rather than apostrophes for the character constant).

In the fifth example, the operator precedence of the ++ operator is well defined. Only the order of evaluation is unspecified. The description could imply that the possible results are limited to the possible orders of evaluation, but in fact the behavior is completely undefined; in principle, evaluating i++ + ++i could make demons fly out of your nose. (Of course that can't happen, but if it did it would not violate the C standard.)

akalenuk commented 5 years ago

Yes, you're absolutely right. Thank you!

I've updated the page just now.

Sorry for being sloppy with the snippets. This was among the first pages for Words and Buttons, I just made a remake of an old Medium post to see what it's like to write everything by hand. I never expected it to get so much attention :-)