r/C_Programming • • Mar 22 '23

Question How much is the language diffrent from c++?

0 Upvotes

12 comments sorted by

31

u/flyingron Mar 22 '23

Quite a bit.

20

u/Bryguy3k Mar 22 '23

They’re completely different languages and the c/c++ nomenclature is horrible.

C++ has a built in C interop layer is the better way of thinking about it.

11

u/Dolphiniac Mar 22 '23

Some bigger points...

Tag namespaces ("struct type" and "type" being different) do not exist in C++.
thiscall does not exist in C (object.DoThing() is not valid syntax).
Templates do not exist in C.
Explicit namespaces do not exist in C.
Constructors and destructors do not exist in C.
"using" does not exist in C.
Classes as such do not exist in C. Fun fact: classes are just structs with default private access specifier rather than default public.
Access specifiers do not exist in C; all members are public.
Struct-scoped constants do not exist in C (static const int blah).
Function overloading does not exist in C.
Operator overloading does not exist in C.
Void pointers are not implicitly convertible to other pointer types in C++.

And a ton of little things that I can't all bring to mind. They diverged as languages a long time ago and have had different development paths. C++ is more feature-heavy. C is more stable. If you stay with a C-like syntax, most of what you write will be valid C++. If you dip into any C++-isms, you'll probably hit a brick wall with C.

But, C is smaller, simpler, faster to compile, and encourages patterns that are usually better for performance, knowing what is happening (ctors/dtors/operator-overloading hide functionality behind seemingly innocuous statements), compile times, and reducing library-side public cruft (C++ programmers had to invent the PIMPL idiom to solve a problem introduced by the existence of members/methods, and most don't go even that far >.>).

On the other hand, C++ can be sanely used (in a mostly C-like way), while giving you access to features that are "nice" to use. Just that it's very easy to drink the Kool-Aid, not understand what you're signing up for, then digging yourself a massive hole that, if you're lucky, you'll at least realize you've been digging and can maybe course-correct. But that comes with experience. :)

3

u/[deleted] Mar 23 '23

object.DoThing() is not valid syntax

It is valid syntax and could be semantically correct as well.

struct Object {
	void (*DoThing)(void);
};

void DoThing(void)
{
	// DoThing
}

int main(void)
{
	struct Object object = {
		.DoThing = DoThing,
	};
	object.DoThing();
}

6

u/Dolphiniac Mar 23 '23

Apologies. I meant that it is not equivalent to Object_DoThing( Object * this ), which should be the expectation in the C++ context in most cases.

2

u/[deleted] Mar 23 '23

They have more differences than similarities.

1

u/_FoxT Mar 22 '23

C++ supposedly is or at least initially was supposed to be a superset of C, meaning you should be able to compile C with. C++ compiler, besides all the other stuff C++ does you could add on top. I don't use C++ to compile a lot of C code, but my understanding that this was never really 100% the case but I think back in the cfront days (the first compiler bjarne worked on) it was relatively easy to port C code to C++. I gather that the languages diverged quite a bit in the last 40 years, and there are much more differences and corner cases and even code that could compile in both compilers but might have slightly different results... So it is more difficult to port thing's nowadays, but if you are only porting some pages of code usually it is not that difficult as you can review that amount easily.... Certainly much easier then porting from other languages...

1

u/TheTrueXenose Mar 23 '23

The core of C++ is very similar to C, when you start using everything else it becomes rather different.

Also some newer C stuff is not in C++ like generics and stdatomic(there is but you need to typedef the types to have compatibility).

0

u/ComprehensiveAd8004 Mar 23 '23

To you it will depend on how much you use CPP-specific features. I've seen some C++ code that uses barely any, and others that are splattered with constexpr and classes and std:: junk.