4 ms·
A HTML copy: https://crms.tiiny.site/ https://crms.tiiny.site/
by dvfjsdhgfv 4y ago
A HTML copy:
https://crms.tiiny.site/ https://crms.tiiny.site/
- raverbashing 4y agoSo, the C style they use is: function types on their own line and spaces between function names and their parameters? That's not a style I would be encouraging to use today It also uses sizeof to calculate the size of an array which would be good in any language except C
- ctrlmeta 4y ago> It also uses sizeof to calculate the size of an array which would be good in any language except C It is a pretty standard and popular way of finding the size on an array. The only caveat is that the programmer really has to be sure that it really is an array and not a pointer to an array.
- eMSF 4y agoOn the contrary: the sizeof operator is the only way to generally get the size of an arbitrary array (with e.g. no known sentinel to mark its last element) in C. You couldn't use a function instead even if you wanted to. Many other languages however don't have the equivalent of the sizeof operator because they don't expose an object's memory representation quite as much as C does.
- raverbashing 4y agoYou're right, but it only works for fixed-sized arrays (which are not as common). Also for a learner it is very easy to think that I can do: int * my_array; my_array = malloc(100*sizeof(int)); int array_length = sizeof(my_array);
- enriquto 4y ago> it only works for fixed-sized arrays It also works for variable length arrays. void f(int n) { float a[n]; int m = sizeof a / sizeof *a; assert(n == m); }
- kevin_thibedeau 4y agoIn this case my_array isn't an array. Misleading learners that arbitrary blocks of memory are "arrays" is the bigger travesty.
- classified 4y ago> That's not a style I would be encouraging to use today Shame on them for not promoting your personal taste in coding style.
- Kwpolska 4y agoI agree, the code style is quite ugly and strange. They also seem to prefer not using braces for `if`, which is really error-prone. And the first example is a recursive Fibonacci function… Recursion is often hard to grasp for beginners, and while this isn’t a beginner-programmer’s book, couldn’t they have started with something easier?
- camel-cdr 4y ago> So, the C style they use is: function types on their own line and spaces between function names and their parameters? Having function return types on an extra line is actually quite useful, because you can easily grep for the function implementation with `grep ^foo -r .` This works quite well, even on huge code bases.
- drewg123 4y agoNot sure why you're being downvoted.. I came here to say exactly that. FreeBSD maintains this style, so you can just 'git grep ^function' to find a function's definition. When working with other codebases which don't follow this convention, its somewhat frustrating to find a function's definition with just grep.
- numeromancer 4y agoIt also scales with increased qualifiers: static inline const unsigned int * function()
- raverbashing 4y agoYes, I know. (I'm more bothered by the space in calls rather than this, though this is not my favourite way) Or: You can use ctags (like the Linux kernel) or just grep for something like (egrep) '^[a-z]+ .+foo\(' if you're really bothered by grepping function calls instead of only definitions