lainchan / λ
Suggest a C Textbook
dictionary · 2024-09-06 06:46
What is your favorite C textbook? Many swear by K&R’s C, others say it’s outdated and to get King's C Programming a Modern Approach. What do you guys think?
Backus · 2024-09-06 08:44
I wish there was a version of K&R with modern code or some kind of guide that explains why its different from modern C and gives you modernized versions of the examples and exercises.
Elliot Alderson · 2024-09-06 13:10
I'm reading C Programming a Modern Approach and it's a
really good book, I've never got deep on any programming
language, but even then I'm not having trouble to keep up with
the content, it has a lot of exercises and projects in it and that
helps a lot to get familiar with the concepts of the language.
Currently I'm starting chapter 11, here is an example of my code:
https://www.codebin.cc/code/cm0r5giws0001jn02y6o9wxf2:J34MAy2ea2ncpxbi4KFLKU7vi7yfb3ry3zGtT1MEYWRM
setq · 2024-09-06 23:55
Read 'em both. Attached is the text file I used to learn C back in the dark ages, before "The C Programming Language" came out (just for fun).
drop · 2024-09-07 06:40
>>39965
Start with King's C Programming a Modern Approach, 2nd edition. It's the best book on C. Then read K&R for the code examples and exercises. After that, the C books that you should read are 21st Century C (published by O’Reilly) and after it, read either APUE or TLPI. I recommend that you save this post into a .txt file because it contains info that's not relevant right away.
Beej has also some handy guides: https://www.beej.us/guide/
The C programming language wiki has some useful pointers as well: https://www.iso-9899.info/wiki/Main_Page
Be sure to learn to write makefiles yourself but use meson for your real projects (I recommend that you avoid cmake or autotools). Use splint or Clang's scan-build to lint your code. Always use -Wall and -Wextra compiler flags. In addition, You should use -pedantic while you are learning C but disable it for your real projects (I wouldn't use it while learning C++ either!). Finally, use either gcc or clang as your compiler. If you are on Windows, you should consider dual-booting. If you can't do that for some reason, then you can use Clang. Alternatively you can use also mingw.
I also recommend that you get into habit of reading man-pages. Use your web browser or the M-x man command in GNU Emacs. This way you get links that you can open.
You can read man-pages at https://manpages.debian.org/unstable/manpages/intro.3.en.html
But note that your distro probably hosts their own interface to manual pages on their homepage.
You also want to learn how to use valgrind or address sanitizers at some point (after you learn how to use malloc() to allocate memory). Another thing that you want to learn is how to debug using gdb (https://www.youtube.com/watch?v=Miw0XLzHCws&list=PL0qfF8MrJ-jzs-PS4ychdY2fw9T-jpKtt&index=3).
Cat Mother · 2024-09-07 08:24
>>40008
>Make
It's a very handful and universal tool not only in the context of programming in C language, it doesn't matter whether you're going to use cmake or not.
>-Wall -Werror
Instrumentation for C is a very complex part of using C because you need to fuzz your stuff and you must do it in clever way.
Book, I would suggest Robert Seacord Effective C for entry level programmers and Linden (with fish).
DEBUG-CONTENTS · 2024-09-07 11:51
>>39965
K+R for basics, Peter Van Der Linden's "Expert C Programming: Deep C Secrets" for deeper stuff.
quote · 2024-09-09 12:53
Any recommendation for an intermediate C book? Something like the Scott Meyers Effective C++, but for C's patterns and pitfalls. >>40008 Does 21st Century C cover this sort of thing or is it more tooling oriented?
Nguoi La · 2024-09-09 18:14
I highly recommend Modern C by Jens Gustedt. Perfect for people who are completely new to the language (or even completely new to structured programming). The only thing I dislike is some of the coding style the author uses, but that's small beans.
progn · 2024-09-12 03:15
There was a book about UNIX programming that was published in the '80s or early '90s and had few new editions since. It was going into details of POSIX programming and explaining various UNIX programming conventions like NULL terminated arrays or "do{}while(0)" macros, etc. It's title was "Advanced Programming In The Unix Environment". It's worth reading even if you are going to do gamedev in C and not system programming.
Unknown · 2024-09-12 10:41
I wonder, are there any decent game programming books that use C for code examples? I know C++ is the lingua franca in the industry but C's simplicity is attractive.
Agent Smith · 2024-09-12 15:14
>>40037
Back in the '90s/very early '00s there been some "gamedev books". I have seen one about gamedev in Pascal. As for C - they most likely would be mostly about C + MS DOS + 386 assembly or C + some library like Allegro for graphics. I would not expect them going in too deep details other than some DOS input/output + DMA stuff. At least I have never seen or heard about any "C gamedev Bible". If there been any it was very obscure.
For sure there been books on computer graphics in C. Both 2D "demoscene" effects and 3D.
sizeof · 2024-09-16 01:15
>>40037
>C' simplicity
You're wrong, there's no simplicity in C.
Donna Hawthorne · 2024-09-16 21:07
>>40048
>You're wrong, there's no simplicity in C.
t. Someone who's never used C++
Rick Deckard · 2024-09-17 18:47
>>40048
>>40049
On a scale from one to ten, with one being Forth and ten being C++, C's about a three, above Smalltalk, below Go. It's definitely on the simpler side of things, but nowhere near the simplest.
Davis · 2024-09-17 19:08
>>40048
There's great simplicity in C compared to almost everything else mainstream. In exchange of C being simple, all the rest of the domain's complexity must now reside in your brain and your knowledge and your bespoke code. To use a simpler tool you need to know a lot more; with a complex enough tool you just have to push a button and it's done. C is simple but hard, other languages may be complex but easy.
typedef · 2024-09-18 01:35
>>40055
Dude
>Understanding each pitfall in language standard
>Behavior that follows your compiler implementation
>Compilers flag to set to much safer language subset
>Setting up fuzzing pipeline
How can this be anywhere near "great simplicity"?
Pris · 2024-09-18 01:50
>>40055
libraries
Any language provides the tradeoff of learning more api for readymade solutions. I think the real tradeoff with C is safety / limitations.
C allows raw access which enables to code more elegant without workarounds but its also a potential source of error which is why its vilified by academia&corporate who aim to lower the price of coders by forcing safety standards into languages so you can hire lower quality people.
() · 2024-09-18 07:29
>>40060
>Understanding each pitfall in language standard
This applies to every language, even those for which the pitfalls aren't widely understood yet.
And C does have considerably less than any others, just because it gives you less tools to shoot yourself in the foot with. When people talk about C's "simplicity" they're just referring to that: the relatively small, tight set of operators and features to fuarrrk around with. Is this a good thing? It does make code more readable even for absolute beginners.
pop · 2024-09-18 17:34
>>40055
>In exchange of C being simple, all the rest of the domain's complexity must now reside in your brain and your knowledge and your bespoke code.
That's not a result of C being simple, it's a result of C being lightweight; a subtle, but significant difference. Scheme is much simpler than C, yet you don't have to worry about every single memory allocation in it.
Dozer · 2024-09-19 00:54
>>40066
>This applies to every language, even those for which the pitfalls aren't widely understood yet.
Then Haskel and Rust are x9000 times simpler that C.
Spike Spiegel · 2024-10-29 19:14
>>40037
I'm looking for something like this, too. All pattern books and engine books I found use C++.
progn · 2024-12-11 12:23
>>40066
Wait until you hear of the C abstract machine.
Pris · 2025-02-12 06:09
I'm looking for a textbook or document that teaches how one would go about making a game engine in C, or at least the necessary design patterns to do so. Most game engine or design patterns book use C++ or another OOP language.
Batou · 2025-02-12 12:05
>>41153
You can apply c++ game book to C, the only difference is how much effort implementation will be. Ultimately a basic engine will always be 3 things: input handling, rendering & audio output, game loop updating whats going on. The difference in language is primarily a matter of what libraries can you use to save time.
Btw you can write C style code in C++ and use C++ libraries. You can also link C and C++. There is no need to limit yourself to C if you just want to write some logic in C.
Jens Hansen · 2025-02-12 14:15
>>41153
https://www.parallelrealities.co.uk/tutorials/
Not a textbook, but this guy's tutorials are rather well done
Church · 2025-02-12 15:34
>>41155
You can find another SDL2 tutorial at https://lazyfoo.net/tutorials/SDL/
The Phantom Phreak · 2025-02-12 16:04
>>41154
The thing is, I don't want to use objects for my game engine, I want to do it in a non-OOP way, but I don't know how to. I know how to render graphics and input with SDL, but I can't seem to tie everything together in an organized manner.
BloodCat · 2025-02-13 08:19
>>41157
>I don't want to use objects for my game engine, I want to do it in a non-OOP
That's what i meant with C style code.
Difference between C++ and C style ? In C++ you define a class with functions and instantiate from that, then call with
for (obj in vector) obj.func()
In C you define a struct and define functions externally, then you use a visitor pattern and loop with
for(int i=0;i <arr_length;i++) func(struct_array[i]).
>tie everything together in an organized manner.
Basic outline how i wrote a 2d game omitting finer details:
split render elements & logic into separate data structures
1. struct array for objects to render {position,size, texture, uv} (uv + animation frames in one texture = only updating uv can animate sprite)
2. linked list of function pointer+args { *func , *args} - this makes dynamic changes trivial as you just throw another function call into the list, functions are also allowed to create functions for simple things like delayed spawn with a function that takes a timestamp and creates a new function removing itself from the list when the time has arrived
3. linked list "scene graph" holding 1. and 2. in each node representing a scene + keybind array akin to 2. , swapping a scene is just replacing the active node and rendering an UI is just activating a UI node on top of the regular scene which comes with its own keybinds array so it can eat inputs meant for UI and pass unused inputs to regular scene if desired
So in conclusion:
Game starts, loads 3. which includes 1. and 2.
Start game loop:
--query input state changes
--loop for 3:
----loop for 2. updating game state
----loop for 1. updating rendering data
--render result
Took me about a month writing my own first 2d engine like this as a side project. (meaning i couldn't dedicate all my time to it)
Molly Millions · 2025-02-13 09:27
thanks a lot guys, this is really helpful