when to use C and when to use C++

S

sam_cit

Hi everyone,

I have been asked this question quite a few times and i wonder what
could the actual reason?

" When to prefer C over C++ or vice versa, of course this is for real
world pratical scenario"

As per my understanding, C++ makes it easier to program application
related logic as OOPS helps in that sense, but for some reason that is
not a reasonable explanation, could any one add on their comments?
 
M

Martin Ambuhl

Hi everyone,

I have been asked this question quite a few times and i wonder what
could the actual reason?

" When to prefer C over C++ or vice versa, of course this is for real
world pratical scenario"

Usually the reason for the question is that some asshole wants to start
a flame war.
As per my understanding, C++ makes it easier to program application
related logic as OOPS helps in that sense, but for some reason that is
not a reasonable explanation, could any one add on their comments?

Of course it is not a reasonable explanation for the question not an
adequate answer for it. It is at best a prejudice, at worst a lie.
 
J

jacob navia

(e-mail address removed) a écrit :
Hi everyone,

I have been asked this question quite a few times and i wonder what
could the actual reason?

" When to prefer C over C++ or vice versa, of course this is for real
world pratical scenario"

As per my understanding, C++ makes it easier to program application
related logic as OOPS helps in that sense, but for some reason that is
not a reasonable explanation, could any one add on their comments?

The problem of C++ is that it is an extremely complex language. People
that have invested a lot of time in mastering its complexity are
productive with it, people that do not want to bother may use C.

Using C coupled with a garbage collector makes many of the features
of C++ unnecessary since memory management, one of the principal reasons
for constructors/destructors disappears from view.

A problem with C is that the language is severly crippled by the absence
of any high level library for data structures like the STL. This is not
a problem with the language itself but with the people that organize the
discussion and evolution of C. The standards comitee denies any progress
to the language and apparently they have decided that no new standard
will appear in the next 10 years or so, since there will be no C 2009
and the next standard will (maybe) appear in 2019...

This means in practice that lists, arrays and many other commonly used
data structures are absent from the C standard library. It means too
that you have to use some external library, tying your code to it,
what many people will not want to do, so they start endlessly coding
their 1000th linked list package.

This group is part of the problem of C. As you can see from the answer
of Mr Ambuhl above, it is full of people that think insults are the
only answer to people that want to discuss anything more serious than:

<begin quote >
Hi people

I have coded:
i++ = (i++ + i++);

and it doesnt't work... PLEEZE HELP
< end quote>

In general, disucssions concerning the evolution of C are destroyed
systematically by a group of people called "regulars" that have
taken as granted that *their* view of C (frozen about 1989) is the
only view that can be defended in this group.

So, since comp.std.c has the same view, there is effectively no
discussion group, and no discussion of the evolution of C at all.

When to use C?

You can use C anywhere, you can code anything with it, it is a general
purpose language. Its advantages are its simplicity, and its
disadvantages are its simplicity.

jacob
 
I

Ian Collins

jacob said:
Using C coupled with a garbage collector makes many of the features
of C++ unnecessary since memory management, one of the principal reasons
for constructors/destructors disappears from view.
I don't intend to be drawn into yet another pointless GC debate but this
falls short of the truth. Memory is but one of the many resources that
can be managed through objects with constructors and destructors, that's
why the idiom is called "Resource Acquisition Is Initialization".
 
J

jacob navia

Ian Collins a écrit :
I don't intend to be drawn into yet another pointless GC debate but this
falls short of the truth. Memory is but one of the many resources that
can be managed through objects with constructors and destructors, that's
why the idiom is called "Resource Acquisition Is Initialization".

I said "*one of* " the principal reasons.
 
R

Richard Heathfield

Ian Collins said:
I don't intend to be drawn into yet another pointless GC debate but this
falls short of the truth.

The OP asked for comments, and Jacob provided one. The OP did *not* ask only
for accurate comments from knowledgeable people.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
A

Andreas Kochenburger

A problem with C is that the language is severly crippled by the absence
of any high level library for data structures like the STL. This is not
a problem with the language itself but with the people that organize the
discussion and evolution of C. The standards comitee denies any progress
to the language and apparently they have decided that no new standard
will appear in the next 10 years or so, since there will be no C 2009
and the next standard will (maybe) appear in 2019...

Interestingly the development of D was spurred by missing C facilities
and to overcome C++ complexities.



Andreas
 
B

Bill Pursell

Hi everyone,

I have been asked this question quite a few times and i wonder what
could the actual reason?

" When to prefer C over C++ or vice versa, of course this is for real
world pratical scenario"

As per my understanding, C++ makes it easier to program application
related logic as OOPS helps in that sense, but for some reason that is
not a reasonable explanation, could any one add on their comments?

Use C when it's appropriate. If you are writing a program
in C++, you may end up writing a lot of low-level routines
that are perfectly valid C. If at that time you recognize that
you are writing C, it increases the flexibility of your code
if you compile those sections as C and create a C library,
and then use them from C++, since they will also
be useable from C applications.

