Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C and C++ - The Difference Please?

Reply
Thread Tools

C and C++ - The Difference Please?

 
 
Paul Woodward
Guest
Posts: n/a
 
      07-19-2003
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


 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-19-2003
On Sat, 19 Jul 2003 14:34:17 +0100, "Paul Woodward" <> wrote:

>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++.

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      07-19-2003
Alf P. Steinbach wrote:
> On Sat, 19 Jul 2003 14:34:17 +0100, "Paul Woodward" <> wrote:
>

[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

 
Reply With Quote
 
Tommy McDaniel
Guest
Posts: n/a
 
      07-20-2003
(Alf P. Steinbach) wrote in message news:<>...
> 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
 
Reply With Quote
 
Govindan Chandran
Guest
Posts: n/a
 
      07-20-2003

"Paul Woodward" <> wrote in message
news:3f1948d9$0$18486$ ...
> 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


 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      07-20-2003
On Sun, 20 Jul 2003 21:54:53 +0800, "Govindan Chandran" <> wrote:

>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++.

 
Reply With Quote
 
Steinar
Guest
Posts: n/a
 
      07-20-2003
> 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
 
Reply With Quote
 
Anthony Albert Nassar
Guest
Posts: n/a
 
      07-22-2003
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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the similarity and difference b/w EBJ and COM+? =?iso-8859-1?B?bW9vcJk=?= Java 1 05-30-2006 12:12 PM
Difference between bin and obj directories and difference between project references and dll references jakk ASP .Net 4 03-22-2005 09:23 PM
EnableSessionState=readonly and difference between InProc and StateServer mode Jeffry van de Vuurst ASP .Net 0 05-18-2004 10:18 AM
2 Pics Scanned @ width 1024 pixels, and DPI 72 and 300. What Is the Photographic Quality Difference? Ubiquitous Digital Photography 8 12-24-2003 05:02 PM
Exact difference between 'const char *' and 'char *', also diff between 'const' and 'static' Santa C Programming 1 07-17-2003 02:10 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57