Archived

This topic is now archived and is closed to further replies.

RogueZero

C vs. C++

Recommended Posts

Ok i was talking to one of my friends about c and c++ and he said that i should learn c first in order to understand c++ better. I think he is wrong. He also said that C++ is the best programming language out now. Is he right on both these topics and whats the difference between c and c++?

Share this post


Link to post
Share on other sites
While many will say that there are different languages for different tasks, I will agree with him, C++ is the best language out right now. I am not going to argue with anyone about this, so end it here.

Very few people actually learn C before they learn C++. In C, structures are handled in a funky way with typedef and such, variables can only be declared at the top of functions, and there are really only two types of variables, global and local.

What I find most people learn is actually non-object oriented C++. This is in my opinion the best route. Once you have all of the basics down like variables, functions, operators, and pointers, then start exploring classes and higher level approaches.

Share this post


Link to post
Share on other sites
My opinions:

> he said that i should learn c first in order to understand c++ better

He''s wrong. By learning C first you''ll only learn bad habits that make you write worse C++.

> He also said that C++ is the best programming language out now.

He''s wrong. There are several good languages, all with their own strong points. C++ is not "the best".

Share this post


Link to post
Share on other sites
By learning C first, you have the potential to learn many bad habits that you would have to unlearn if/when you move to C++. Though by learning C first you do get a better appreciation why most people will use C++ instead.

Now as far as C++ being the best language, that is pure opinion. My opinion is it absolutely and completely depends on what you are doing. For some things I''d go with Perl, others Java, and some things C++.

--
Aethon

Share this post


Link to post
Share on other sites
quote:
Original post by 31337
While many will say that there are different languages for different tasks, I will agree with him, C++ is the best language out right now. I am not going to argue with anyone about this, so end it here.

Very few people actually learn C before they learn C++. In C, structures are handled in a funky way with typedef and such, variables can only be declared at the top of functions, and there are really only two types of variables, global and local.

What I find most people learn is actually non-object oriented C++. This is in my opinion the best route. Once you have all of the basics down like variables, functions, operators, and pointers, then start exploring classes and higher level approaches.


Very few? There are TONS of schools that teach C before C++, and a lot of people that go to these schools learn it in this order. I for one learned C before C++, and I know when to use the featuers of C++ and when not to, because just because they make development time faster in some cases, doesn''t mean they make the game run faster . There isn''t really a big downside to learning C, because C++ is pretty much backward compatible.

Share this post


Link to post
Share on other sites
quote:
Original post by civguy
My opinions:

> he said that i should learn c first in order to understand c++ better

He''s wrong. By learning C first you''ll only learn bad habits that make you write worse C++.

> He also said that C++ is the best programming language out now.

He''s wrong. There are several good languages, all with their own strong points. C++ is not "the best".


If only this were 100% true... you may just pick up GOOD habbits along the way too though. I personally learned C before C++, and I think it helped more than it hurt. Sure I have a few habbits left over from C, but not all of them are bad. Just because it''s a feature in C++, doesn''t make it the best choice... some times, plain old C runs much faster than something written using the "advanced" features of C++. I said this above, and I''ll say it again.. just because development time gets faster, doesn''t mean your game will run any faster, and if you never learn C, chances are, you''ll miss out on a few things that may have helped.

Share this post


Link to post
Share on other sites
quote:
Original post by aethon
By learning C first, you have the potential to learn many bad habits that you would have to unlearn if/when you move to C++. Though by learning C first you do get a better appreciation why most people will use C++ instead.



Everyone trying to bring up this bad habbit thing.... name a few bad habbits that you had to "unlearn" in orer to move to C++... void pointers? Organizing data in structures instead of classes? Using functions rather than class member functions? Having to initialize all your data rather than have a constructor do it (I could see how this could screw you up if you went from C++ -> C, but not from C -> C++)? How many of you guys saying you''ll pick up bad habbits learned C first, and have more bad than good habbits? I did learn C first, and I did migrate over to C++. I don''t use ALL the features of C++, but I do know about most of them if I ever find a need for them.

Share this post


Link to post
Share on other sites
quote:
Original post by Ready4Dis
Everyone trying to bring up this bad habbit thing.... name a few bad habbits that you had to "unlearn" in orer to move to C++...


Using true object oriented design instead of "Hey I''m using classes, thus OOP!"

Since C does not directly support OOP when switching to C++ that does, you potentially have to unlearn the habits which would interfere with OOP.

--
Aethon

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by aethon
quote:
Original post by Ready4Dis
Everyone trying to bring up this bad habbit thing.... name a few bad habbits that you had to "unlearn" in orer to move to C++...


Using true object oriented design instead of "Hey I''m using classes, thus OOP!"

Since C does not directly support OOP when switching to C++ that does, you potentially have to unlearn the habits which would interfere with OOP.

--
Aethon


Saying you have to unlearn somethings just because you''re using a different language is like saying you can only learn 1 language and if you switch you have to unlearn it and if you want to program in it again you have to relearn it. C and C++ are different languages.

Share this post


