|
I'm working through learning C with the Dennis* Ritchie & Brian Kernighan book. Spent more time fiddling with the environment than programming the last few days however... I am on excersize 1.9. Been able to figure them out so far. I found a site with all the exercises as the books, in the same order. So I started going there afterwards to check how my solutions fare against the ones there. This is how I approached the excersize of removing all double or longer blanks, and it works when I test it on a text file I got set up. The site solution seems to be doing what I came up with though I think my syntax is more compact. https://www.learntosolveit.com/cprogramming/ex_1.9_sinblank I note they set up an initial value for the previous char as well as used a symbolic constant. I didn't do any of that since I didn't see the point. But is this a practice I should consider when doing C? Just trying to get good habits in from the start. *I share the same name as the creator of C, that bodes well His Divine Shadow fucked around with this message at 18:44 on Jan 1, 2023 |
# ¿ Jan 1, 2023 18:42 |
|
|
# ¿ May 17, 2024 14:23 |
|
Thanks that makes sense, learned the importance of initializing variables.
|
# ¿ Jan 1, 2023 19:12 |
|
The book used ints in the examples and said this is because in a single character inside '' quotes is actually a small integer so it works with ints Apparently you can use char, but chars don't like negative values, which might be returned by some non-ascii characters. So not a problem with ints, but apparently you can use a signed char to make it allow negative values. Or use ints? My understanding might be flawed.
|
# ¿ Jan 1, 2023 19:49 |
|
csammis posted:Learning C from K&R in the year 2023 seems really counterproductive unless your goal is explicitly to learn how C was written forty years ago. The modern C standard has more expressive power and safety than K&R had at the time. I like this way personally. It's more interesting I find. And I asked around the net before starting, asking about books and so, this was still recommended by loads of people to me. This is the 2nd edition which uses ANSI C which is very close to modern C that it should not be an issue.
|
# ¿ Jan 1, 2023 20:00 |
|
Is it bad form in c to write one over the other. From what I understand they should be equivalent since an array in c is just a pointer in disguise.code:
|
# ¿ Jan 9, 2023 12:12 |
|
Ihmemies posted:This course would probably be easier, if it existed in some real form. Now it is a self-study course consisting of ~80 exercises, a 155-page PDF and an exam. No lectures, no assistance or anything really. Just a bunch of old slides from year X and a bunch of excericses you have to do. I thought the bitwise stuff look neat and interesting from what little I've seen of them so far. This guy explained the concept well for how you'd use bit fields and it sounds very useful to me. https://www.youtube.com/watch?v=aMAM5vL7wTs I went and studied hex to binary tables a little after this, made it easier to follow the video. His Divine Shadow fucked around with this message at 13:48 on Feb 6, 2023 |
# ¿ Feb 6, 2023 13:45 |
|
I don't have a question but I just want to say the bitwise exercise in the C book I got is hard and makes me feel stupid. I had to go look at the solution and looked at alternative solutions and feel like I am barely hanging on. Hopefully I'll get there.
|
# ¿ Mar 14, 2023 10:23 |
|
I have a function that writes it out on screen for me actually. Lifesaver. Though there might be some merit to writing on paper.
|
# ¿ Mar 14, 2023 14:55 |
|
Well here's how I attempted to do exercise 2-7 in my C book. I have not looked at the answer yet but I feel pretty sure it's radically different, more compact. I do believe this code is pretty inefficient and can be optimized a lot and most of the x* variables can be removed but I wrote out it out like this to make it simpler to see and understand what's happening. The exercise is that I want to flip or invert any bits, starting at position p as well as n bits to the left of p. Leaving the rest unchanged. My idea was to make two bit masks to mask of the area that is to be flipped, combine that into a new mask and then use said mask with a XOR operator on the original. I've tested it on a couple of numbers and it seems to work. I noticed I have to use p-1 to start from bit 4 and not 5, which OK I can see that might work, it counts from 1 not 0 I guess. But then (p-1)+n doesn't yield the correct offset. There I have to remove the -1 . I'm still not sure why and I don't like it.
|
# ¿ Mar 15, 2023 06:24 |
|
leper khan posted:I would do something like Is there an advantage to doing it that way? code:
|
# ¿ Mar 15, 2023 19:16 |
|
leper khan posted:Fewer arithmetics. Readability. Real answer requires perf testing I'm too lazy to do right now for you Satisfied with that reasoning, to my eyes I understood my own code more easily at first but it was fresh in my head. FWIW they don't produce the same results, I'm picking over it now as an another exercise.
|
# ¿ Mar 16, 2023 05:35 |
|
rjmccall posted:I mean, your implementation is flipping N+1 bits and Leper’s is flipping N, that’s the obvious difference. I did some + and - on the variables and now they both come out identical with the same data fed to them, so I guess what you said. All this still feels obscure and hard to wrap my head around, even the easy to read function. Oh well onto exercise 2.8, wtf does it mean to rotate a bit? That was a rhetorical question. I already googled it. This actually looks easier though, I think I know how to do this... We'll see.
|
# ¿ Mar 17, 2023 11:52 |
|
Eh, you can go older, something I did for fun
|
# ¿ Mar 22, 2023 12:17 |
|
I was gonna ask a question but I found the answer myself, it was why was this function I had simply copied and used earlier, was using malloc when it hardcoded the length anyway. But as I found here:quote:https://www.programmingsimplified.com/c/source-code/c-program-convert-decimal-to-binary I do have a follow up however. So I was using this function directly in a printf statement to format an integer, I never assigned it to a variable. By doing that I assume I have created a memory leak. So the right way to handle this is to assign the return value to a variable that I can free later right? I guess it's not possible for a function that calls malloc to clean up after itself either or it would erase the pointer it was supposed to return.
|
# ¿ Mar 28, 2023 09:12 |
|
You mean like a function that takes a length and int, like say something like thiscode:
|
# ¿ Mar 28, 2023 10:01 |
|
Oh yeah, gotcha.
|
# ¿ Mar 28, 2023 10:42 |
|
C++ code looks so loving incomprehensible to me compared to regular C.
|
# ¿ Apr 5, 2023 12:50 |
|
|
# ¿ May 17, 2024 14:23 |
|
Reading a few pages back about CS and universities and I realize I will never ever be able to do any of that, it all sounds dreadfully boring. I'm also too old. I only do some programming stuff because it's interesting, but the higher education scene sounds like aliens talking about concepts beyond my feeble brain.
His Divine Shadow fucked around with this message at 08:15 on Mar 15, 2024 |
# ¿ Mar 15, 2024 08:13 |