Do I learn C or C++ for SDL? I have Zero programming skills and want to learn

I have zero knowledge on how to code but I want to learn a programming language as a side hobby and was interested in SDL. My main question is do I learn SDL in C or C++? I was skimming some tutorials that’s in C++ but people are saying that SDL is a C programming language so I am confused on what programming language to start out with before jumping into SDL.

I use C++, mostly because it keeps my code tidier when you group together all code related to one thing as a C++ ‘object’.

I think SDL is in C to make it more portable and give fewer problems compiling their exact same SDL code on different operating systems.

I’d recommend using C++, but bear in mind that C++ is just C with objects added on, so if you learn C++ you’ll be learning C first anyway!

Agree on Sean comment. On top of what has been said, one way to see this you can mostly use “C code” and use C++ libraries wherever it makes your life easier, for example, when need a map, a list, vector, stack, queue, etc that are all included in the standard library.

This has been my approach while experimenting with SDL.

I think it makes a lot of sense to learn C before learning C++ - I think it’s hard to understand how C++ works without that background.

SDL is a C library but can be used from C and C++ in the same way, so if you start with C and SDL, what you’re learning there is directly applicable when (if) moving to C++ later.

I have written code both in C and C++ at work.

I also think that you should first learn C. Read the Kernighan and Ritchie book, i think this is the 2. edition https://hikage.freeshell.org/books/theCprogrammingLanguage.pdf , this is in my opinion the best book about programming ever written.

What concerns C++, think why you need C++ or do you need it. The principles of object-oriented programming that originally came from Simula, are of course important to know, and one can somewhat use them in C as well. C is not very type safe, but there are debugging methods to overcome this.

There are though two problems when implementing these principles in a language. First, such language is too complex and impossible for most humans to completely remember. Second, having so many features in a language makes debugging more complicated. C is simpler, and so all is debugged in the same way. In spite of all the advanced features and safety of C++, C code is still the most bug free. And how advanced is the code, doesn’t really come from how advanced programming language it is written in, as some may believe.

Using C++ often comes from the fact that many libraries are written in C++. C++ is not however just an extended C. It is true that C++ contains all C, but when using the features of C++, also from the libraries, things need to be written in the C++ way. And when writing code in that way, C++ is a completely different language than C.

Thanks for the reply guys, I guess I’ll stick with C for now and then grind out C++ in the near feature if I need to.

Hi,

my 2p after years of coding in C++ and contributing to some C github projects.
True that modern C++ is “something else” and very few people can claim to know the inside out of the current standard (I am not one of them).

But, if you go with C, be prepared to spend a very large part of your time trying to

  1. concatenate 2 strings (a secure and portable solution will require a lot of googling to avoid the unsafe alternatives)
  2. what about creating and passing around a vector of numbers (let alone matrices)
  3. valgrind will become your best friend in the never ending effort to find and fix memory leaks (and array out of bounds)

So my suggestion is C with the addition of

  1. std::string
  2. std::vector
  3. std::shared_ptr
  4. classes (just classes, you can ignore inheritance and vtables)

You will then be able to focus on your project rather than rewriting safe versions of the above in pure C.

Cheers

Yes, versions of these can be very easily written in pure C. The key is to first implement a dynamic vector or string, that can even be an exercise for the beginners.

I can never believe that the authors of C didn’t know the object-oriented principles from languages such as Simula. They omitted things, and did it for purpose. They knew very well what they were doing, their aim was a reliable language that can be used for creating operating systems. Some things that they omitted can be implemented in C, also posix provides some things that are missing in C, it was meant to be so.

C and C++ both currently have a lot of “details” that get in the way of learning introductory programming concepts and structures. If you have to choose between just those languages for SDL, though, I’d suggest C++ just because it has a few quality of life features that C does not (function overloading, stricter typing, classes, namespaces, etc.). If you actually learn modern C++ instead of learning it as “C with extras”, you’ll also avoid a lot of the pain that we all had to endure in the past.

My background: I read an intro C book 16 years ago, then learned C++ while doing SDL stuff. Now I teach at a university and do C# first semester, then C++ second semester.

