Any quick reference for the difference between C and C++

P

PengYu.UT

Hi,

I have been using C++ a lot and I've almost forgot what syntax is
support by C and what syntax is support by C++? My feeling is that C++
gives programmer much flexibility. And the code is more localized in
C++ than C, which mean changes won't propogate far away in C++ code. By
reusing through inherence and overloading, a lot of code can be saved.

But I have to use C for some reason. Is there any programming paradigm
in C, which can give me as much advantage that C++ as possilbe? Is
there any quick reference which can tell me what C++ syntax is
forbidden in C?

Best wishes,
Peng
 
E

Erik de Castro Lopo

Hi,

I have been using C++ a lot and I've almost forgot what syntax is
support by C and what syntax is support by C++? My feeling is that C++
gives programmer much flexibility.

I don't know who said it but I agree with this statement:

C offers you enough rope to hang yourself. C++ offers a
fully equipped firing squad, a last cigarette and a blindfold.
And the code is more localized in
C++ than C, which mean changes won't propogate far away in C++ code.

I think your jugdements on C here are based on the C code
you have worked on and have little to do with C in general.

Erik
--
+-----------------------------------------------------------+
Erik de Castro Lopo (e-mail address removed) (Yes it's valid)
+-----------------------------------------------------------+
"Python addresses true pseudocode's two major failings: that it
isn't standardized, and it isn't executable."
- Grant R. Griffin in comp.dsp
 
Y

yohji

Hmm,both C and C++ are very successful.Choosing which of them depends
on what you want to do.If you like to do some low-level programming
like me,C is a better choice.And if you like OOP,C++ can be a better
choice.Er,Java is also one of the C-family languages,right?
 
M

Malcolm

I have been using C++ a lot and I've almost forgot what syntax is
support by C and what syntax is support by C++?
new, delete, operator, class, public, private, protected, template,
namespace are C++ keywords.
inline is not C89, but is widely supported. Ditto "//" comments.
references eg void cursor(int &x, int &y) are C++ only.

That's pretty much all you need to know, though run code through a C
compiler if you want to use the common subset.
My feeling is that C++ gives programmer much flexibility.
Yes. There are usually more good approaches to a problem in C++ than in C.
And the code is more localized in C++ than C, which mean changes won't
propogate far away in C++ code. By reusing through inherence and
overloading, a lot of code can be saved.
This is very arguable. By declaring a C file with a few public functions and
a lot of static functions you can have something that is actually more
encapsulated than a C++ class.
But I have to use C for some reason. Is there any programming paradigm
in C, which can give me as much advantage that C++ as possilbe? Is
there any quick reference which can tell me what C++ syntax is
forbidden in C?
There is a difference between programming with objects, and object-oriented
programming. C programs using objects, for instance a structure that is
declared in a file, and with functions that operate on it, are common and
work well. It is possible to do object-orientation in C, where the objects
have relationships with each other, so all our "line" objects have a common
"getlength() method. However it normally a mistake because the syntax of
setting up function pointers and nested structures and the like gets so
horrible. That's why C++ was invented

Sometimes people use "object-oriented" as a synonym for "well-designed".
Procedural programs can be just as maintainable and well-structured as
object-oriented ones.
 
P

PengYu.UT

Could you introduce me any information on good C programming style?
Thanks!

Peng
 
G

goose

Could you introduce me any information on good C programming style?
Thanks!

Easy-Peasy:
1. Avoid UB.
2. ???
3. PROFIT!!!

Although a required reading is on the table to get #1 right.
 
K

Keith Thompson

Could you explain these items one by one? Thank!

What items? Don't assume that your readers have access to the article
to which you're replying. The Google Groups interface makes it far
too easy to post followups that do not quote the previous article, but
it is possible (but harder than it should be) to do it properly.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

As it happens, I was able to read the parent article. It was a joke.
If you don't understand the reference, just ignore it.
 
K

Keith Thompson

I don't see your site url. Could you show it to me? Thanks!

I'm sure he'll be happy to do so *if* you post a followup to his
article that quotes it properly. As it is, there's no easy way to
tell what you're talking about, and the person you're addressing
probably won't bother.

To find out how to do this, see my other followup in this thread.
 
P

PengYu.UT

new, delete, operator, class, public, private, protected, template,
namespace are C++ keywords.
inline is not C89, but is widely supported. Ditto "//" comments.
references eg void cursor(int &x, int &y) are C++ only.

That's pretty much all you need to know, though run code through a C
compiler if you want to use the common subset.
Also overloading functions right?
This is very arguable. By declaring a C file with a few public functions and
a lot of static functions you can have something that is actually more
encapsulated than a C++ class.
Would you please give me some more informations? I'm not very familiar
with public and static functions in C. Is there any webpage describe
how to use them to get a good design?
 
M

Malcolm

Would you please give me some more informations? I'm not very familiar
with public and static functions in C. Is there any webpage describe
how to use them to get a good design?
In C++ we would do something like this.

class chess
{
private char board[8][8];
bool tomove;
/*
public function to see if it is checkmate
*/
public bool checkmate(void)
{
int kingcango[8];
int i;
getkingmoves(kingcango);
for(i=0;i<8;i++)
if(kingcango == 1)
break;
if(i == 8)
return true;
else
return false;
}
/* complicated private function that lists allowed moves for the king */
private void getkingsmoves(int *possibles)
{
}
}

In C we do the same thing like this.

/* chess.h */

typedef struct
{
char board[8][8];
int tomove;
} CHESSBOARD;

int checkmate(CHESSBOARD *chessboard);


/* chess. c */

/* this function can be acalled by anyone */
int checkmate(CHESSBOARD *chessboard)
{
int kingcango[8];

getkingsmoves(chessboard, kingcango);
/* etc */
}

/* this function can only be called by other functions in the file chess.c
*/
static void getkingsmoves(CHESSBOARD *chessboard, int *possibles)
{
/* complicated code here to list king's legal moves */
}


What this means is that we can change the function getkingsmoves() as much
as we like, just like a private C++ function, as long as we continue to
support checkmate() somehow or other.
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top