If you know c++, can you hack it in C?

If you know c++, can you hack it in C?
It's just the same thing except no classes, right?

Attached: kuruminha.png (900x900, 929.34K)

no. C++ is not a superset of C. they're diverged long ago. even the most idiomatic C code, won't even compile as C++

struct { int x; int y; } rect = { .x = 50 };

int *x = malloc(69 * sizeof(*x));

strlen((char []){ 'h', 'i', '\0' });

yes. Also you can only use (T *) style casts, but basically that. Also no namespaces.

>needs to come up with divergent niche features to prove his point
C and C++ are basically the same if you ignore classes and namespaces for almost all intents and purposes.

Attached: 1616242021445.png (748x766, 169.36K)

>divergent niche features
>compound literals and designated initializers are "niche"
lmao, no wonder seppels cucks think plain C is bad when they're using an outdated 50 years old version of it.

Sepples is basically a shitty language tacked on top of C. Most sepples code doesn't even resemble C at all.
Yes, you can use Sepples as "Slightly Improved C", or you can go full on std::stream

>std::stream

that first line compiles for me with g++ just fine
and the second line only needs a (int*) for the cast which is even C syntax

>that first line compiles for me with g++ just fine
gcc allows it as an extension. it was only recently added to c++20

g++ -std=c++17 -o c c.c -Wpedantic
c.c: In function 'int main()':
c.c:7:43: warning: C++ designated initializers only available with '-std=c++20' or '-std=gnu++20' [-Wpedantic]
7 | struct { int x; int y; } rect = { .x = 50 };
| ^


there's a lot of more things which are completely different between C and C++. here's another common mistake i see C++ devs make all the time.

[user /tmp]% cat test.c
int fun() /* this won't have any type checking. */
{
return 5;
}

int main(void)
{
return fun("hello", 5, "i can", 5.0, "pass whatever I want", '0');
}
[user /tmp]% gcc -o test test.c
[user /tmp]% ./test
[user /tmp]% echo $?
5
[user /tmp]%


you are guranteed to write shit code if you treat C as a C++ subset (which it's not).

this is not idiomatic c

the only c idiom which will cause you trouble is automatic void* conversions with malloc

That's undefined behavior, retard.
>inb4 it works on my machine!
That doesn't make it not UB.

>That's undefined behavior, retard.
no, that's legacy braindamage. pre-standard C allowed you to not specify the arguments for a function, and they kept it in the standard for C89 so that older C code can still compile.

Declaring a function without specifying arguments is standard. The UB part is calling it with different arguments than it is defined with. On some platforms it can screw up your stack.

what about smart pointers
and lambdas
and templates
and iterators
and range based for loops
and auto
and const expr

those are almost all classes

>On some platforms it can screw up your stack.
exactly why i posted that as an example. in C++ fun() and fun(void) are same, in C they're not.

> you are guaranteed to write shit code if you treat C as a C++ subset (which it's not).

Ah, I see your point now. Yeah, I'm in the habit of writing foo(void) in c++ regardless

It's no classes and no lots of other shit as well, but yes if you know C++ you can probably handle C after some adjustment.

Well, it's clear not all C code can compile with a C++ compiler.
Even something as simple as
int class = 5;

Will compile on C but not on C++ for obvious reasons.

digits checked 😈

>If you know c++,
False premise. Nobody knows C++.

>Sepples is basically a shitty language tacked on top of C. Most sepples code doesn't even resemble C at all.
Honestly, it wouldn't even be THAT bad if C++ were actually a proper superset of C, but even that isn't true. There are a number of subtle, but very important differences that will cause valid C code to cause a compilation error or behave differently with a C++ compiler.

A notable example is toplevel const non-volatile variables: they have external linkage by default in C, just like non-const variables, but internal linkage in C++ (an extremely stupid decision that I still fail to understand). And of course, malloc requires a cast in C++, when it's not required in C (and it's usually considered bad practice to do so).

This amount of subtle differences is enough to conclude that C and C++ should really just be approached as two separate languages, despite their common ancestry. Trying to apply concepts of one to the other will have catastrophic results.

ackshually if you use #define class nigga it will compile

That's UB.

Early const was supposed to work like constexpr but they half-assed it.