Handmade Hero»Forums»Code
Ginger Bill
222 posts / 2 projects
I am ginger thus have no soul.
C Programming Tutorials
I know Casey wanted to do a more in-depth C programming introduction for anyone who hasn't even done C before. However, he has his hands on handmade hero already so he cannot do it.

I was thinking I might do a "Intro to C Programming (on Windows)" thing. I am not sure if it should be a written series or a video series. I don't want to do both but either would be fine.

Another problem with doing something like this is that getting start with C on windows is difficult. I would have teach everything from scratch.

I will have to teach numerous things before I even started writing a line of C and along side too:

  • Text Editor (I will probably use Sublime as it is what I use and the learning curve is pretty low compared Emacs/Vi(m))
  • Command Line
  • What a parser/compiler/linker/etc is
  • Installing a compiler (MSVC most likely)
  • Using a debugger for visualizing the data (actual bytes in memory) and actually debugging (probably not a while though)
  • How computers actually work
  • Briefly skip over assembly and explain the importance of C

I was also thinking of making little projects for the new skills that they have learnt such as basic games and (useful) tools. Kind of like homework but not exactly.

Does anyone have any advice on this idea of mine and would it be a useful thing? Are there any tutorials that already go into this much about low(ish)-programming and computers?
Connor
52 posts
It's been so long ;)
C Programming Tutorials
Edited by Connor on
I think a full fledged handmade-C tutorial series would be awesome. Something that we could direct people wanting to get into handmade hero but they didn't know C. It could be like a precursor to the actual handmade hero show.

You could split up the series - the setup (teaching command line, text editor, etc) then the actual series (assembly and importance of C, installing a compiler, how a compiler/parser/linker is, debugger, etc)

That way someone who already has a text editor / knows how to use a command line can go ahead and just learn the language.

I don't know if this is really possible, but I would love a series that is very slow. I mean like the same amount of time it would take you to complete a college course. So the time frame of 1-4 years. Something that goes and explains EVERYTHING, and takes the right amount time to explain each topic.

That kind of thing is unlikely to happen. The amount of time it would take the creator would just be too much.

