I know that C++ is based on C, but what are the differences?

  • Context: C/C++ 
  • Thread starter Thread starter Ki Man
  • Start date Start date
  • Tags Tags
    C++
Click For Summary

Discussion Overview

The discussion revolves around the differences between C and C++, focusing on their features, programming paradigms, and recommendations for learning. Participants explore the implications of these differences for new learners, particularly in a college setting.

Discussion Character

  • Exploratory
  • Debate/contested
  • Technical explanation

Main Points Raised

  • Some participants note that C++ includes features for object-oriented programming, such as classes, which are not present in C.
  • It is mentioned that C++ has a more extensive standard library compared to C, facilitating common tasks.
  • One participant highlights the difference in input and output methods, with C using printf() and C++ typically using iostreams.
  • There is a discussion about the flexibility of C versus C++, with some arguing that C is less flexible but safer, while others disagree and assert that C is equally capable.
  • Participants discuss the enforcement of coding standards by compilers, with examples of variable declaration rules differing between C and C++.
  • Recommendations for learning programming include starting with Python for its clarity, while suggesting C++ for those interested in object-oriented programming.

Areas of Agreement / Disagreement

Participants express differing views on the flexibility and safety of C compared to C++. There is no consensus on whether C is inherently less flexible or if its restrictions are beneficial. The discussion remains unresolved regarding the best language to learn first.

Contextual Notes

Some participants reference specific standards of C, such as ANSI C and C90, which may influence their arguments. The discussion also touches on the evolution of programming languages and their educational applications.

Who May Find This Useful

This discussion may be useful for new programming students, educators considering curriculum choices, and individuals interested in the comparative features of C and C++.

Ki Man
Messages
539
Reaction score
0
I know that C++ is based on C, but I recently checked out the book https://www.amazon.com/dp/1565923065/?tag=pfamazon01-20, thinking it was a C book but now I am unsure and can hardly tell the difference between the two.

Can someone help clarify what the differences are? which do you recommend that I learn to use as a tool when I get into college?
 
Last edited by a moderator:
Technology news on Phys.org
operator overload, inheritance, polymorphism, class...check in the TOC or index for these words.
 
C++ started out as basically C with extra features (most notably the 'class') to support object-oriented programming. So most of the basic constructs are the same, or very similar, in both languages: arithmetic expressions (calculations), boolean expressions (comparisons, etc.), if-statements, loops, ordinary functions, etc.

Where the two languages differ is basically that (a) C++ has features that directly support object-oriented programming, whereas C does not, and (b) C++ has a more extensive standard library than C does, for performing common tasks.

At the very beginning level, the biggest difference is in the way you normally do input and output. For example, C does output using the printf() function and its relatives, whereas C++ usually uses iostreams (e.g. 'count' for standard output), which are easier to use for simple input and output. However, C++ also allows you to use C-style output via printf(), for backwards compatibility with C.

Going a bit further, both C and C++ let you group data together using something called an "array", which works pretty much the same way in both languages. C++ also has something called a "vector" which can do things that an array cannot and is much more convenient to use than an array in many situations.

Similarly, in both C and C++ you can store text as an array of characters, but C++ also has a 'string' data type that is specifically designed for that purpose, which is more convenient and less prone to programmer errors.

Many C++ books, especially older ones, look a lot like C books, because they're basically C books that were re-tooled into C++ books. The language is C++, but they do things in a C-style way, using the older features inherited from C rather than the newer ones that were created for C++.

For an example of a "modern" approach to C++, try Koenig and Moo's "Accelerated C++".

At the college level, I think you'll find that most places use Java for their introductory programming courses now, because of its ties to the Web and because it has an even more extensive standard library than C++ does. The Java standard library includes graphics, networking, etc., which the C++ standard library does not. You can still do all that stuff in C++ or C, but you have to use libraries that are tailored for your operating system: Unix versus Windows versus MacOS, whereas in Java it's the same everywhere (at least in principle).
 
The difference between the two is very subtle. By C, I hope you refer to the 1990 ANSI C standard. The unfortunate thing is that most casual learners will never be able to tell the difference. I personally think that C is far less flexible than C++, but in a way that makes it safer. If you really need to know the difference, try compiling a program you've created and successfully compiled in C++ using a strict C compiler like GCC. If you're using MSVC, then rename the source file *.c instead of *.cpp. You'll be surprised at all the new errors that pop up.
 
mezarashi said:
I personally think that C is far less flexible than C++, but in a way that makes it safer.
It's a double-edged sword. The inflexibility of C makes it harder to shoot yourself in the foot -- but it also forces your code to be more cumbersome, meaning you have many more chances to shoot yourself in the foot, and makes your code harder to read and maintain.
 
I completely disagree with the notion that C far less flexible than C++.

There is nothing you cannot do in C that you can do in C++ except for using STLs.
 
By inflexible I don't mean incapable. There seems to be formal rigid method of writing code which is actually enforced by the compiler. Just an example off the top of my head, in C90, this is not allowed (but is okay to a C++ compiler):

Code:
for(int i=0;i<50;i++)

But I agree with Hurkyl that C++ is a much more enjoyable language to read and write. It is simply our own responsibility to make sure we adhere to strict coding rule standards, since the compiler does not enforce it for us.
 
mezarashi said:
There seems to be formal rigid method of writing code which is actually enforced by the compiler. Just an example off the top of my head, in C90, this is not allowed (but is okay to a C++ compiler):

Code:
for(int i=0;i<50;i++)
Honestly, I would have cited this as a failing of C90 (and thankfully fixed in C99). I can see some merit in some of C's restrictions, even if I dislike them (such as no function overloading)... but C90's harsh limitation on where you can declare a variable is my single greatest dislike of that standard: it is incredibly disruptive to both the writing and the reading process. :frown:
 
do most compilers accept both or just one or the other? can someone recommend me a Windowns Vista safe compiler/IDE?
 
  • #10
There's some C only compiler around, but the C++ one take C.

http://msdn.microsoft.com/vstudio/express/visualc/" , ... Take your pick.
 
Last edited by a moderator:
  • #11
Ki Man said:
which do you recommend that I learn to use as a tool when I get into college?

If you're new to programming, I'd say learn the basic procedural and object-oriented programming paradigms in Python. Python is a much cleaner language for learning how to think like a programmer than C or C++.

If you have to choose between C & C++ or you're just curious, I'd say C++ hands down. It would be very difficult to learn object oriented programming using C. You can always go to C from C++ if needed.
 

Similar threads

  • · Replies 16 ·
Replies
16
Views
5K
Replies
8
Views
3K
Replies
69
Views
11K
  • · Replies 7 ·
Replies
7
Views
1K
Replies
86
Views
3K
  • · Replies 118 ·
4
Replies
118
Views
10K
  • · Replies 8 ·
Replies
8
Views
5K
  • · Replies 8 ·
Replies
8
Views
3K
Replies
89
Views
7K
  • · Replies 1 ·
Replies
1
Views
2K