Author Topic: Learning C through a fun project?  (Read 166 times)

Offline Vagabond

  • Hero Member
  • *****
  • Posts: 614
Learning C through a fun project?
« on: April 11, 2018, 06:38:19 AM »
Hey everyone,

I've enjoyed learning C++, but I tend to avoid facets of the language pre C++11. This means I don't have a great grasp on topics like raw pointers, the manual destruction of allocated resources, etc.

Anyways, I wouldn't mind spending time working with C instead of C++ to force myself to work on these features in an environment where things like classes and std::string and std::vector, etc are not available.

If anyone has an idea for a new or existing project that would be useful and/or interesting and that an intermediate programmer (for a hobbyist anyways) who is a newbie to C could contribute to, I would appreciate suggestions. It could be Outpost 2 related or completely not Outpost 2 related.

I would want to avoid using Linux or Macintosh as the primary development environment on my side (learn one skill at a time here). I would be comfortable working with Git or Mercurial for source control or something else.

I would probably buy a book to work on concurrently through.

Thanks,
-Brett

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 1717
    • LairWorks Entertainment
Re: Learning C through a fun project?
« Reply #1 on: April 11, 2018, 04:37:31 PM »
My suggestion is unless you intend to be writing kernel level drivers or writing software for embedded systems, don't bother. C is genuinely painful. You'd think it'd be very similar but I learned the hard way that it's very, very, very much not.

A possible good way to get started is to grab an open-source engine... say Quake or Quake 2, and pull it apart. That's basically how I learned it by pulling apart the Quake 2 code and rewriting half the engine. I ultimately ended up porting it C++ anyway but it was a huge learning experience.
- Leeor
LairWorks Entertainment

Titanum UFO's

Offline Arklon

  • Administrator
  • Hero Member
  • *****
  • Posts: 1159
Re: Learning C through a fun project?
« Reply #2 on: April 11, 2018, 08:28:57 PM »
On the other hand, C has designator initializers for arrays and structs (and has since C99), but because the C++ ISO committee prefers insanity, that incredibly useful feature was never ported to C++.

Otherwise, you're probably better off writing something in C++ just using the cstdlib and no STL, which is the bulk of what you need to learn to write C, or what you would be doing if you were making an STL replacement, etc. - just to save yourself the tedium that is the rest of what writing C is like, which is hacking in object-oriented programming using nothing but structs and pointers like what the Quake 1/2 engine do.

I would say it's valuable to learn a little about how C++'s extra features over C, i.e. OOP, overloads, operator overloads, STL, exceptions (yuck), etc. are typically implemented/how they get compiled, and understand the overhead of them and how to mitigate that. If you're conscious about that, it's not too difficult to write C++ code that's just as efficient as the equivalent in C.
« Last Edit: April 11, 2018, 08:54:17 PM by Arklon »

Offline lordpalandus

  • Hero Member
  • *****
  • Posts: 567
Re: Learning C through a fun project?
« Reply #3 on: April 11, 2018, 11:50:23 PM »
Libtcod, the library I'm using for my python roguelike, can be compiled for C and C++ as well. Could always make something with the C version of the libtcod library.
Currently working on Chaotic Planar Prison.
Link to OPU page = http://forum.outpost2.net/index.php/topic,6073.0.html

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 1717
    • LairWorks Entertainment
Re: Learning C through a fun project?
« Reply #4 on: April 12, 2018, 07:48:36 PM »
On the other hand, C has designator initializers for arrays and structs (and has since C99)

C++11 has something similar (or maybe identical?) for arrays. It's nice to be able to initialize a std::map<std::string, int> like this:

Code: [Select]
std::map<std::string, int> whatever =
{
    { "whatever1", 1 },
    { "whatever2", 2 },

    /* ... snip ... */

    { "whatever99", 99 }
};

For production/live code that does this see ProductPool.cpp from the OutpostHD project.

but because the C++ ISO committee prefers insanity

I haven't laughed so hard in quite awhile. But yes, I agree, C++ has lagged behind in a lot of very useful features. C++17 thankfully includes a lot of them.

I would say it's valuable to learn a little about how C++'s extra features over C, i.e. OOP, overloads, operator overloads, STL, exceptions (yuck), etc. are typically implemented/how they get compiled, and understand the overhead of them and how to mitigate that. If you're conscious about that, it's not too difficult to write C++ code that's just as efficient as the equivalent in C.

You have a point. I didn't realize how amazingly useful the STL was until I started pulling apart the idTech 2 engine. Holy shit, the ways to avoid pointer bugs in C++ makes C look like I'm carving stone tablets in Sumerian. But I also do appreciate what I can do with it on a very low level... e.g., if I was going to be writing software for an embedded system like a Rasberry Pi or even lower specs than that I'd probably stick with C with potentially some hand-tuned assembly.
« Last Edit: April 12, 2018, 07:51:09 PM by leeor_net »
- Leeor
LairWorks Entertainment

Titanum UFO's

Offline Vagabond

  • Hero Member
  • *****
  • Posts: 614
Re: Learning C through a fun project?
« Reply #5 on: April 13, 2018, 10:30:18 AM »
Thanks for everyone's advice. I actually like the idea of using C for embedded programming the most as this is something C really excels at. Maybe that isn't the best way to learn the language though.

I think my local library allows checking out Arduino (spelling?) boards or similar other products. Might be a good idea to check that out in more detail and try it out.