~ Connor
Ginger Bill
222 posts / 2 projects
I am ginger thus have no soul.
C Programming Tutorials
For C, which version should I teach: C90, C99 or C11? C11 is not supported by all compilers (and doesn't offer many new features); C99 is only just supported by MSVC (however, I still cannot get that MSVC 14 to install properly but I will figure it out) (you can get most of the features with #defines and compiler flags); Ansi-C/C89/C90 is very old but is supported virtually everywhere!

I want to do the entire series on Windows as that is what the majority of people have. However, what compiler should I use? MSVC or MinGW?

This is going to take a while to plan and I will have to make sure it is really a decent series.
Mārtiņš Možeiko
2233 posts / 1 project
C Programming Tutorials
Edited by Mārtiņš Možeiko on
I would teach subset of C99. It has some pretty nice stuff - like ability to declare variables at any place, designated initializers, compound literals, one line comments, inline and restrict keywords.

MSVC 2015 actually doesn't support C99 fully, it's missing few features (VLA).
I would recommend sticking with MSVC if target platform is Windows. Because of debugger. Once clang will be fully integrated in IDE by Microsoft (including the debugger), then I would switch to it.
Ginger Bill
222 posts / 2 projects
I am ginger thus have no soul.
C Programming Tutorials
Do you know where I can find a list of what features are and are not in MSVC for C99?
Mārtiņš Možeiko
2233 posts / 1 project
C Programming Tutorials
I'm not aware of any reasonable list. As far as I know VS2015 is missing only VLA and tgmath.h header from C99. Rest of features in this list are supported: https://en.wikipedia.org/wiki/C99#Design
Ginger Bill
222 posts / 2 projects
I am ginger thus have no soul.
C Programming Tutorials
That's really good news!

VLAs can be a real problem on embedded systems. They can be easily substituted by malloc or a custom allocator. There is alloca but this is what VLAs use internally if I remember correctly so it doesn't solve the problem.

As for tgmath.h, I have never used it personally so that is not a problem either.
Mārtiņš Možeiko
2233 posts / 1 project
C Programming Tutorials
Edited by Mārtiņš Možeiko on
alloca is a bit worse than VLA in my opinion. alloca releases allocated memory only at end of function. So if you put it inside some loop, then you're gonna have a bad time. VLA afaik releases memory at end of each scope.

But both of these are implemented by compile internally, not via each other.

Substituting both of them with malloc is not trivial. First of all the performance suffers. VLA and alloca is very fast - pretty much just one subtraction instruction (multiple such allocations can be even joined together to one subtraction). malloc on other hand can contain a lot of code to find free space. Another disadvantage is that malloc requires explicit free. If you forget that, that's a memory leak. VLA or alloca automatically releases memory, so code get's a bit cleaner. Sometimes malloc takes a mutex to be a thread safe, so it get's pretty expensive.

Of course if you have custom memory allocation, like HandmadeHero does, then this get's a bit less of a problem.

Oh, and I don't see how VLA can be a problem on any system. If you need memory you allocate it somewhere. On stack, on heap, or statically in .bss globally. In any case you have a problem if you allocate too much. If you know how much memory you have available and how much exactly you are allocating, then there is no problem.
Ginger Bill
222 posts / 2 projects
I am ginger thus have no soul.
C Programming Tutorials
Thank you for clearing up my misunderstanding of VLA and alloca. I have hardly ever used them so my knowledge of them is poor. If I am to teach of them in the series, I will need to research them like mad and with anything I am not sure of how it works exactly. alloca seems a little broken as it deallocates at the end of functions scope; who thought that would be a good and consistent idea?!

When I mean using "malloc or a custom allocator", what I was implying is if you need an array on the stack, and you do not know the size of that array at compile time, are you sure you want it on the stack and allocated at runtime? The stack is usually small compared to that of the total memory. On embedded systems, the stack can be extremely small so using VLAs and alloca may cause a stack overflow which neither warn nor prevent against. Many allocators if they run out of space, usually return a null pointer or return a flag/error of some sort.

I usually use arenas, pools, and other types of allocators mainly (similar to handmade hero). I don't use malloc in many cases as it can be very slow and a custom implementation of a heap allocator can/will be better. I only use malloc if want to make a test tool/program and not that bothered by its implementation.

Use the right tool for the job. If you don't know the size of an allocation on the stack at runtime, do you really want it on the stack?
Mārtiņš Možeiko
2233 posts / 1 project
C Programming Tutorials
Edited by Mārtiņš Možeiko on
If you don't know allocation (or estimate, or upper bound, call it whatever you want), do you really want allocate at all, even on heap? How allocating on stack it is different from allocating anywhere? Imho you know your memory requirements, you know your bounds and you do whatever is the best way, including performance. Be it stack, heap or global static.
Ginger Bill
222 posts / 2 projects
I am ginger thus have no soul.
C Programming Tutorials
Very true. You need to understand what you are doing and why.

In regards with this series, I want people to understand the hardware and actually what is going on. VLAs, alloca and malloc can seem magic but beginners can easily misuse them so that is where the caution is coming from.
Jim R. Didriksen
63 posts
C Programming Tutorials
I believe I'm your target audience (or me a few years ago maybe)

I would prefer a Video series (a stream with a chat with a QnA afterward would save you from making statements with assumptions of knowledge.)

Keep it as similar to Casey's setup as possible to help people who started with Casey's videos, fewer things to learn/accept when you don't know what is significant will make it easier for people to jump between your series and his videos.

I have watched a ton of tutorial videos since YouTube was invented and the best ones just don't assume you know anything. https://www.youtube.com/user/thenewboston/videos thenewboston's videos are great if you know nothing, a lot of people hate his videos for not doing everything the "right way" but he makes it easy to follow and understand what he is doing. (at least this is true for his old videos, I haven't been keeping up.)

(Teach how to do something, explain what it does then explain how to do it better.)

You say you need to explain for a long while before you write the first line of C, I believe this will hard to follow. Write the code or batch file while talking about then pick it apart afterward talking in detail what something does and why it matters.

Explaining the parser/compiler/linker etc. before the viewer has or will need to interact directly with it will end up filling their heads with information they think they need to remember and understand before they really need to and their heads are full of the basics of int,char,for loops, if statements etc.

What I would want to get out of a C-basics series would be;

- To solidify my knowledge of where the C code ends and the C++ code begins.
- To learn the simplest types that make up the complex types.
- To learn the lowest code that makes up the complex code.

Do with this information what you will. (put in "I would" and "I would like" where i seem to bossy.)
Ginger Bill
222 posts / 2 projects
I am ginger thus have no soul.
C Programming Tutorials
Edited by Ginger Bill on
I remember watching some of the TheNewBoston's videos many years ago and found them very good for people without any experience in anything. I had to learn Java and I heard it was kind of like C so I was looking for tutorials and he popped up. He explained a lot it and I understood very quickly (even though I knew how to program already a true beginner could as well).

Through no fault of his, about 2 years after this for about 2 years, this was my "OOP everything" phase all because of Java. Luckily I don't have to touch Java any more!

I was thinking of laying out the series/course/whatever-it-is as if you were for a true beginner who wanted to learn C and the understand the hardware they a programming on not this software-ether. Explaing the parser/compiler/linker is needed (especially for MSVC) but I will not go into depth till further on. Initially all I will explain is that compiler turns your code into something the computer can understand etc.

This is the overview of the layout I was going to do:
  • Introduction
  • Installing and Using a Text Editor
  • Using the Command Line
  • Installing and Using a Compiler
  • Introducing C
  • Data and C
  • Formatted/Debug Printing
  • Control Statements: Branching
  • Control Statements: Looping
  • Functions
  • Arrays and Pointers
  • Strings
  • Structures, Unions, Typedef, Bit-fields
  • Input and Output and Files
  • C Preprocessor and Macros
  • Memory
  • TODO(bill): Figure out more
Andreas
23 posts
C Programming Tutorials
As Jim suggested, I also prefer a more pragmatical approach to teaching, do first, explain later. I think it's important to get the student engaged as soon as possible. Then any information that follows will seem much more relevant.

It seems to me that you're going for something similar. I don't have any opinions regarding the order of subjects. Looks good to my limited experience.
Dumitru Frunza
24 posts
Apprentice github.com/dfrunza/hoc
C Programming Tutorials
Slightly off-topic - I find this resource :
http://c.learncodethehardway.org/book/
well written.
The author also expresses the same "no non-sense" attitude as Casey does, which is a bonus.