Link to post
Share on other sites
the bad habit thing is total BS ... because a learning experience is just that .. if you have the right attitude, all knowledge is power ... even assembly langauge ... even C# and .NET ...

BUT - there is not need whatsoever to learn C before C++

... I know, because I learned GWBASIC, then Pascal, then C, FORTRAN and ASM at colledge, then C++, HTML, then PERL, XML, Python, and Ruby, a little Java, PHP, and C#.

All I have to say is, each language opens the mind to viewing the world its way ... and the C way is no better or worse than C++ ... although I think C++ is the absolute best general purpose language in the world (but I love other langauges for special areas) ... C is beautiful only in it''s simplicity, but much worse for learning that C++ if only for the fact that you cannot use the STL while learning C, and it uses an archaic FORTRAN like input / output system (which is REALLY powerfull at the right times, but causes most of my C programming co-workers to not really GET C++''s iostream philosophy - and that''s the ONLY bad habit I know of that C programmers have ... not understanding iostreams).

Share this post


Link to post
Share on other sites
Ignoring the "bad habits" argument, and going back to the original poster''s question...

Personally, I learned C before I learned C++, but only barely--one term, to be exact. Of course, I already knew BASIC, so I can''t really judge C as a "first language". That said, I would recommend learning non-object oriented C++ (which many schools do these days) first, as was already suggested. Pointers, for one thing, can be confusing enough for beginners; there''s no need to overly complicate things by adding in static class members, private/public/protected, etc. Of course, once you''ve got a solid grasp on the basics, the object oriented functionality is fairly easy to grasp.

Also, C does have some strong points over C++, though I can''t think of many. String formatting is much easier (sprintf, in all its ugly glory, can do things much more cleanly and compactly than a strstream...), but that''s one of the few highpoints I can think of.

Finally, as to which is the "best language"--it really depends on what you''re doing. For general programming, I use C/C++ (mainly C++, with a few things like sprintf thrown in); for quick-and-dirty, I like perl (especially its string formatting and parsing abilities!); and for other circumstances, there are languages such as Prolog, Python, Lisp...all of which are very powerful when used appropriately. I''m not sure that InterCal has much use, though...

-Odd the Hermit

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

All you who are saying if you learn C it''s just bad habits you will have to ''unlearn'' are talking nonsense.

It is possible to write good, clean, readable C code. Is that a ''bad habit''?

As for all the rubbish about OO you have to realise OO is a way of THINKING, you don''t need C++ to write OO code. You can code in an Object Orientated style in most languages. Sure, some of the more ''advanced'' features may be difficult to implement but if it''s someone just starting to learn then they should steer away from anything that''s too far ahead of themselves.

As proof of the above my degree course taught us Object Orientated Analysis which was design only, no code involved at all. At the same time they were teaching us C but told us to use .cpp files. Once everyone had got the hang of actually writing code then they moved on to writing classes.

If you need to know what a variable is, how they work and how loops work then C is your best bet. Mainly because you can set yourself up with a main() and start learning those things and not have to worry about anything else. Once comfortable then move onto classes.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
When I was learning C++ I learned by reading C++ tutorials, but didn''t learn classes and templates at start. That way I didn''t have to unlearn stuff from C. And it worked great for me that way.

Now I just hope that the next C langugage will throw out some of the "uncleanity" in how C++ handles classes and templates...

Share this post


Link to post
Share on other sites
quote:
Original post by civguy
My opinions:

> he said that i should learn c first in order to understand c++ better

He''s wrong. By learning C first you''ll only learn bad habits that make you write worse C++.

> He also said that C++ is the best programming language out now.

He''s wrong. There are several good languages, all with their own strong points. C++ is not "the best".


C is not some drug-like bad habit that you''ll get addicted to and can''t shake. Programmers are smart enough to know the good vs. bad habits, so don''t be afraid of C as if it will warp your brain or something. For me it was best to go to C and then C++ because that let me transition from procedural to object-oriented programming smoothly, and I didn''t pick up any bad habits on the way. You could just as easily use C++ all the way starting with procedural style and then moving to object-oriented, since C++ will let you write simple procedural code too.

Value of good ideas: 10 cents per dozen.
Implementation of the good ideas: Priceless.

Proxima Rebellion - A 3D action sim with a hint of strategy

Share this post


Link to post
Share on other sites
C++ does not cover everything C can do, even if it is a superset. Being able to fall back on C functions can be invaluable at times when you wish to minimize dependencies on libraries.

Share this post


Link to post
Share on other sites
quote:
Original post by RogueZero
Ok i was talking to one of my friends about c and c++ and he said that i should learn c first in order to understand c++ better.



I would recommend learning Java before learning C++. In many respects, it''s simpler than C++, which will probably make it easier to learn than C++. Also, since object variables behave like pointers in C++ you won''t have the common problem of trying to figure out what a pointer is and why its useful. This is actually the path I took (and I had trouble seeing the usefulness of non-pointers).

quote:

I think he is wrong. He also said that C++ is the best programming language out now.