-Brett

Offline Hooman

  • Administrator
  • Hero Member
  • *****
  • Posts: 4205
Re: Learning C through a fun project?
« Reply #6 on: April 17, 2018, 02:23:19 AM »
Yeah, I was thinking embedded stuff. That seems to be the main use case for C these days. A C compiler is a bit easier to write than a full C++ compiler, so it may have more support for various low end and embedded systems. There's also less going on under the hood to surprise developers with hidden performance costs. That can sometimes be an issue with C++, especially for people who are less experienced with it.

A possibly fun and educational project might be to figure out how C++ features can be implemented in C. Such as how classes can be implemented with structs. How virtual functions work. How function overloads work. How namespaces work. If you're really looking for trouble, you can look into how multiple inheritance works. Though I should note, most post C++ languages dropped the concept of multiple inheritance, at least outside the more limited concept of multiple interfaces.

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 1717
    • LairWorks Entertainment
Re: Learning C through a fun project?
« Reply #7 on: April 17, 2018, 05:24:19 PM »
Multiple inheritance (especially the way C++ does it) leads to all sorts of ways to create massive (and very expensive) headaches.

My general rule of thumb is "If you're using multiple inheritance, you're doing it wrong". Granted there are edge cases where it makes sense but the second you run into the Diamond ProblemTM you find ways to build better inheritance graphs.
- Leeor
LairWorks Entertainment

Titanum UFO's

Offline lordpalandus

  • Hero Member
  • *****
  • Posts: 567
Re: Learning C through a fun project?
« Reply #8 on: April 17, 2018, 09:27:39 PM »
I'm assuming multiple inheritance means something like:

great-grandparent->grandparent->parent->child->grandchild ... etc.

Does C++ allow for Composition, like Python does?
Currently working on Chaotic Planar Prison.
Link to OPU page = http://forum.outpost2.net/index.php/topic,6073.0.html

Offline Vagabond

  • Hero Member
  • *****
  • Posts: 614
Re: Learning C through a fun project?
« Reply #9 on: April 18, 2018, 01:41:16 PM »
I bought the book Programming in C, 4th edition by Stephen G. Kochan https://www.amazon.com/Programming-C-4th-Developers-Library/dp/0321776410/

The book assumes no prior programming knowledge. The first chapter was pretty informative as an overview of how C came about and how a program compiles. Interestingly enough, Visual Studio 2017 doesn't fully support C11 and a lot of people will not use VS2017 when programming in C, I think due to it being a bit of a second class language on Visual Studio.

I've been picking up interesting tidbits like a c function assumes an int return if no return is specified in the prototype. Also, I didn't realize you could declare a variable that exists inside a function as static. Actually, I don't know if declaring a variable in a function as static is a C only thing or if it applies to C++. There is a keyword called auto that explicity states a function variable is not static as well.

He uses lots of mathematic examples like calculating all the prime numbers, calculating Fibonacci numbers, finding the square root, etc. He is easy to read and follow, so it is an enjoyable read so far.



lordpalandus,

multiple inheritance doesn't mean that. What it looks like you are describing is 'single' inheritance as supported by C# (only 1 parent per class). More specifically C++ also supports single inheritance in addition to multiple, but C# does not support multiple inheritance. Multiple inheritance is where one class has more than one parent class.

parent1 parent2
   |       |
   ChildClass

Using multiple inheritance on a regular basis is considered a poor practice as it can make the code base difficult to understand, debug, and continue to practice future inheritance on. The Archive code actually has a class that uses multiple inheritance. I'm sure there are edge cases where it is a good practice, although I've never come across a design on my own where it seemed the correct choice.

I've never heard of composition before. After briefly looking at it online, I think it is foreign to both C# and C++ although it does seem like an interesting idea.

-Brett

Offline leeor_net

  • Administrator
  • Hero Member
  • *****
  • Posts: 1717
    • LairWorks Entertainment
Re: Learning C through a fun project?
« Reply #10 on: April 18, 2018, 05:08:47 PM »
Quote
I'm assuming multiple inheritance means something like:

great-grandparent->grandparent->parent->child->grandchild ... etc.

What you described is a single inheritance hierarchy. Each child class only inherits from a single parent class.

In C++, multiple inheritance is when an object inherits from more than one base class. E.g.:


Code: [Select]
class Foo
{
    /* Whatever stuff here */
};

class Bar
{
    /* Whatever stuff here */
};

class FooBar: public Foo, public Bar
{
    /* I inherit from two parents! */
};


Quote
Does C++ allow for Composition, like Python does?

Yes. It's just a fancy way of saying that a class can use other classes as member variables. E.g.:

Code: [Select]
class Contrived
{
    /* useless stuff here */
};

class SomeClass
{
public:
    SomeClass() {}
    ~SomeClass() {}

private:
    int _someInt;
    std::string _someString; /* std::string is a class */
    Contrived _someContrivedObject; /* Contrived is a class declared above */
};

The above code is basically useless but it does illustrate composition. Basically, you're composing a class using other classes (and also non-classes if you want to be very broad about the definition).

A good way to think about it is like this: Inheritance means Class B is a Class A vs. composition in which Class B has a Class A.
« Last Edit: April 18, 2018, 05:11:44 PM by leeor_net »
- Leeor
LairWorks Entertainment

Titanum UFO's