Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is C++ compatible with C? [closed]

Tags:

c++

c

After reading an amount of C++ articles and tutorials, I often see the phrase "C++ is not C!" or some variant. If this is true, why is C++ trying to be compatible with C? Surely this just encourages C programmers to write bad C++ code? I'm only a newb in the field, so feel free to bombard me with reasons why I'm wrong in thinking that C++ should just make it's mind up and forget C. Ell.

like image 520
Ell Avatar asked Mar 05 '11 22:03

Ell


People also ask

Why is C++ compatible with C?

C++ started as "C with classes", and it was just a precompiler that transformed the class & co. syntactic sugar into C code (C was chosen because it was quite widespread, C compiler were available for many platforms); this was one of the reasons it was (is) C-compatible.

Why C is close to machine?

If you compare C with java , C is closer to the hardware, because java does not directly operate on the system. Java is executed on the java virtual machine, which then operates on the system. C does not have such a layer between it and the system and is therefore closer to the harware.

Is C fully compatible with C++?

If the C++ compiler provides its own versions of the C headers, the versions of those headers used by the C compiler must be compatible. Oracle Developer Studio C and C++ compilers use compatible headers, and use the same C runtime library. They are fully compatible.

Is C close to machine language?

C is almost a portable assembly language. It is as close to the machine as possible while it is almost universally available for existing processor architectures. There is at least one C compiler for almost every existent architecture.


1 Answers

The purpose of compatibility with C is so that C++ programs can have convenient access to the billions (trillions?) of lines of existing C code in the world. It was never intended as a suggestion to code C++ as if it was C.

Peripheral objectives were:

  1. Leverage the C skills that many programmers have (given that it is still one of the most widely used languages in the world).
  2. Encourage the use of C++ as a better C, for: a) easing the transition to C++, and b) improve C coding practices for programmers who have no intention of going to C++.
like image 124
Marcelo Cantos Avatar answered Sep 18 '22 04:09

Marcelo Cantos