r/programming • • May 01 '18

C Is Not a Low-level Language - ACM Queue

https://queue.acm.org/detail.cfm?id=3212479
152 Upvotes

303 comments sorted by

View all comments

Show parent comments

3

u/ThirdEncounter May 01 '18 edited May 02 '18

Don't C programs compile unmodified with a C++ compiler?

Edit: So the consensus is, not except for specific cases. Thanks, all.

20

u/defnotthrown May 01 '18

Just from memory VLAs and designated initializers are the two C99 features not supported by C++.

But they're available as custom extensions in many C++ compilers (since they mostly happen to be C compilers too) and designated initializers might make it to C++20 last time I checked.

so neither int a = getchar(); int arr[a]; nor int arr[5] = { [1] =50, [4] = 30}; are valid C++, but they are valid C

10

u/MayorOfBubbleTown May 01 '18

Most of the time. Beginner C programs will but the C programming language has added new features since the early 80s when C++ was created.

8

u/dangerbird2 May 01 '18

Aside from new features in C99 that don't exist in C++, implicit type conversions are different, especially for void pointers, which act as an "escape hatch" to the C type system, but require explicit type conversions in C++. A program with the line char *buffer = malloc(10 * sizeof(char)); would compile in C, but fail to compile in C++, where you'd need to cast the malloc to char *. public headers for C libraries also need to wrap functions with extern "C" when compiled in C++, so the linker does not assume the functions have a C++ API with name mangling to support function overloading.

14

u/Charles_Dexter_Ward May 01 '18

Most would but, for example, you can have a variable named "class" or "new" in C and a C++ compiler would complain.

4

u/meneldal2 May 02 '18

#define class notclass

3

u/Charles_Dexter_Ward May 02 '18

Yes, there are ways to modify C code to compile under C++. The point was that C++ is not a proper superset of C and there are valid C programs that do not compile under strict C++.

4

u/smikims May 02 '18

Not for anything non-trivial, but it's usually not that hard to convert a C program to valid C++.

4

u/[deleted] May 02 '18 edited May 02 '18
int main(int new, char **class) {
    int or_eq = 0;
    return or_eq;
}

Good luck finding a C++ compiler that will accept this (perfectly valid) C program.

Sometimes the program will compile but do completely different things. See e.g. https://github.com/mauke/poly.poly/blob/master/poly.poly.

Relevant excerpt:

char X;
int main(void) {
    struct X {
        char a[2];
    };
    if (sizeof(X) != 1) { 
    ...

3

u/Inex86 May 02 '18

No C++ compiler will compile this because the example makes no sense. Not sure what’s the point you’re trying to make. And your second example works exactly as intended, since X is redefined inside main and hides global scope char X.

1

u/[deleted] May 02 '18

Not sure what’s the point you’re trying to make.

The comment I replied to asked: "Don't C programs compile unmodified with a C++ compiler?"

I gave a concrete example of a C program that does not compile as C++, i.e. my answer is "no".

I also gave an example of a C program that compiles as C++, but behaves differently at runtime, proving that C++ is not a superset of C even if you only consider programs that are valid in both languages.

6

u/lelanthran May 01 '18

Don't C programs compile unmodified with a C++ compiler?

const works differently in C++ (it actually works) than in C (added at the last minute and doesn't work the same).

3

u/ais523 May 02 '18

Normally not, but the only common incompatibility is in the return type of malloc (although it's void * in both C and C++, void * has a different meaning in the two languages). Once you've worked around that, a typical C program is quite likely to compile as C++.

There are plenty of other incompatibilities that could happen in theory, but they rarely come up in practice.