Some people argue that C++ makes it easier to program,
but this is often because they lack sufficient knowledge
of how to apply object-oriented programming techniques
in their C-code. In my (limited experience), C++ tends to
encourage difficult to maintain, hideous crap that is
just annoying to read.

The STL is an extremely nice feature of C++, but it
is often horribly inefficient. The data structures are
designed to be flexible and generic, and as a result
they are not the perfect match for any programming
situation. When you code in C or C++, one of
the primary reasons for that language choice is
speed...so why kill yourself on the performance side
by using overly generic data structures?

I will freely acknowledge that I have a strong bias
against C++. I think I've met 3 people who write C++
code that doesn't make me vomit. Several C++
coders I know have expressed such amazing
ignorance of C as claiming that the -> operator
is not a part of the C language and an apparent
ignorance of the 'struct' keyword. (Which is to say
that one person actually said 'That's C++, C doesn't
have the arrow operator', and the abundance of
code that fails to use structures makes me believe
these people are just friggin' clueless.)

Most of the 'nice' feature of C++ are syntactic
sugar (introspection, operator overloading, automatic
constructors/destructors). The try/catch clause is
nice, but leads to sloppy programming. Namespaces
are nice, but lead to sloppy build environments. The
STL is nice, but is usually not the best choice.

In summary, use C when it is appropriate,
and use C++ never.
 
I

Ian Collins

Bill said:
The STL is an extremely nice feature of C++, but it
is often horribly inefficient.

Sorry, FUD.
The data structures are
designed to be flexible and generic, and as a result
they are not the perfect match for any programming
situation. When you code in C or C++, one of
the primary reasons for that language choice is
speed...so why kill yourself on the performance side
by using overly generic data structures?
What's overly generic about a vector?
I will freely acknowledge that I have a strong bias
against C++.

Fair enough, but please don't spread misinformation.
 
R

Richard Heathfield

Bill Pursell said:

Use C when it's appropriate.

Better advice would be "use the *most* appropriate tool for the job". The
choice of "most appropriate tool" depends on the job, the tools available,
the various (and often conflicting) requirements of efficiency,
portability, ease of maintenance, development time constraints, and
familiarity. It would (probably) be very unwise to write a C program to
process a business transaction, however easy that might be for you, if the
other 10,000,000 lines of the suite are written in COBOL, because most of
the available maintenance expertise is going to be in COBOL, not C. But if
you're starting a new project and picking a brand new team (or going it
alone), you might well look at Python and Perl and so on for your text
processing application, and then think to yourself, "sheesh, I'd be much
happier writing this in C, simply because I know C so much better, so I'd
find it easier to write, easier to read, easier to debug..."
If you are writing a program
in C++, you may end up writing a lot of low-level routines
that are perfectly valid C. If at that time you recognize that
you are writing C, it increases the flexibility of your code
if you compile those sections as C and create a C library,
and then use them from C++, since they will also
be useable from C applications.
Agreed.

Some people argue that C++ makes it easier to program,

I, on the other hand, would argue that sticking with what you know makes it
easier to program in the short term. That isn't necessarily the best
choice, though.

Just the other day, I found myself about 20 lines into a C program, when I
thought, "hmmm, to do *this*, I'm going to need to do *that*, which means
I'll have to write some routines to do *the other*, and by the time I've
done *that*, I'll have implemented about half the STL - so why not just use
C++ and save myself a lot of time?" So I did.
but this is often because they lack sufficient knowledge
of how to apply object-oriented programming techniques
in their C-code. In my (limited experience), C++ tends to
encourage difficult to maintain, hideous crap that is
just annoying to read.

Then I suggest you spend a little time developing your ability to write
easily maintainable, non-hideous, jewels of C++ that are a delight to read.
It isn't easy! But it is certainly possible.
The STL is an extremely nice feature of C++, but it
is often horribly inefficient.

I agree, but often it doesn't matter, because "horribly inefficient" is a
relative term. When I use the STL, I am sacrificing a certain amount of
performance in favour of ease of writing, ease of reading, and ease of
maintenance. If I can save myself 40 hours of development time and end up
with a program which runs in 10 seconds using STL, when my own hand-written
C routines might be able to perform the same task in 8 seconds, then I'm
likely to jump at the chance *unless* those extra 2 seconds become a
significant part of the equation. Let's simplify matters, and assume I'm
the only person who will ever run the program. If so, then my payoff for
using C comes only when I run the program for the 72001st time(!). If, on
the other hand, I have many users lined up for the program, then the
equation changes. Even then, it might be worth writing the C++ version
simply to get the users up and running, and then re-writing it in C if and
only if I start to get a lot of complaints about performance.
The data structures are
designed to be flexible and generic, and as a result
they are not the perfect match for any programming
situation. When you code in C or C++, one of
the primary reasons for that language choice is
speed...so why kill yourself on the performance side
by using overly generic data structures?

Because "killing yourself" isn't the only choice. C++'s "overly generic"
data structures are nevertheless no slouches at performance. Yes, maybe you
can beat them with customised routines, but those customised routines take
time to write and get right. If the payoff is worth it, by all means do so,
but don't just *assume* that the payoff will be worth it.
I will freely acknowledge that I have a strong bias
against C++.

