44

Lately, I've seen a couple of C questions "with some C++". Here is one: Why does my function only update the struct when I pass it with an & symbol?

Quote from questions in case it disappears:

For those asking, we've been taught to code in C using some C++ elements (such as the pass by reference from C++ or cin/cout) to make it easier, therefore it may seem to you that the code is not C nor C++.

How should this be handled? My spontaneous thought is that if you use even the tiniest bit of C++, then you need to compile it with a C++ compiler. This means that you instantly get the restrictions that C++ has, but C doesn't.

I could start a rant about why this is stupid, like that it creates bad habits such as casting malloc, but that's not the topic here.

I think that these questions should be tagged with C++ and not C. Am I right? After all, it is not valid C code.

11
  • 5
    related meta.stackoverflow.com/questions/252430/… Commented Dec 15, 2017 at 12:44
  • 9
    I am inclined to agree with this.. stick to one language at a time Commented Dec 15, 2017 at 12:49
  • 4
    There are rarely questions that are tagged with c and c++ justifiably. Most of them are carrying one of these tags wrongly, and because of unconciousness that c and c++ are totally different programming languages. If you see a question containing plain c code, edit and remove the c++ tag please. Same vice versa if anything in the question is c++ code, remove the c tag. Commented Dec 15, 2017 at 12:55
  • 5
    @SurajRao There are rare cases where both tags are justified though. Especially questions aboput interfacing C++ with C code and vice versa. Commented Dec 15, 2017 at 12:56
  • 1
    @user0042 true. but the current scenario is about mixing C and C++ elements in a very different way Commented Dec 15, 2017 at 12:58
  • 7
    I would absolutely tag this as C++, but mention that the coding style is "C with a bit of C++". C and C++ together is mostly for interfacing between the 2 languages. Since you are compiling with a C++ compiler, tag it as C++ Commented Dec 16, 2017 at 3:29
  • 7
    If there is code that a C compiler will reject but a C++ compiler will (or might reasonably be expected to) accept, it should be tagged C++. If the code is just C with no distinctive C++ features, it should be tagged C. I pity the poor student being (mis)taught — that's an appalling teaching methodology. Commented Dec 16, 2017 at 10:06
  • I can agree that it should be tagged C++, but should there also be tags for different styles of writing C++? Some other styles might be: using the <algorithms> library and iterators as often as possible, OOP, or template meta-programming. Commented Dec 16, 2017 at 23:59
  • 3
    @DonaldDuck I think Rakete1111 described it well in his answer. It is not C. It is C-style C++, but still C++. Commented Dec 17, 2017 at 0:11
  • 2
    Relevant: Compatibility of C and C++ (Wikipedia) Commented Dec 17, 2017 at 14:37
  • The example question is about C++ references. It's clearly a C++ only question and should have been tagged as such. A C programmer who knew no C++ could not answer it. Commented Jan 31, 2018 at 11:02

3 Answers 3

60

The more fundamental problem is that you are not "code[ing] in c using some c++ elements".

You are coding in C++ in C style. You have C++, not C, and you should only tag your question with . Your thought is correct in that regard:

My spontaneous thought is that if you use even the tiniest bit of C++, then you need to compile it with a C++ compiler.

If you need to compile it with a C++ compiler, then it's not C anymore (because no C compiler would accept the code, if they don't have some extensions).

3
  • 3
    The most common comment I see for such a question is along the lines of "c++ is not c". Update the tags to just [c++], inform OP/TC of such, and move on. Commented Dec 17, 2017 at 3:37
  • this implies that when i write a bash script and use even a single line of python, my tag would be python? or if i used the jni in a java program, i was actually programming c code? Commented Dec 18, 2017 at 9:59
  • 3
    @clockw0rk: no. When you use JNI, you have Java program and some native code, so you need a Java compiler and a compiler for that native language, hence, tagging with two languages would be justified. (in case, the actual problem is affected by both; sometimes, the problem still is only on one side…) In the example of this question, no C compiler is needed. There’s only a C++ program. It’s not using all features of C++, but which C++ program does this anyway… Commented Dec 18, 2017 at 10:03
40

In my experience, the community consensus is roughly this:

The following should be tagged only:

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

The following should be tagged only:

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

The following should be tagged and both:

  • Questions regarding specific differences between C and C++.
  • Questions regarding compatibility or porting code between C and C++.

Questions about code that does not compile in either language (for example combining VLA and iostream), need to be clarified and possibly closed.


Notes:

  • Questions regarding C++ code that uses C libraries (for example using extern "C"), may use both language tags in case it matters for the question, otherwise they should just use .
  • Questions regarding the use of C libraries (such as pthread.h, windows.h etc) should generally be tagged , unless compiled as C++.
  • Questions regarding non-standard C extensions should be tagged (including non-standard gcc -std=gnuXX, GLib etc).
  • Questions regarding non-standard C++ extensions such as should be tagged .
  • Pre-standard C ("K&R") should be tagged .
  • Pre-standard C++ should be tagged .
  • The gcc compiler is a C compiler.
  • The g++ compiler is a C++ compiler.
  • Visual Studio is a C++ compiler.
  • C++ compilers may have an option to compile code as pure C. They can then be used in pure questions, if the C option is set.
  • "Either C or C++ is fine" opinions from the OP is a strong indication of a poor or unclear question. Answers will be very different depending on language picked. Prompt for clarification, perhaps close as unclear/too broad until the OP has clarified this.
  • Similarly, questions from a person who doesn't even know which programming language they are using need clarification. Questions from such people are unlikely to make any sense.
