33

I have recently gotten into C from C++, and I have noticed that often people answer with the C++ way to do something in a question tagged C.

This is slightly annoying as C++ is (more or less) a superset of C, but C is not a strict subset of C++. There are quite a lot of features common to both C and C++; there are some features that look the same in C and C++ but have different meanings (e.g. sizeof('a')); there are some features in C that are not in C++ (e.g. variable-length arrays and flexible array members); there are many features in C++ that are not in C (e.g classes, templates, and exceptions).

+----------------+
|   C            |
|       +--------+----------+
|       | Common |   C++    |
|       | Core   |          |
+-------+--------+          |
        |                   |
        |                   |
        |                   |
        +-------------------+

clarification: I'm talking about where you write an answer only applicable to c++ and by saying c++ is a superset of c I mean c k&r c.

15
  • 18
    If the question is tagged c, it will garner C answers. If the question is tagged c++, it will garner C++ answers. If it is tagged with both, people will point out that those are two different languages and to figure out which one is relevant to the question.
    – Eljay
    Commented May 13, 2021 at 21:17
  • 11
    Without examples this question is not very useful. With examples it might fit in [meta].
    – Eugene Sh.
    Commented May 13, 2021 at 21:19
  • 28
    This is slightly annoying as c++ is a superset of c, but c not of c++. I would say that is not exactly correct. There are items in c that are not valid in c++ like the VLAs
    – drescherjm
    Commented May 13, 2021 at 21:21
  • 16
    @drescherjm Or valid, but behave differently, which is even worse
    – Eugene Sh.
    Commented May 13, 2021 at 21:22
  • 8
    @EugeneSh. - I describe it as C++ being derived from C. While it retains much of C's original syntax and semantics, it doesn't retain all of it. Also, C has changed in the time since C++ was developed and many of those those changes (such as VLAs) haven't been incorporated into C++, so we have legal C programs that are not legal C++ programs, and legal C programs that are also legal C++ programs but with different behavior.
    – John Bode
    Commented May 13, 2021 at 21:32
  • 4
    I see C and C++ the same way I see apes and humans. Both descend from a common ancestor. Both are different from this common ancestor, with C retaining greater similarity, becoming a better, or better suited for the requirements of today, C. C++ diverged greatly from the C of the 1980s immediately. Do not infer from this that I think C++ is better or more "evolved". C makes a better C than C++ does and C++ is a better C++ than C is. Commented May 13, 2021 at 21:48
  • There are some underlying features in C++ that are not in C, such as exception handling, e.g. try and catch. Also, inheritance. These features require a change to the language so don't consider C++ as a superset of C. Another issue is function overloading. Commented May 14, 2021 at 0:55
  • 2
    How is this question not "about Stack Overflow or the software that powers the Stack Exchange network"? It seems like it rather clearly is (it's asking about a pattern of behavior they've observed on the network). Heck, that's why it was migrated to Meta by a moderator in the first place. I've voted to reopen.
    – Ryan M Mod
    Commented May 14, 2021 at 1:03
  • Sometimes the question is tagged C and the example code in the question uses C++ stuff. And sometimes it's the other way around - usually because the OP is using a C++ compiler and has tagged correctly but just aren't using any C++ stuff. That second case always brings comments and answers saying to use vector and unique_ptr which the OP clearly doesn't know and I usually answer those with code that would compile in C just because the OP knows how to read code like that but I worry I'll end up with downvotes for not telling them to C++-ify their code. Commented May 14, 2021 at 1:11
  • 2
    C++ is not a superset of C, for example they differ in how they implement complex numbers. They're different languages and that's why talking about C/C++ language is usually frowned upon on SO
    – phuclv
    Commented May 14, 2021 at 3:13
  • 1
    @RyanM I have no idea why the other close voters chose that reason, but I voted to close it as unclear because I can't figure out what the actual goal of this question is. Commented May 14, 2021 at 19:36
  • 1
    The part of C what is not in C++, is very little. Your image is misleading. In my experience, this type of falsification serves some type of ideologist purposes, although I never understood exactly what (many C++ programmers vehemently state that C++ is a different language, and not an extension of C. Truth is that yes, it is).
    – peterh
    Commented May 15, 2021 at 19:31
  • 1
    even worse. Some answer in C#
    – Ol Sen
    Commented May 15, 2021 at 19:51
  • 1
    @peterh: Some language-lawyer questions do need to distinguish C from C++, even when asking about the rules for code that looks like it's only using the common subset of both languages. I agree with your sentiment in general that some people take it too far, and a C++ question can be a duplicate of a C question for example, if (and only if) the C way is also the best way to do something in C++. Especially in cases where C or C++ just happens to be the language used for making system calls, and that's what the question is really about, not deep C details. Commented May 16, 2021 at 16:18
  • 1
    @OlSen c# answer deserves down + NAA flag on the spot
    – peterh
    Commented May 16, 2021 at 16:32

6 Answers 6

6

