
As I’m somewhat restricted at the moment, bashing away on a 7 year old Toshiba laptop running Ubuntu, I thought I’d look around for some C games to help keep the blog going. There’s no shortage of games in C, even those that use SDL2 but what I’ve found is they can be a real pita to compile.
I guess most are for Linux and SDL but I’ve found things like the include path for SDL header files is one pain point. VS Code has a Replace in Files menu item so it’s not too difficult to change paths in multiple files.
Another difference I found was putting inline functions in the middle of a function. I didn’t know you could nest functions that way. A bit of digging found that good old gcc allows this as an extension but it’s definitely not normal C and clang doesn’t allow it which is what I tend to use.
My C standards
So my modest standards for writing C code are, to make life a bit simpler for porting…
- Use conditional compilation so you can compile on Windows and Linux. This includes paths to SDL, string print to buffers, MSVC _s functions (on Windows). Please compile on both platforms; it’s real easy to break it!
- I’ll eventually use this so that the timer routines I use will be one set of files that can compile on MSVC/clang.
- No nested functions. Yes I know gcc can do it, but its just unnatural. Use recursion if you must or function pointers but not nesting. If you really want to nest functions, program in Delphi or C#.
Other things I’d like are mostly taste, like putting a comment for every function.
Things that don’t really matter are whether you put function definitions before main() or after. If you put them after then you have to include a definition.
Likewise I don’t care if you use #pragma once or the older compile guards.
It’s probably a good idea to not call any of the GitHub banned functions. Remember who owns GitHub! (the same people who added the _s functions in Visual C++/MSVC…)