on September 20, 2009 by Wolf in Programming, Comments (6)
Why do you use C instead of C++?
Or just as reasonably, Why use C++ when C is perfectly good??
C is the older of the two and is a “functional” language. “Functional” is not here the opposite of “dysfunctional*” which means that something has abnormal functionality; nor is it the opposite of “non-functional”, which means the object is not in a usable state. In programming, Functional is set opposite to “Object-oriented.” Another way to characterize C is to say it is “procedural” in nature. This means that the entire program is set up around a series of procedures which can also be called modules, or functions.
A C program could be built around a single procedure (the main() function) or there could be several functions that are called at various places through the program. If you built a word processor application, you would expect your users to call the saveFile() function at least once per session, as it is how they would cause the program to write the file to a location on the hard drive. You would only have to define the saveFile() function once, and the users could call it as many times as they want.
C++ is almost the same as C, but it uses the Object-Oriented Programming paradigm. OOP programmers design Object classes which are a sort of meta-object having the characteristics and methods accessible to an object of the sort defined. This is a little like an ideal in Platonic philosophy. The ideal table consists of a flat horizontal plane or surface with a slope approaching zero, and some sort of mechanism for holding the top surface some distance from the floor.
The parameters any table has are:
Height from floor
- Width
- Length
- Weight limit
- Materials
Now we can call any instance of our ideal table and define the size (Height, Length, Width), strength of construction and materials. Object-oriented programming might require a class to be constructed of which we would make instances within the program, or me might reuse existing classes in our program. Well-understood classes can make your program source code easier to read and understand, and that may make maintenance and enhancement of the application simpler as well.
Functional languages may make faster-running code, since you are not as likely to reuse very complicated multi-purpose functions or procedures, but you may use unneccesarilly complete classes (since you have them on hand) in your OOP applications.
Anything you can build in C can also be implemented in C++. C++ helps you to standardize the way code is created and managed (in classes rather than the more open to view C functions). C++ reduces, slightly, the numbers of ways a programmer might have contacts and internal methods of functions, since class methods and components are set at a higher level than procedural functions. C programmers have more choice when it comes to the implementation of design. C++ applications may be simpler to read and maintain, while C applications may run a little faster.
Book Mark it-> del.icio.us | Reddit | Slashdot | Digg | Facebook | Technorati | Google | StumbleUpon | Window Live | Tailrank | Furl | Netscape | Yahoo | BlinkListTags: C/C++, programming

?ukasz Krawczyk
March 16, 2010 @ 7:16 pm
C is not a functional language, it’s imperative one. Functional is not a synonym for procedural.
http://en.wikipedia.org/wiki/Functional_programming
http://en.wikipedia.org/wiki/Imperative_programming
Object oriented and procedural paradigms are orthogonal in context of C language family, you can write object oriented code in C(you just won’t have syntax for it) as well as purely procedural code in C++.
Finally, the difference in speed between those two languages is caused primarily because C++ code tends to be more abstract, which leads to situations when it cannot be effectively optimized by compiler(memory aliasing trough pointers), so it is not only matter of using dynamic binding(polymorphism).
KK
January 11, 2011 @ 7:04 pm
Wow, you are so smart guys!
So C is basically the same thing as c++ but it does not have classes?
So if you compile a C program in a c++ compiler will it work? Will you have to change some things or you will have to change it completely?
And also is there Visual C, like Visual C++, Visual C# or Visual Basic? Can you write Windows programs with C?
Wolf
January 12, 2011 @ 2:25 am
You can definitely write windows programs in C. The Windows OS was itself written in “…C, C++, and C# for Windows. Some areas of code are hand tuned/hand written assembly…” – Ryan Waite – Product Unit Manager – Windows HPC ( social.microsoft.com/Forums/…)
You can (probably) compile a C program with a C++ Compiler, but not the reverse (Geekinterview.com)
Visual C, Visual C++, Visual C# are offshoots built on the C platform. Visual Basic is built on the BASIC language. All are Microsoft products, I believe, and all are supported by Microsoft’s Visual Studio IDE. If you want to try out Visual Studio, check out Dreamspark.com
Network infrastructures
January 12, 2011 @ 4:41 am
Interesting article.I see no reason to use C instead of C++. Whatever you can do in C, you can do it also in C++. If you want to avoid overheads of VMT, don’t use virtual methods and polymorphism. However, C++ can provide some very useful idioms with no overhead. One of my favorites is RAII.Classes are not necessary expensive in terms of memory or performance.
Sorin
May 20, 2011 @ 11:37 pm
You know, I got to this page after searching the web for the EXACT OPPOSITE: “why use c++ instead of c?”
My question was “what are the advantages of c++ that cannot be just more easily written in c?”
So I was surprised that most articles were actually discussing about “why would someone use c instead of c++ ?”
So before any more harm is done, and before any more casualties among the ranks of newbie/freshmeat programmers, and among the armies of programmers in general, I suggest you to have a good look at this article:
http://yosefk.com/c++fqa/defective.html
Hopefully this will convince new software “recruits” to stay away from C++, and either stick to C, or better yet to learn some useful high-level programming language like java, ruby, scala, or even javascript (which is a really great dynamic language, and can be viewed as superset of LISP – “LISP with named slots”)
Wolf
May 23, 2011 @ 6:39 am
Thanks for the link, Sorin. I didn’t realize the depth of the problem. It seems like most beginners I meet are interested in getting to programming GUIs as fast as possible. Javascript may be the easiest path to their ends.