We have policies in place for cross-tagging C and C++ and mixing the languages, which were fleshed out couple of years ago with community consensus here. The rules from there are now listed in the C and C++ tag wikis respectively, quoted below for your convenience:

Using and together

C and C++ are two distinct and often incompatible languages. Avoid using both tags in the same question unless you have good reasons.

A question should be tagged with only, if:

  • It contains pure C, with no trace of C++, or questions with code that could be either language.
  • The code is compiled with a C compiler.

A question should be tagged with only, if:

  • It contains code with any C++ features. Even though the code may be "C style".
  • The code is compiled with a C++ compiler.

A question should be tagged with both and if it is about:

  • Specific differences between C and C++.
  • Compatibility or porting code between C and C++.
  • C++ code that uses C libraries (for example code using extern "C").

From this we may conclude that it is fine to ask "C style" questions using the C++ tag only, assuming that the code is compiled in C++, using extern "C" etc. There's no policy for "C style" answers but it seems reasonable that the same should apply there. C compatibility might be a valid concern. If not, it's mostly a matter of style and if you don't like an answer's style, you are free to down vote it. It's still a valid answer though.

With one exception, also from the tag wikis:

Editing and moderation guidelines for posts with both and tags:

/--/
If you encounter a post with both tags, edit/re-tag it if needed according to the above rules. If you can tell the language by reading the posted code, simply edit tags accordingly. Avoid prompting the user "is it C or C++?" in comments unless the question is truly unclear.
/--/
Answers with C++ code to a C question that has never been tagged should be deleted as off-topic. Please check the question edit history before flagging/deleting such answers, to verify that the question never had the C++ tag.

That is, when someone who posts a C++ answer (or Java, C# etc answer) to a C question out of the blue. Be careful to check the edit history when that happens, since the question could originally have been tagged C++ and then the answer is fine.

If a question was always tagged C++ and someone drops a C only answer using features unique to C (VLA, _Generic etc) then such an answer would be equally off-topic.

The usual procedure for blatantly off-topic answers would be to down-vote, poke the poster with a comment to give them a chance to fix it, then if no reply after a long time, maybe start a "very low quality" delete procedure by flagging the off-topic answer.

44

If the question isn't also tagged C++ (and never was - make sure to check edit history), I'd leave a comment under the answer, telling the user they used the wrong language. (Once you have enough reputation, that is.)

Usually, it's an honest mistake, which I made myself a number of times.

3
  • Some instances may be honest mistakes, but more often then not I suspect people just trying to post an answer, any answer, in an attempt to farm reputation. Often the post doesn't even answer the question, but that's yet another problem.
    – Askaga
    Commented May 16, 2021 at 15:25
  • Downvote, besides commenting.
    – usr1234567
    Commented May 16, 2021 at 19:50
  • 6
    @usr1234567 When I make this mistake, I appreciate being politely told about it rather than getting downvoted, so I tend to do the same. If the author refuses to correct their mistake, then I downvote. Commented May 16, 2021 at 19:53
38

One reason is that a fair number (actually too many, so an unfair number) of people tag their questions with both C and C++ even though they are only asking about one or the other. This may be due to novices being unaware there is a difference or uncertain what it is or due to attempting to learn and write C code using a C++ compiler. (The latter may be an unfortunate side effect of Microsoft Visual C++.)

Whatever the reason a question is tagged with both, it may be answered by people following the C tag and/or people following the C++ tag. After such answers are entered, somebody may delete one of the C or C++ tags, judging it to be inappropriate. By the time you come upon the question, it may have a C++ answer but only a C tag, or vice-versa.

7
  • 23
    And , as usual, the only way to completely solve the problem is exterminate mankind. For some reason this option is ill-favored when I bring it up. Commented May 13, 2021 at 21:37
  • 3
    @user4581301 propose it on 'Homework Sunday'- I can guarantee one enthusiastic supporter:) Commented May 14, 2021 at 5:14
  • 3
    Not necessarily just Microsoft Visual C++. I first encountered C on Solaris and I was quite puzzled to find out that certain syntax worked only after I renamed a source file from .c to .cc. Had no clue that this made the compiler to assume (pre-standard) C++. Commented May 14, 2021 at 19:21
  • oh yeah, you can compile C code and C++ code with gcc
    – qwr
    Commented May 16, 2021 at 4:05
  • Actually Visual C++ looks at the extension and if it ends with .c, it will use a Plain C compiler.
    – sashoalm
    Commented May 16, 2021 at 11:00
  • 1
    @Eric, is it possible to replace [c] with [plain-c]? This might make peope less likely to make that mistake. Typing [c] in the tag editor could autoexpand to [plain-c].
    – sashoalm
    Commented May 16, 2021 at 11:03
  • I've asked questions with both C and C++ tags, for example about Arduino. Technically, the language is C++, but many libraries and classes are missing (e.g. std::) and it's usually better to use c strings than dynamic Strings, to limit memory usage. That's why I also tagged it C. Commented May 16, 2021 at 14:52
