The difference between C and C++

L

Lindsay

I was just reading another post when someone commented that the code was C
and not C++. What are the differences? (Examples?) The answer does not
affect my programming but would help to know if I need to post a question.
 
M

mlimber

Lindsay said:
I was just reading another post when someone commented that the code was C
and not C++. What are the differences? (Examples?) The answer does not
affect my programming but would help to know if I need to post a question.

In short, whereas C supports only the structured programming paradigm,
C++ is a "multi-paradigm" language, which means that it supports
different programming techniques such as structured programming,
object-oriented programming (thanks to inheritance, virtual functions,
strong typing, etc.), and generic programming (thanks to templates).

Almost* any C program can be built unchanged with a C++ compiler, and
the entire C standard library is part of the C++ standard library.
Additionally, C++ has its own standard library, which includes the STL
(standard template library), which consists of generic algorithms and
containers for a variety of uses.


* See http://www.parashift.com/c++-faq-lite/big-picture.html#faq-6.11.
The C++ version of the C standard language/library is based on a
slightly dated version of the C standard, but this doesn't affect their
compatibility all that much as far as most programs are concerned.

Cheers! --M
 
S

Stewart Gordon

Lindsay said:
I was just reading another post when someone commented that the code was C
and not C++. What are the differences? (Examples?) The answer does not
affect my programming but would help to know if I need to post a question.

C was invented by Dennis Ritchie in the 1970s.
C++ was invented by Bjarne Stroustrup in the 1980s.

C++ was created as a successor to C. The _main_ difference is support
for object-oriented programming, but there are a number of feature
changes. Quite some detail is gone into here:

http://en.wikipedia.org/wiki/C++

It's quite common to describe C++ as a superset of C; this isn't exactly
true:

- a C program may use C++-specific reserved words as identifiers
- some obsolete syntaxes are not part of C++, such as the old way of
specifying the types of function parameters
- some features that were added in C99 have not yet made it into a C++
standard.

Of course, a C program can be, and often is, also a valid C++ program.
However, if it doesn't use any of the features specific to C++, then
it's rather meaningless to call it a C++ program. My guess is that what
whoever it was meant is that a snippet of code that somebody posted
doesn't use any C++-specific features, and as such can be considered
plain C, and therefore more appropriate for comp.lang.c than here.
Maybe if you would care to supply an example, somebody can clarify further.

Stewart.

--
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/M d- s:- C++@ a->--- UB@ P+ L E@ W++@ N+++ o K-@ w++@ O? M V? PS-
PE- Y? PGP- t- 5? X? R b DI? D G e++>++++ h-- r-- !y
------END GEEK CODE BLOCK------

My e-mail is valid but not my primary mailbox. Please keep replies on
the 'group where everyone may benefit.
 
L

Lyon

yeah, i think you can past your code, then we can talk more about why
the snippet is C , not really c++
 
J

John Carson

Stewart Gordon said:
Of course, a C program can be, and often is, also a valid C++ program.
However, if it doesn't use any of the features specific to C++, then it's
rather meaningless to call it a C++ program.

It is not meaningless. It is 100% accurate and 100% meaningful. If it
conforms with the C++ standard, then it is a C++ program. That is the simple
fact. Whether or not it is also a valid C program is irrelevant.
 
R

Randy

I see both points, but I'm leaning towards Lyon's side. If it
is purely C, then questions might better be answered in the
comp.lang.c newsgroup. But I also agree with you, John,
that syntactically you can call it a C++ program as it's
compilable with a C++ compiler.

--Randy
 
W

wkaras

Lindsay said:
I was just reading another post when someone commented that the code was C
and not C++. What are the differences? (Examples?) The answer does not
affect my programming but would help to know if I need to post a question.

In my opinion, C is an obsolete language. The only reason to post
anything
to comp.lang.c is to communicate with the many people who don't agree
that
C is an obsolete language.

If you post code that could be improved by using other features of C++,
hopefully someone would point that out to you. I don't see the
relevance
if it so happens that the code you post could compile in straight C.

You might want to look at http://www.gotw.ca/resources/clcm.htm for
the guidelines on posting to comp.lang.c++.moderated to help you
decide what you should or shouldn't post.
 
R

Randy

It will be many, many years (20?) before C is obsolete, if ever. Even
if folks stopped programming in it today, just maintaining the legacy
code would take decades.