Type safety is yes what they pay so much attention to in C++. Undoubtedly important, but requires a lot of additional language features just for the type safety. There are testing and debugging methods to overcome the lack of type safety in C.

But basically, you both suggest one thing, use C++ just to use a few features from there, in addition to C. The problem with that is that when something is written in C++, one can use everything from C++ there, and it is no longer determined what language features are used and what are not. This goes all the way to how to handle and test that code. Also when a library or an initial code is written in C++ for whatever reasons, then this forces anyone who uses it to use C++ as well, writing it in C is no longer a reasonable option.

There is one more thing that i think the beginners should know. Instead of using the C++ smart pointers, add all allocated resources to a dynamic array, and then release them in the end with one function, in the backwards order. Why in SDL every structure starts with a type member, which is always some constant? It is because due to the C inheritage, when casting that structure to the type of the first member, one always gets that member, no matter what the type of the structure is. This is done for a similar purpose, to know the type of the data, to know how to handle it and how to free that data.

Freeing the resources and the dynamic array are also implemented in my GUI toolkit. I used Valgrind to make sure that there are no memory leaks, there never were. Of course i found the Xlib memory leaks, but these are likely not because Xlib is written in C, but because Xlib is badly written.

Likely the same should be done in any C code, not only in a GUI toolkit.

One more thing that the beginners should know. It is easy to avoid global variables in C++, like by using a copy constructor for an object and passing all data when creating it. How to do it in C, is to pass variables to all functions in the structure called context. That’s the C way of doing it.

These are the things that should be told to the beginners of C in the beginning, but they are not.

Learn C++ or learn both. You can learn C++ without knowing C, lots of C++ book are available that will give you that foundation, C++ is C with objects.

One learns a lot when one learns C. Somewhat inconvenient to do some things, but convenience is never important for beginners. When it is only a hobby, it may be fine to learn just C, though it depends on what you do. But if it’s more than that, maybe learn then C# instead, it has SDL bindings as well. Honestly i like C# much more than C++, though yes it is interpreted, and it is commercial. But C# may enable you to get some job much more than C++, and C++ is going down. Or even learn python, it has SDL bindings in pygame. But by avoiding C++, you at least omit one huge thing from the dustbin of your brain. You can be certain that C will never disappear, in spite of all its deficiencies and outdated standard library where some functions should never be used, will not disappear hundred years from now, it is so important. So if you want a glimpse to the world where all is fast and everything can be done, learn C.

While it may be true that you have to do more work in C. Let me give one big pro for C – longevity.

I have C programs that I wrote 30 years ago, and I can simply get out without having to make any changes, and they still compile and run today. Even when using external packages such as n/curses.

On the other hand, I’ve tried to compile C++ programs written by others that are barely 5 years old, and already C++ has changed so much they can’t be compiled anymore without significant work. That’s my major complain against C++, and why I write my own stuff in C.

That said, there’s something odd when using a C-compiler with SDL2. I’m new to coding in SDL2 – as in just today trying to learn the basics – and using the tutorial from “Lazy Foo”, I’m finding crashes caused when I compile with gcc (or clang) vs. g++. And the only non-C stuff in the code is “bool”, which I converted to “int”. But move stuff around, and it works. But that’s for another thread, and that thread already exists: libsdl-failing-to-initialize-with-double-free-error

The only correct answer is Ada!

On a more serious note, in addition to the QoL features of C++, you really can’t understate the importance of OOP concepts in making games and other complex applications. It’s definitely doable in C (every algorithm thing is; in fact, some object-oriented languages are even transpiled to C anyway), but you’ll have a lot more flexibility if you can, for example, define a game_object class and make your all your avatars, vechicles, powerups, obstacles, monsters, etc extend it. Then you can do stuff like add them all to the same list and sort it by y coordinate so closer objects are always drawn on top of further ones, or make reusable graphical menu code where you can mix and match also-reusable selection types. These kinds of things will most easily be done with a lot of hard coding and poor DRY (don’t repeat yourself) methodology in something like C that isn’t built for OOP.

(Also, I was serious about Ada. It rules. You just have to be a little crazy to appreciate it haha.)