Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   C and C++ - The Difference Please? (http://www.velocityreviews.com/forums/t275820-c-and-c-the-difference-please.html)

Paul Woodward 07-19-2003 01:34 PM

C and C++ - The Difference Please?
 
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



Alf P. Steinbach 07-19-2003 02:24 PM

Re: C and C++ - The Difference Please?
 
On Sat, 19 Jul 2003 14:34:17 +0100, "Paul Woodward" <noreply@newsgroups.com> 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++.


Gianni Mariani 07-19-2003 04:47 PM

Re: C and C++ - The Difference Please?
 
Alf P. Steinbach wrote:
> On Sat, 19 Jul 2003 14:34:17 +0100, "Paul Woodward" <noreply@newsgroups.com> 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


Tommy McDaniel 07-20-2003 06:15 AM

Re: C and C++ - The Difference Please?
 
alfps@start.no (Alf P. Steinbach) wrote in message news:<3f19526d.183864656@News.CIS.DFN.DE>...
> 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

Govindan Chandran 07-20-2003 01:54 PM

Re: C and C++ - The Difference Please?
 

"Paul Woodward" <noreply@newsgroups.com> wrote in message
news:3f1948d9$0$18486$cc9e4d1f@news.dial.pipex.com ...
> 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



Alf P. Steinbach 07-20-2003 02:19 PM

Re: C and C++ - The Difference Please?
 
On Sun, 20 Jul 2003 21:54:53 +0800, "Govindan Chandran" <govin1@pacific.net.sg> 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++.


Steinar 07-20-2003 07:11 PM

Re: C and C++ - The Difference Please?
 
> 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

Anthony Albert Nassar 07-22-2003 11:00 PM

Re: C and C++ - The Difference Please?
 
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.




All times are GMT. The time now is 07:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.