Your focus is apparently fairly narrow (x86 platform running Windows?)
since many if not most of the embedded specialty processors (e.g.,
TI's entire line of digital signal processors) use C compilers. There
are still applications out there when an extra MB of memory or
a few hundred MHz of clock speed are very precious resources.

--Randy Yates
 
C

Christopher Hulbert

In my opinion, C is an obsolete language. The only reason to post
anything
to comp.lang.c is to communicate with the many people who don't agree
that
C is an obsolete language.

Good thing it's only your opinion :)!
 
W

wkaras

Randy said:
It will be many, many years (20?) before C is obsolete, if ever. Even
if folks stopped programming in it today, just maintaining the legacy
code would take decades.

Your focus is apparently fairly narrow (x86 platform running Windows?)
since many if not most of the embedded specialty processors (e.g.,
TI's entire line of digital signal processors) use C compilers. There
are still applications out there when an extra MB of memory or
a few hundred MHz of clock speed are very precious resources.

--Randy Yates

I looked up "obsolete" in the dictionary, and it appears it can
mean either "no longer is use" or "out of date". I meant to
say that C is out of date.

There must be a common DNA sequence between the
people at the airlines who think it makes sense to
charge more for a flight to Cleavland than a flight to
Detroit that stops in Cleavland, and the people who
think you have to use C instead of C++ for performance
reasons. The only feature in C++ but not C that
impacts size and/or performance even if you don't
use it is exceptions. So far, I haven't seen a C++
compiler that doesn't have an option to disable the
use of execptions.
 
C

Christopher Hulbert

I looked up "obsolete" in the dictionary, and it appears it can
mean either "no longer is use" or "out of date". I meant to
say that C is out of date.

How do you feel it's "out of date"?
There must be a common DNA sequence between the
people at the airlines who think it makes sense to
charge more for a flight to Cleavland than a flight to
Detroit that stops in Cleavland, and the people who
think you have to use C instead of C++ for performance
reasons. The only feature in C++ but not C that
impacts size and/or performance even if you don't
use it is exceptions. So far, I haven't seen a C++
compiler that doesn't have an option to disable the
use of execptions.

It's Cleveland by the way.
 
M

mlimber

Randy said:
It will be many, many years (20?) before C is obsolete, if ever. Even
if folks stopped programming in it today, just maintaining the legacy
code would take decades.

Your focus is apparently fairly narrow (x86 platform running Windows?)
since many if not most of the embedded specialty processors (e.g.,
TI's entire line of digital signal processors) use C compilers. There
are still applications out there when an extra MB of memory or
a few hundred MHz of clock speed are very precious resources.

Agreed, although I program on TI DSPs with C++.

Cheers! --M
 
W

wkaras

Christopher said:
(e-mail address removed) wrote: ....

How do you feel it's "out of date"?

Because there is a newer language, C++, that does everything
C does as well as C does, plus it does some other useful
things.
 
T

Tim Slattery

Lindsay said:
I was just reading another post when someone commented that the code was C
and not C++. What are the differences? (Examples?) The answer does not
affect my programming but would help to know if I need to post a question.

Without seeing the code, we can't make a very good guess about what
"someone" was reacting to. There are *many* differences between C++
and C, starting with the "class" keyword. C doesn't have classes, C++
makes *very* heavy use of them. C insists that all variables be
declared at the beginning of a scope, C++ lets you define variables at
the point of use. C++ has templates (and therefore the Standard
Template Library (STL)), C does not. C lets you declare a function
without giving details about parameters, C++ does not. (ie: "void
f();" in C declares a function that could take any number of arguments
of any type, in C++ if declares a function that has no parameters.)
The differences go on and on...

--
Tim Slattery
(e-mail address removed)
 
B

BigBrian

Because there is a newer language, C++, that does everything
C does as well as C does, plus it does some other useful
things.

You obviously haven't done any kernel programming. It can't be done in
C++, not on Linux anyway.

-Brian
 
W

wkaras

BigBrian said:
You obviously haven't done any kernel programming. It can't be done in
C++, not on Linux anyway.

-Brian

Since the original topic of this thread was posting guidelines,
I'd like to suggest that it accomplishes little to post
conclusions with no supporting reasons or arguements.
Unless you're able to make the ground rumble beneath the
feet of anyone reading your post.

Why can't C++ be used in kernel code? The issue I'm aware
of is that it takes special tools to use compiled code in
situations where paging has to be done explicitly. I've
heard these tools do not yet support C++ virtual
function calls or execptions. But C++ is still better than
C even if you don't use virtual function calls or exceptions.
if you use
 
D

Default User

Because there is a newer language, C++, that does everything
C does as well as C does, plus it does some other useful
things.

Untrue. C as of the latest standard has features that are not found in
C++.



Brian
 

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

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,196
Latest member
ScottChare

Latest Threads

Top