When to use printf / scanf vs cout / cin?

I am testing some fragments that I found on the Internet using g ++ from MinGW. This is a C ++ compiler ... why then it compiles C correctly ... why people are intertwined with C and C ++.

Specific question: is it OK to use C and C ++ and compile under g ++. If so, it makes my life easy, since I do not need to change the code.

Oddly enough ... to make C ++ work, especially when passing a string to the ifstream constructor, a string of type C is required ...

My guess would be because, since C ++ depends on C constructs at times, OK writes two languages ​​together.

However, you should rely on cout / cin or printf / scanf as a style.

+8
c ++ c iostream printf scanf
source share
4 answers

There are a few oddities when char* is required. You can eliminate the space by using the .c_str() method for std::string to get it.

For the most part, a subset of C in C ++ is compatible. Exactly how this is incompatible is unlikely to matter for the most part:

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

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

Is it wrong to use the C-header instead of its C ++ equivalent in C ++ (e.g. stdio.h instead of cstdio)?

For Bjarne’s reasoned argument about why to avoid scanf, see the beginning of this article:

http://www2.research.att.com/~bs/new_learning.pdf

There are many advantages to using iostreams instead of printf:

'printf' vs. 'cout' in c ++

+5
source share

The C ++ language inherits most of its core functionality from C. This is because C ++ was derived from C. The C ++ standard includes mainly a reference to the C-standard. Therefore, you can use the C ++ compiler to write code using C-constructs, idioms, and paradigms. This is often referred to as using C ++ "as the best C".

Long and short of the above yes, you can use printf in C ++ code. The implementation of this is expressly permitted by the Standard.

Doing this, however, often neglects many of the functions that C ++ defines. I will leave this conversation on another matter, but suffice it to say that many people will tell you simply “don't do this” or “it's not C ++”. This eliminates the reasons why you might not want to use printf in a C ++ program or really want to. But rest assured that this is technically permitted.

+3
source share

It is OK to use C and C ++ and compile under g ++.

Yes, it’s great to combine two languages. This is common with code that starts with C, but then more C ++ features are added (obviously, someone changed the compiler along the way).

Typically, C code will compile and run using the C ++ compiler. There are many possible exceptions, such as the use of keywords such as class and virtual for thing names in C code, or in the rules of a softened C cast.

You often hear people say "they are very different languages." This is because any programming question you asked probably has a different answer depending on what language you are trying to use. However, there are many similarities and aspects of backward compatibility.

+2
source share

If you use C ++, use C ++. ( cin cout )
Why fstream also accepts p string puzzles.

0
source share

All Articles