8

I'm testing some snippets I found off the web using g++ from MinGW. This is the C++ compiler...why then does it correctly compile C....why do people intertwine C and C++.

The concrete question is: Is it O.K. to use both C and C++ and compile under g++. If the answer is yes, this makes my life easy as I do not have to modify the code.

Oddly enough...to get some C++ to work, particularly when passing a string to an ifstream constructor it requires a C type string...

My guess would be that because C++ depends upon C constructs at times is is O.K to write the two languages together.

However as a matter of style you should settle on cout/cin or printf/scanf.

  • C is essentially a subset of C++. By the time C++ came out there were tones of code in C (still are) so you couldn't just expect to make the two languages incompatible. – FailedDev Oct 15 '11 at 19:34
  • 1
    Note: as of C++11, you can also construct a `std::ifstream` from a `const std::string&` (e.g. see section 27.9.1.7 of the [last draft](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf)). – reima Oct 15 '11 at 19:42

4 Answers4

5

There are a few oddities where char* is needed. You can bridge the gap by using the .c_str() method of a std::string to get one.

For the most part, the C subset of C++ is compatible. Exactly how it isn't compatible is not likely to matter for the most part:

http://en.wikipedia.org/wiki/Compatibility_of_C_and_C%2B%2B

If you're compiling snippets of C code under a C++ compiler, be sure to change it to use the "c" lib format in your includes...for example #include <cstdio> instead of #include <stdio.h>

Is it bad practice to use a C header instead of its C++ equivalent in C++ (e.g. stdio.h instead of cstdio)?

For a fairly reasoned argument from Bjarne himself on why to avoid scanf, check out the beginning of this paper:

http://www.stroustrup.com/new_learning.pdf

There are a lot of benefits to using iostreams instead of printf as well:

'printf' vs. 'cout' in C++

mariohuq
  • 163
  • 1
  • 1
  • 10
  • Well his argument about printf and scanf seems completely focused on scanf. Using printf vs. cout is very much a matter of opinion, as both of them have advantages and disadvantages. Formatting stuff with streams is way more complex than necessary and the OOP aspect of streams can hide bugs (eg `cout << this` vs. `cout << *this`). – Voo Oct 15 '11 at 19:53
  • Updated with a supporting link for why-not-use-printf. It doesn't matter *as much*, true. But I still think the very concept of using an unchecked mini formatting language in a string is antithetical to the C++ spirit. It's sort of analogous to cases I've seen where someone made an "XML format" which was a little bit of header and big blobs of CDATA. At that point you're in a .XML *file* but your data isn't really "XML". Same if you're "programming in printf language" and bypassing the C++ type system and not letting everything get checked at compile time. – HostileFork says dont trust SE Oct 15 '11 at 20:09
  • I'm not so sure if type-safety is that much of an argument considering that streams accept pretty much anything, so you can still get unwanted results easily. Also gcc makes non-matching argument lists less of a problem (it still is, especially on other compilers). Being able to inherit i/ostream is nice in some situations no doubt. Some people prefer one syntax over the other, so that's not an advantage for one or the other. The one thing I abhor about streams in C++ is their formatting abilities. Just try to do something like `%.*s` efficiently with streams. – Voo Oct 15 '11 at 21:03
  • I very much *like* using a system that has its invariants set up so that if I have some debug code like `cout << "The value of x is " << x << endl;` then it is robust with my changing the point of declaration of `x` from integer, to string, to any custom class that has iostream methods. That's good, not bad. Printf requires me to remember to go in and touch the string with any type change I might make, and the narrow formatting language can't be made extensible. For these reasons, it grates on my aesthetics at a foundational level far more than the iostream format modifiers do. [shrug] – HostileFork says dont trust SE Oct 15 '11 at 21:26
  • This question might interest you, for what seems to be one of your specific complaints: http://stackoverflow.com/questions/6094309/disabling-pointer-output-in-c-streams – HostileFork says dont trust SE Oct 15 '11 at 21:28
  • Good to know, that remedies at least that problem, nice. Any performant, simple method for implementing `%.*s` with streams? (asking can't harm ;) ) That would be the second large problem, as I personally implement a `toString()` or similar method internally (it's useful not just for iostreams after all to get a string representation of a class), so I don't have lots of problems with changing types - also gcc catches errors there. My biggest gripe with printf is that the standard forgot several important specifiers (try to print a ptrdiff_t value correctly..) – Voo Oct 15 '11 at 22:37
  • StackOverflow thinks we're talking too much in the comments. Here, new question: http://stackoverflow.com/questions/7781746/efficient-way-to-implement-a-bounded-string-formatting-operator-for-use-with – HostileFork says dont trust SE Oct 16 '11 at 01:21
3

The C++ language inherits much of its core functionality from C. That's because C++ was derived from C. The C++ Standard includes, by reference much of the C Standard. Therefore you can use the C++ compiler to write code using C constructs, idioms and paradigms. Doing so is often referred to as using C++ "as a better C."

The long and the short of the above is yes, you can use printf in C++ code. Doing so is explicitly allowed by the Standard.

Doing this however will often neglect many of the features that define C++. I'll leave that conversation for another question but suffice it to say that many people will tell you simply "don't do that" or "that's not C++." This sets aside the reasons why you might not want to use printf in a C++ program or indeed why you would want to. But rest assured that it is technically allowed.

sth
  • 222,467
  • 53
  • 283
  • 367
John Dibling
  • 99,718
  • 31
  • 186
  • 324
2

Is it O.K. to use both C and C++ and compile under g++.

Yes, it is fine to mix the two languages. This is common with code that started out as C, but then got more and more C++ features added (obviously somebody changed the compiler along the way).

Generally, C code will compile and run with a C++ compiler. There are many possible exceptions, such as use of keywords like class and virtual for names of things in C code, or C's relaxed casting rules.

You will often hear people say "they are very different languages". That's because any programming question you ask probably has a different answer depending on which language you're trying to use. However, there are lots of similarities and backwards compatibility aspects as well.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
0

If you use C++, then use C++. (cin,cout)
Why fstream takes c string puzzles me too.

Daniel
  • 30,896
  • 18
  • 85
  • 139