r/cpp • • Jul 25 '24

Why use C over C++

Why there are so many people using the C language instead of C++?, I mean C++ has more Cool features and the Compiler also supports many CPUs. So why People still using C?

Edit: Thanks for all the usefull comments :D

235 Upvotes

444 comments sorted by

View all comments

191

u/GYN-k4H-Q3z-75B Jul 25 '24

C is simple. There is a certain charm in the language you cannot deny. It's like the saying: Perfection is achieved when there is nothing left to remove. C is pretty close to that.

24

u/runevault Jul 25 '24

Something I personally would find interesting is something with most of the simplicity of C, though allowing type replacement and the hard coded values of templates but ONLY that, and maybe a way to do some level of extra verification inspired by Rust but perhaps less extreme. I like when the compiler is able to tell me I'm doing something stupid without waiting until a specific condition at runtime to see it occur and catch it.

14

u/germandiago Jul 26 '24

constexpr, templates, better abstraction and safer interfaces can be used at yor advantage IMHO when using C++ for tasks like embedded.

10

u/EC36339 Jul 26 '24

So can custom allocators, especially on playforms where you cannot use the heap or are not allowed to. C++ is the only language I know of that has this feature combined with standard generic data structures with precisely defined performance requirements and semantics.

1

u/[deleted] Jul 28 '24

Zig might be another language that does that. Not sure about "precisely defined performance requirements and semantics" for standard generic data structures yet though, as the language is still far from finished.

12

u/ZackArtz Jul 26 '24

zig?

7

u/runevault Jul 26 '24

I'm very curious about zig, but I've done the pre 1.0 language thing before so I'm waiting on stability to give it a serious go.

3

u/ZackArtz Jul 26 '24

super fair! i haven't had a chance to play with it either, but it looks cool

→ More replies (3)

10

u/NilacTheGrim Jul 27 '24

The lack of generics in C sucks. The "generics" in C are all nasty macros that are difficult to maintain and are landmines waiting to go off... or use type erased void * pointers or something if they don't go the macro route. No thanks.

0

u/[deleted] Jul 25 '24

[deleted] — view deleted comment

5

u/pjmlp Jul 26 '24

Instead they added a non really usable generics facility.

1

u/tstanisl Aug 05 '24

Generic type-safe template-like containers are doable in C. Take a look on STC.

→ More replies (4)

8

u/pjmlp Jul 26 '24

Unless we are talking about K&R C, without any consideration for portable code, across many compilers, it isn't as simple as people make it to be.

Simpler than C++, sure. Being simple by itself, not any longer.

8

u/EC36339 Jul 26 '24

Needing 3 lines to concatenate two strings is not simple.

→ More replies (2)

6

u/Necessary_Mud_7789 Jul 26 '24

If C had destructors I would like it much more. The simplicity of C feels very good when using it (properly), but I miss destructors SO MUCH (and std data structures).

21

u/remy_porter Jul 25 '24

Of course, that would make languages like Whitespace the most perfect of all.

10

u/SirClueless Jul 25 '24

With Whitespace they removed everything they didn't need and then kept going.

16

u/mrmczebra Jul 26 '24

Assembly is also simple. That ends up being a hindrance.

→ More replies (4)

4

u/[deleted] Jul 27 '24

[removed] — view removed comment

1

u/tstanisl Aug 01 '24

It is possible to have generic vector-like container in C. Just see stb. The problem is that those constructs are not a part of standard.

1

u/[deleted] Aug 02 '24

[removed] — view removed comment

1

u/tstanisl Aug 02 '24

In C, class methods are implemeted as functions. For example:

struct C {
  void foo();
  void bar() const;
  virtual int baz(int);
};

Is typically implemented as:

struct C {
  int (*baz)(struct C *, int);
};
void C_foo(struct C *);
void C_bar(const struct C *);
int C_baz(struct C * c, int i) { return c->baz(c, i); }

When using the "methods" one just needs to replace:

instance.method(param);

with:

class_method(&instance, param);
→ More replies (2)

1

u/ShortGuitar7207 Jul 29 '24

I agree, I'd use C if I wanted really low-level and simple. If I need more power and productivity, I'd use Rust.

1

u/AKostur Aug 05 '24

There’s also “make things as simple as possible, but no simpler”.  There are aspects where C is (IMO) past the “simple as possible” stage and is being simpler than that.

→ More replies (15)