0

First of all, I know the main differences between C and C++. But before I've thought that a certain subset of syntax for both languages is identical. However in passing discussion I've heard it mentioned that even if the program uses syntax limited enough to be legal and well-defined both in C and C++, it still can have different behaviour. I've met quite a few compiler-specific extensions that add C-like behaviour to C++, sometimes not quite as they actually work in C. But I can't think of differences for parts that are standard-defined.

To make this question better defined, here's what I'm interested in:

  • Languages compared are C11 and C++11
  • A single program that is well-defined both in C11 and C++11 standards. No undefined behaviours, no compiler-specific syntax. Any parts that are implementation-defined should be considered equivalent if possible.
  • Standard-mandated differences in behaviour of the same program depending on which language is applied.

Note that I realise it's much easier to find such a program for two specific compilers. What I'm interested is differences in standards specifically.

Alice
  • 121
  • 2
  • 2
    `sizeof('a')` is an easy difference (on most platforms) to take advantage of. – Mat Apr 24 '20 at 18:24
  • @Mat sizeof('a') is implementation defined. It *can* be the same, even if it usually isn't, so it doesn't count. At the first glance, in that question most answers are either using implementation-specific tricks, or things that became illegal in C11. The only two cases from that question that may fit here are the comma operator trick and the variable/static array difference. I'll go and check relevant standards to make sure it's valid examples. (As a side note, I have no clue why I failed to find that question, thanks for pointing it out) – Alice Apr 24 '20 at 18:54
  • 1
    `sizeof('a')` is _not_ implementation-defined in C++. The only place this "trick" would fail is if you find an implementation with `sizeof(char) == sizeof(int)` for both C and C++. – Mat Apr 24 '20 at 18:59
  • The accepted answer is fine in both C++ and C with another `sizeof` "trick", not sure what doesn't fit your rules about it. – Mat Apr 24 '20 at 19:06

0 Answers0