40

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/…
    – Suraj Rao
    Dec 15, 2017 at 12:44
  • 7
    I am inclined to agree with this.. stick to one language at a time
    – Suraj Rao
    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.
    – user0042
    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.
    – user0042
    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
    – Suraj Rao
    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++
    – Justin
    Dec 16, 2017 at 3:29
  • 6
    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. 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.
    – Fsmv
    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++.
    – klutt
    Dec 17, 2017 at 0:11
  • 2
    Relevant: Compatibility of C and C++ (Wikipedia) 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.
    – JeremyP
    Jan 31, 2018 at 11:02

2 Answers 2

55

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.
    – Cloud
    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?
    – clockw0rk
    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…
    – Holger
    Dec 18, 2017 at 10:03
36

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.
6
  • @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.
    – Lundin
    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) 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.
    – JeremyP
    Jan 31, 2018 at 11:07
  • I created a tag that you might want to include in your list. It's called c++-cstyle.
    – klutt
    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.
    – user743382
    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. Aug 17, 2018 at 13:55

You must log in to answer this question.

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