C and C++ - The Difference Please?

P

Paul Woodward

I am new to programming in the C / C++ language and I am using a mixture of
online materials plus a small collection of C and C++ books.

What I basically want to know is how do I know if something I am learning is
from the C Language or the C++ Language and should I avoid mixing the two.

I am under the impression that C++ is the successor of C but I would imagine
at the same time there are some functions that have become obsolete under
C++ from the C language and I want to avoid using them.

I am developing on a Linux machine and compiling using gcc and simply using
VIM as a text editor for my source code.
 
A

Alf P. Steinbach

I am new to programming in the C / C++ language and I am using a mixture of
online materials plus a small collection of C and C++ books.

What I basically want to know is how do I know if something I am learning is
from the C Language or the C++ Language

There are some subtle syntactical and semantic differences, but for these
you'll just have to assume that what you see in a C++ book or article is
proper C++. Don't rely on the compiler. Many compilers allow C constructs,
such as (C99) dynamic size arrays, in C++ code.

C doesn't have classes or templates, so all classes and templates are C++.

Regarding library functions: if you can put "std::" in front of the name,
then it's C++. However, the opposite is not necessarily true. For example,
"assert" is implemented as a macro (you cannot put "std::" in front), but
is available in both C and C++.


and should I avoid mixing the two.

Yes, you should. In particular, avoid C memory allocation (malloc and
friends). And wherever practical, use C++ standard library classes such
as std::string and std::vector instead of raw arrays and pointers.

As a novice, avoid C i/o such as printf; use C++ std::cout and friends
instead.

Don't use old-style C headers such as


#include <stddef.h>


but use the C++ wrappers such as


#include <cstddef>



I am under the impression that C++ is the successor of C but I would imagine
at the same time there are some functions that have become obsolete under
C++ from the C language and I want to avoid using them.

Not many, but see above.


I am developing on a Linux machine and compiling using gcc and simply using
VIM as a text editor for my source code.

g++ (gcc) is one of the compilers that by default allows C99 constructs
in C++ code. That doesn't mean it's a bad compiler. On the contrary,
but it's a good idea to keep that in mind, and check if there are options
you can use to restrict the compiler to standard C++.
 
G

Gianni Mariani

Alf said:
[great suggestion snipped]

g++ (gcc) is one of the compilers that by default allows C99 constructs
in C++ code. That doesn't mean it's a bad compiler. On the contrary,
but it's a good idea to keep that in mind, and check if there are options
you can use to restrict the compiler to standard C++.

One additional suggestion is the version of the gcc - get the latest
(3.3) and get gcc-3.4 when it comes out.

The gcc-2.9x C++ compiler is lacking.

G
 
T

Tommy McDaniel

g++ (gcc) is one of the compilers that by default allows C99 constructs
in C++ code. That doesn't mean it's a bad compiler. On the contrary,
but it's a good idea to keep that in mind, and check if there are options
you can use to restrict the compiler to standard C++.

In particular, the -pedantic and probably -ansi options.

Tommy McDaniel
 
G

Govindan Chandran

Paul Woodward said:
I am new to programming in the C / C++ language and I am using a mixture of
online materials plus a small collection of C and C++ books.

What I basically want to know is how do I know if something I am learning is
from the C Language or the C++ Language and should I avoid mixing the two.

I am under the impression that C++ is the successor of C but I would imagine
at the same time there are some functions that have become obsolete under
C++ from the C language and I want to avoid using them.

I am developing on a Linux machine and compiling using gcc and simply using
VIM as a text editor for my source code.

--

Kind Regards,

Paul Woodward

To start off with read the classic programming text for "C" : "The C
Programming Language" by Kernighan and Ritchie.
All "C" programs are C++ programs. Since essentially C++ is a superset of
"C" with object-oriented syntax and semantics added,
mainly objects and classes. ( C uses structs).

Then for a one of the best tutorial reference , refer to ( borowed from a
library ) or get yourself a copy of "The C++ Programming Language"
by Bjarne Stroustrup( the inventor of C++). It is a bit hard to read at
first, so you may refer to a less terse C++ tutorial
book , to help break you into the Strousop book. Depending on your
background, choose for yourself a beginner
C++ tutorial book(suitable for your understanding level), because I dont
know your programming history or coding experience.
Both books mentioned above are quite easy to find both have white covers


Use pico , advanced editor or xemacs as programming editor for editing
source code ( as your confidence grows.) vim or vi
is a bit hard for a Unix or Linux beginner.

Regards,
Gavin
 
A

Alf P. Steinbach

All "C" programs are C++ programs.

Otherwise good advice, but the above is incorrect.

It's not difficult to write code that compiles as both C and C++,
which means writing in the common subset of the two languages.

But C is not a proper subset of C++.
 
S

Steinar

But C is not a proper subset of C++.

That's especially true after C99. The following things are some of the
features *not* supported in C++ but are in the new C standard
(according to Steven Prata, C primer plus, 4th ed.)

* Restricted pointers,
* Variable-length arrays
* Flexible array members
* Macros with a variable number of arguments

and more
 
A

Anthony Albert Nassar

Stroustrup had some very informative articles on this subject in the C++
User's Journal (http://www.cuj.com/); they should still be available online,
so I won't summarize them here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top