So do I. It's only my *second* favourite language.
I think I've met 3 people who write C++
code that doesn't make me vomit.

I suggest that you consult a doctor. :)
Several C++
coders I know have expressed such amazing
ignorance of C as [...]

Well, we can't blame the language for its users!

In summary, use C when it is appropriate,
and use C++ never.

....except when it is appropriate. Or, better still, use the best tool for
the job.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
J

jacob navia

Martin Ambuhl a écrit :
Usually the reason for the question is that some asshole wants to start
a flame war.

Translation:
"I want to kill any discussion that goes beyond homework level".

The easiest way is to insult anyone that posts an interesting question
that *could* lead to an interesting discussion.

comp.lang.c is just for questions like this

Hi people

I wrote
i++ = i++ + i++
and it doesn't work...

PLEEZE HELP
 
J

jacob navia

Richard Heathfield a écrit :
Ian Collins said:




The OP asked for comments, and Jacob provided one. The OP did *not* ask only
for accurate comments from knowledgeable people.

No, he did not.

And you are sure the appointed expert in what I know
and what I do not know.
 
S

stdazi

Hi everyone,

I have been asked this question quite a few times and i wonder what
could the actual reason?

" When to prefer C over C++ or vice versa, of course this is for real
world pratical scenario"

I tend to use C++ at work, where development speed is crucial (ie. you
don't really care about putting string terminations, you don't want to
care much about allocations of string/other objects... etc.. - sad but
true). On the other hand, when I code in my spare time, I code for
fun,so I like to think about each and every detail (as it's fun :) I
enjoy it so I don't mind to spend extra time placing null terminations,
etc...

Just my case...
 
M

mdh

Richard said:
Bill Pursell said:



Better advice would be "use the *most* appropriate tool for the job".


Richard,
[Perhaps a little OT]

Thanks for that. I have often wondered about this myself. I started
with C as it seemed to be the underlying language in "Cocoa", (OS X).
(Problem is, that I have really begun to like C!! Besides, I am not
sure a list as active and enjoyable as this one, exists elsewhere?) My
next thought is do I need to then learn C++? It this question does not
make too much sense, it is because I do not yet clearly see all the
intricacies of the different programming languages.
 
R

Richard Heathfield

mdh said:

(Problem is, that I have really begun to like C!!

That is not a problem, believe me. C is a very likeable language. (It is
also a very hateable language, it seems.)
My
next thought is do I need to then learn C++?

Only you can answer that. (Do all programmers need to learn C++? No. Do some
programmers need to learn C++? Yes.)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
J

james of tucson

mdh said:
Thanks for that. I have often wondered about this myself. I started
with C as it seemed to be the underlying language in "Cocoa", (OS X).

I would have expected "Objective C."
 
M

mdh

I would have expected "Objective C."

James...not quite sure I understand the difference between the 2. I
know it is OT...but care to tell in a short sentence, or maybe
privately by e-mail?
 
S

Skarmander

Ian said:
I don't intend to be drawn into yet another pointless GC debate but this
falls short of the truth. Memory is but one of the many resources that
can be managed through objects with constructors and destructors, that's
why the idiom is called "Resource Acquisition Is Initialization".
Note that this idiom is fairly specific to C++, with its deterministic
finalization and peculiar exception handling mechanism (supporting either a
"catch" or a "finally" block, but not both). Garbage-collected OO languages
typically offer nondeterministic finalization, and the "destructor" (or
whatever it's called) is a last, untimely resort to freeing resources, which
otherwise have to be managed explicitly.

C with a garbage collector and exception handling would come a lot closer to
matching C++ (for those who insist). Of course, if it's OO you're after
you're sort of going to need classes... and then you'll want generics too
(though not necessarily in the form of C++'s templates). Before you know it,
you'll have C# or Java.

Which, I guess, just goes to show you that different languages have
different uses.

S.
 
J

james of tucson

Skarmander said:
C with a garbage collector and exception handling would come a lot
closer to matching C++ (for those who insist).

Objective-C's manual reference counting is one compromise -- the
language is much closer to C, and adds object allocation constructs
without going as far as literally replacing the language.

I thought Objective-C was an obscurity until I started programing on
MacOSX, and discovered that it was easy for a C programmer to work with
and quite useful.
 
C

Clark S. Cox III

mdh said:
James...not quite sure I understand the difference between the 2. I
know it is OT...but care to tell in a short sentence, or maybe
privately by e-mail?

Your learning C will not be a waste if and when you want to use
Objective-C, but you must be aware that they are not the same thing.

[OT]
Objective-C is essentially C, plus some SmallTalk-style objects. It is
much smaller and more dynamic than C++, but also requires more runtime
support. In addition, it is a proper superset of C (i.e. any valid C
program is also a valid Objective-C program); any additional keywords
either begin with the '@' character, or are used in a such a limited
context that they do not conflict with their C meanings (or lack thereof)

See:
http://en.wikipedia.org/wiki/Objective-C#Philosophical_differences_between_Objective-C_and_C.2B.2B

[/OT]
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top