11
  • @Stargateur I would however have hoped for more votes (of any kind) to verify that this is really the community consensus among the c and c++ users. Commented Dec 21, 2017 at 14:15
  • 6
    I don't know if the community consensus is that "Visual Studio is a C++ compiler" (I kid) Commented Dec 21, 2017 at 16:04
  • I don't know if it is the actual consensus, but if it isn't, I would strongly recommend that it become the consensus. Commented Jan 31, 2018 at 11:07
  • I created a tag that you might want to include in your list. It's called c++-cstyle. Commented Mar 6, 2018 at 21:59
  • 3
    "[c] only": "Code that is pure C with no trace of C++, or code that could be either language." -- That doesn't sound right. If code happens to be written in the common subset of C and C++, but it's compiled with a C++ compiler and answers shouldn't be restricted to what C offers, then tagging the question as [c] seems inappropriate to me. And to you too, apparently, since you contradict it later by saying that questions about code compiled with a C++ compiler should be tagged [c++] only. Commented Aug 9, 2018 at 9:42
  • @klutt: "I created a tag that you might want to include in your list. It's called c++-cstyle." The tag you created is the subject of debate on MSO. Commented Aug 17, 2018 at 13:55
  • It is common to write libraries that should compile with a C or C++ compiler. In that case what you are writing is definitely C, but you want to avoid any C-only construct. As per this guide, this would not qualify it for being tagged as both (since it isn't an explicit portability-question), even though any correct solution must respect both language standards. Commented Dec 19, 2023 at 9:03
  • 1
    @julaine How is writing portable code between C and C++ not a portability question? Commented Dec 19, 2023 at 13:52
  • @Lundin My scenario is that somebody asks "How do I do <thing> in C?". It may have several answers, some are C++ compatible and some are not, and I want to know. Commented Dec 20, 2023 at 7:28
  • 1
    @julaine that sounds like it is covered under "Questions regarding compatibility or porting code between C and C++." Commented Dec 20, 2023 at 7:44
  • Or, in other words: With C, just like with sql, you usually have more restrictions (and possibilities) than just the language standard, and discussing common restrictions in a question that doesn't explicitly ask for them is a value-add to the site in my opinion. Here is one of the top C-questions, the discussion is full of C++, but there is no C++-tag on it: stackoverflow.com/questions/605845/… Commented Dec 20, 2023 at 7:48
-3

Maybe my code is C and C++ at the same time. But my intent is to use C++. Therefore, answers like "use templates" can be acceptable.

So it's the intent of the asker is what matters. Even if their code is C, but the poster used the c++ tag, leave it alone.

9
  • 1
    Admittedly I'm not an expert, but how can code be "C and C++ at the same time"? Also, this answer feels like it's motivated by a specific example, have you come across cases of someone incorrectly retagging a C++ question to C? Commented Aug 5, 2024 at 22:21
  • @JohnMontgomery C++ started off as being an extension of C. Strictly speaking, C is not a subset of C++ now. But it's almost a subset. A short C snippet is likely be valid C++ also. Commented Aug 5, 2024 at 22:25
  • 3
    By my understanding of the other answers, if you're using a C++ compiler (which presumably you are if you would accept an answer using C++ features) then the C++ tag is indeed correct regardless of the specific code. I'm just not sure what you're discussing here that wasn't already settled years ago. Commented Aug 5, 2024 at 22:54
  • @JohnMontgomery No. The other answer says "Code that ... could be either language, should be tagged c only". This clearly contradicts what I wrote. Commented Aug 5, 2024 at 23:29
  • 1
    "The following should be tagged c++ only: ... Code that is compiled with a C++ compiler." And again I ask, have you seen any instances of this problem actually happening? Commented Aug 6, 2024 at 18:15
  • @JohnMontgomery Do you really not see how that answer is different from mine? This looks like a reasoning failure on your part. "Code that ... could be either language, should be tagged c only" -- re-read that, and then re-read my answer. "Code that is compiled with a C++ compiler." means "Code that requires a C++ compiler". Irrelevant here. Commented Aug 6, 2024 at 18:58
  • 1
    Since C and C++ are quite different in numerous intricate ways, it's important to pick the correct tag. If the OP knows for sure that they are compiling with a C++ compiler and explicitly says so in the post, the post should be tagged C++ only. If the OP says nothing about the compiler used and post code that may compile in either language, then C is assumed - or alternatively the question needs to be closed as unclear. One reason behind these rules is a very common mistake where the OP doesn't even know which compiler or language they are using and therefore end up with errors. Commented Aug 12, 2024 at 13:15
  • 1
    It's a very common and quite tiresome FAQ that someone compiled their supposed C code with a C++ compiler or vice versa. Commented Aug 12, 2024 at 13:16
  • 1
    (And since we introduce these tag usage rules for C and C++ back in 2017, there seems to be far less drama and conflicts about using both tags on the same post inappropriately, compared to how it used to be.) Commented Aug 12, 2024 at 13:19

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.