Page 1 of 1

Possible review of small C programs

Posted: Tue Apr 29, 2025 7:52 pm
by empathymonster
Hi there. I've visited osdev (dot) org quite a few times in the past year, mostly out of curiosity, as I'm not an experienced programmer, but never used the forums. In a nutshell, my question is if this is a good forum to get feedback on my C programs in general. I don't want to use reddit. As a quick background, I've taken Python and Java in college, so I'm not a completely inexperienced programmer, but I'm definitely still in the intermediate end of of learning C. From my college courses, I learned to first plan my programs before writing any code, and to thoroughly document my code. Whenever I use a stdlib function, I usually comment it with the manual page and the section in the GNU C Introduction and the GNU libc reference manual where I obtained the information. I have looked up the Linux coding standards for C formatting, and that's generally what I try to follow. If that's allowed, it's also something that I would not abuse by too frequent postings, as I completely understand people are busy and their feedback comes free. Or, perhaps, if someone might be willing to let me pm them, say, a program every two or three weeks that's ~200 loc or so. Mostly what I'm interested in is, for example, things like the rationale over using something like stpcpy() over strcpy(), if I'm handling errors properly, what's the rationale for using this library function or system call over that one, things of that nature. As a further note, I have a copy of Modern C by Dmitrovic, and I wouldn't say that I necessarily have it memorized, but I am thoroughly familiar with all of the material. Thanks so much.

Re: Possible review of small C programs

Posted: Tue Apr 29, 2025 9:13 pm
by nullplan
Some days I wonder if this is AI or if people really are like that nowadays.
empathymonster wrote: Tue Apr 29, 2025 7:52 pm Whenever I use a stdlib function, I usually comment it with the manual page and the section in the GNU C Introduction and the GNU libc reference manual where I obtained the information.
Talk about overkill. If you really did that, you wouldn't be able to see the source code beneath the mountains of comments.
empathymonster wrote: Tue Apr 29, 2025 7:52 pm Mostly what I'm interested in is, for example, things like the rationale over using something like stpcpy() over strcpy(),
stpcpy() is like strcpy() but with a sensible return value (pointer to the end of the destination string). Neither is terribly good. Rule of thumb: A function that takes a pointer without a length is probably not good. Yes, that means that strlen() is not a good function. strnlen() is better, but only in POSIX, not ISO-C. But you can build strnlen with memchr().

As for posting to the board: It is not like we are usually drowning in messages. We have a "General Programming" board for stuff like that. Just send stuff, maybe someone will bother with it.

Re: Possible review of small C programs

Posted: Tue Apr 29, 2025 9:23 pm
by empathymonster
I don't understand why commenting my code with the section in the glibc ref manual would make you think it's AI. It's mostly for my own reference.

Re: Possible review of small C programs

Posted: Tue Apr 29, 2025 9:35 pm
by empathymonster
I do appreciate the reply though. I'll try to keep any comments to a minimum then if I do post something.

Re: Possible review of small C programs

Posted: Wed Apr 30, 2025 1:45 am
by iansjack
There’s no problem, as far as I’m concerned, with posting whatever you like in the General Programming forum. The proof of the pudding is whether you get any answers or not. No-one is forced to answer, or read, posts so don’t worry about them being too busy; if that’s the case they just won’t respond. The only problem would be creating too much noise by posting lots of trivial questions. It doesn’t sound as if that would be the case.

Commenting - how much or how little - is a matter of personal preference, although in a group programming effort you would need to establish common rules. Most amateur programmers under comment, which generally comes back to bite you when you later look at the code. Use whatever level of commenting helps you most.

Re: Possible review of small C programs

Posted: Wed Apr 30, 2025 9:20 am
by nullplan
empathymonster wrote: Tue Apr 29, 2025 9:23 pm I don't understand why commenting my code with the section in the glibc ref manual would make you think it's AI.
Wasn't that. More the general lack of structure and the rambling evident in the OP. I open up the topic and am immediately greeted by a wall of text, that's rarely a good sign.
iansjack wrote: Wed Apr 30, 2025 1:45 am Most amateur programmers under comment, which generally comes back to bite you when you later look at the code.
True, but hiding the actual implementation beneath pages and pages of comments is probably also not good. I do like the comments in the musl source code, which explain the reasons rather than the implementation details, e.g.

Code: Select all

	/* The main program must be relocated LAST since it may contain
	 * copy relocations which depend on libraries' relocations. */
	reloc_all(app.next);
	reloc_all(&app);
Thanks, now I know why these things are there in that order.

Re: Possible review of small C programs

Posted: Wed Apr 30, 2025 9:39 am
by iansjack
I take it that you are not a fan of Knuth’s literate programming. He’s one of my coding heroes.

Any decent programming editor should let you collapse comments (functions, blocks, etc.) so that you can see the wood for the trees.

Re: Possible review of small C programs

Posted: Wed Apr 30, 2025 11:00 am
by empathymonster
Thank you for the replies. I was trying to be as brief, but also as detailed as possible with my original post. I didn't want to come off as someone who has absolutely no idea about programming, bc I know more experienced people tend to frequent this forum. When I said I comment my code with the manual page, I didn't mean I literally paste the entire manual page into a comment. That'd be silly. I just make a note of which page I got the info from so that I can pull it up if I need to when I come back to that section code. Same thing with a glibc reference. It's just something simple like 11.2.2 file name resolution, page 268. And if for whatever reason there's what I feel is a particularly important bit of text, I'll put that in there, but it's usually no more than a sentence or two, similar to the musl example you gave. Anyways though, we don't have to continue on with that point. I read this post before posting: viewtopic.php?t=16944 , which is the forum rules - required reading, and even the link it suggests, How To Ask Questions the Smart Way, by Eric Raymond. I went through the forum FAQ and read what I thought were immediately relevant topics for me. In short, I'm trying to make an effort and wanted to try to reflect that in my post.

Re: Possible review of small C programs

Posted: Mon May 05, 2025 1:09 pm
by eekee
empathymonster wrote: Wed Apr 30, 2025 11:00 am When I said I comment my code with the manual page, I didn't mean I literally paste the entire manual page into a comment. That'd be silly.
I didn't think you did. Well actually, I did for half a second, then I realised. :mrgreen: I like it when an editor recognizes man page links and just lets me click them. Some will even make links from filename and line number or even search string. The Acme text editor will plumb any text you right-click, sending it to a plumber program which is scripted to rewrite the plumbed string and launch programs or send messages. The 9front OS adds to all that a viewer which can open PDFs at a specific page. :)

Thanks for reading all that on how to ask questions well, it helps a lot when people do. :) There's just one thing, if you could break your text into paragraphs, it's easier to read and less intimidating. ;) I don't think there are really good rules on how to make paragraphs. I just try to break when the thought changes enough, but if a paragraph feels too long, I look harder for places to break it. (And some days I have to try not to try too hard. ;) ) Sometimes I rewrite a bit to make a better break point, but not often.

Would you like this thread moved to General Programming?