Best at what? To me, C++ does many things, but doesn''t do any of them well. Another (more C++ friendly) way to say it is that C++ is a great compromise between many things.

quote:

Is he right on both these topics and whats the difference between c and c++?



The biggest difference is the mindset you use when using them. I''d recommend comparing wxWindows (maybe QT, I don''t have much experience with it) with either the win32 API or GTK+. All three are (in my opinion) fine GUI API''s, but wxWindows (and QT) are done the C++ way while win32 and GTK+ are done the C way.

Some other (minor) differences:
- C doesn''t have OO machinery (classes, inheritance, etc.) (you''d use callbacks), function overloading (syntactic sugar), operator overloading (syntactic sugar), exceptions, or templates (you''d use void pointers)
- C++ doesn''t have VLA''s (you''d use a vector/list/deque), designated initializers, or compound literals
- inline is static by default in C++ while it''s extern by default in C

Share this post


Link to post
Share on other sites
I learned C++ first, through the Half-Life SDK more or less =P

But I significantly prefer printf and scanf to cout and cin, so I continue to use those.

Mix and match is fun =)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Another reason to use C when starting out is that there actually is a standard. Read a C book, write the code and it will work.

C++ can have problems when changing from one compiler to another. In msdev 6.0 cpp you can''t...

if (anything)
{
int a;

// do stuff
}

if (somethingElse)
{
int a;

// do more stuff
}

but you can in .net 7.0.

At our work we use Havok (a C++ physics engine) and Renderware (a C rendering engine). We were forced to move to .NET for the XBox version. Renderware was no problem at all but Havok just wouldn''t compile on .NET due to the differences in the C++ compiler. We got in touch and they told us they''re currently rewriting stuff to make sure it will compile on .NET.

You can keep C++, if you want compatibility use C.




Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Another reason to use C when starting out is that there actually is a standard. Read a C book, write the code and it will work.


There is an ANSI/ISO C++ standard too. The problem is no compiler fully implements all of it since the standard is so complex. GCC support is very good, and VS.NET 2003 is suppossed to be very good too, though I've never used VS.NET 2003. There are multiple C standards too, they are just better supported by compilers. Take for instance K&R C vs ANSI C. ANSI C is far more prevalant, but the two are not 100% compatible. (And if you do learn C, stick to ANSI C, K&R C is a rather old dialect).

Edit: Typos

[edited by - Aethon on July 28, 2003 1:54:24 AM]

Share this post


Link to post
Share on other sites
yes aethon, but he''s correct in saying ... right NOW, ANSI C compilers are 100% compatible to the standard ... and ANSI C++ compilers are NOT ...

although with GCC 3.x, BCC 5.5 (and BCB 4 or higher), Visual C++ .NET 2003, they are close enough not to matter ... Visual C++ was the LAST common compiler to avoid supporting the simple things like for loop scoping correctly ... and things like "partial template specialization" support, just don''t affect the beginner.

BTW .. I love C++, and think it''s the best langauge in the world ... but I use ruby, perl, C#, PHP, also ...

and I used to use C, and TOTALLY 100% agree that it''s a fine programming langauge ... in fact, it has NO MAJOR FLAWS at what it is ... which is actually not true about Java, or C++, or any of the larger langauges which have made many more compromises ...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

aethon: and that''s my point. If it''s for someone starting out then staring at the book going "I''m damn sure I typed this in correctly yet it''s saying there''s an error" isn''t really going to help.

But then again it would be a useful learning exercise to fix it

Share this post


Link to post
Share on other sites
quote:
Original post by Ready4Dis
How many of you guys saying you'll pick up bad habbits learned C first, and have more bad than good habbits?
Of course you'll get more good than bad habbits. What matters is that the bad habbits exist. During the transition phase from C to C++, people usually write neither good C (or actually, it's not C at all) nor good C++. They still klung on to #defines instead of constants, use callbacks when C++ would give better alternatives, fail to use iostream when they feel "printf and scanf are good enough", don't use std::string but char arrays and don't use std::vector but dynamically allocated arrays. And so on. I know this transition phase can be long. If you start with a good C++ book, you won't have a transition phase. Just by looking at the questions in this forum you should notice that a lot of people are in the transition phase.
quote:
Anonymous Poster:
It is possible to write good, clean, readable C code. Is that a 'bad habit'?
Yes. When doing C++, you should be writing good, clean, readable C++ code.

[edited by - civguy on July 28, 2003 3:50:33 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

quote:

Yes. When doing C++, you should be writing good, clean, readable C++ code.



That''s a bit like saying don''t learn BASIC first otherwise you''ll be writing BASIC instead of C++ and your code would be full of compile errors.

A lot of you seem to forget he doesn''t know how to code either language. When first starting out you should be coding very simple things. Nothing that requires templates, inheritance, even classes. If you can''t get your head around how variables work and their uses then what chance have you got of learning how and where to use classes. Walk before you can run.

And if you''re not going to be using any of the advance features of C++ you may as well stick to C as that''s more compatible.


Share this post


Link to post
Share on other sites