28

Sometimes as a C++ user, I come across a question and think "hey I can answer that" without noticing that the question is tagged "C". I know I've done it at least once, I'll try to provide a link later.

Here it is, my shame on public display: https://stackoverflow.com/a/13388063/5987. Despite being a C++ answer on a C question, it got 7 upvotes; someone must have found it useful. But the comment pointing out my mistake has 15.

3
  • 20
    Dragging your own shame into the spotlight, just to shed some light on a discussion. My respect.
    – Yunnosch
    Commented May 14, 2021 at 1:09
  • 6
    @Yunnosch thank you very much. Obviously the incident left an impression on me since I'm able to recall it 8 years later - but it's worth more now as an object lesson. I've long ago gotten used to the fact that my embarrassing moments will live on the internet forever, I don't try to delete them. I hope I haven't repeated the mistake since then but I honestly don't know. Commented May 14, 2021 at 3:16
  • Isn't the syntax highlighter trying to guess the programming language every time a page is loaded? If only it would do so already at answer creation time it could maybe warn if the question tags and the detected language do not agree (or simply display the detected language, so one can easier override it manually if needed). Commented May 16, 2021 at 20:53
13

To be fair, there are some questions like bit hacks where the simple answer is the same in C and C++. (e.g. How to count the number of set bits in a 32-bit integer? - although without language tags it's attracted answers in multiple languages. The top answer has code that is both valid C and C++, and describes in comments the minor changes to make it valid Java. Is that a good example of how SO questions should be asked? No, probably not, these days we'd want a language chosen, because many languages that aren't stuck in the past like C have a builtin for this.)

SIMD intrinsics (Intel, ARM, or whatever) are also pretty much the same whether you're using them from C or C++.

In cases like that, the interesting / hard part was independent of the language, and the answer is still highly useful to someone using the other language. It's still better to have the answers fully work in the language the question asked for, but cases like this are presumably where sloppy habits of using the wrong language can develop.

Or for example, a question that's really about system-call semantics, like fork/exec and dup2(), or about how open(O_APPEND) affects later write calls, might be written in C or C++, and the answer wouldn't really depend on either. Or using some library. So it's not just asm/performance-optimization stuff that can fall into this category of C vs. C++ being incidental to the real question.


Another point to consider is that if a problem is specialized enough, do we really want two separate questions for the C and the C++ version of it? If a C++ answer to the question could be significantly different from existing C answers, it may sometimes make sense to leave a C++ answer for future readers who are using C++ but find this C question. That's a less work and not a lot worse than creating a new self-answered Q&A and linking both ways from one to the other to make sure people can find the other whichever one they stumble onto first. (I'm not 100% sure this reasoning is really justified; I don't have a good example off the top of my head.)

Of course, if you do this on purpose, you should point that fact out, and ideally note any non-obvious C vs. C++ differences that it relies on. e.g. the inline keyword, or a const variable being legal in a constant expression (like the size of an array that can't be a VLA, like in a struct or global).

2
  • What "bithacks"? A web site? Hacks with bits? For the latter, it should probably be "bit hacks". Commented May 17, 2021 at 10:09
  • @PeterMortensen: Oh, apparently most google results for "bithacks" do have a space, as you say. Thanks. Commented May 17, 2021 at 16:44
5

TL;DR - Because some users find that the OP can simply adapt the code to the other language, as both languages have similar code structure, syntax, and compilation.

Disclaimer: I don't follow the tag, or the tag, but I highly relate to the situation from following another tag. As you have pointed out, C++ is mostly a superset of C. They both have similar code structure, syntax, and compilation.

Of course, that is not really a good reason to answer a question tagged with one of the languages in the other language, but I believe the concept is similar to something that I often see occur ever since I started following the tag.

You see, OpenCV works in many different programming languages (including and ), and even though OpenCV is OpenCV, using it in different languages comes with different features here and there (and different syntax, of course).

It's not uncommon to see the OP of an tagged question accept an answer that is in a programming language different from the programming language the question is tagged with, and that is because it's not too difficult to adapt the concept of the code in the answer to the language the OP is using. Just a few days ago a user awarded a bounty to a user who provided a python-based answer on their android question about OpenCV, and they accepted my python-based answer on the same thread.

As C++ and C have similar syntax, I'm sure plenty of people find an answer in the other programming language adaptable, hence helpful, and would much rather take it than not receive an answer at all. Of course, this would likely only apply when the answer has been properly explained (if that's not what all answers optimally should have).

1
  • 2
    If one intentionally gives an answer in a different language I would at least clearly mark it as such and also mention why I'm unable to provide a solution in the desired language. Something like: "Sorry, my C is a bit rusty, but I know a lot of C++ and I would solve the problem in C++ the following way: ..." That way people would have a fair warning what is coming at them. If the solution is then easily adaptable, it's probably also good if someone else can provide an adapted solution in the desired language, just for completeness. Commented May 16, 2021 at 20:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .