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

  • C/C++
  • Thread starter Ki Man
  • Start date
  • Tags
    C++
In summary, C++ is based on C, but it has extra features to support object-oriented programming. It also has a more extensive standard library than C does.
  • #1
Ki Man
539
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
  • #2
operator overload, inheritance, polymorphism, class...check in the TOC or index for these words.
 
  • #3
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. 'cout' 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).
 
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 
  • #7
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.
 
  • #8
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:
 
  • #9
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.
 

1. What is the main difference between C and C++?

The main difference between C and C++ is that C is a procedural programming language while C++ is an object-oriented programming language. This means that C++ allows for the use of classes, encapsulation, and inheritance, while C does not have these features.

2. Can C code be compiled in a C++ compiler?

Yes, C code can be compiled using a C++ compiler. C++ is designed to be backward compatible with C, so most C code can be compiled and run in a C++ environment. However, some C code may need to be modified in order to work properly in a C++ compiler.

3. Are there any differences in syntax between C and C++?

Yes, there are some differences in syntax between C and C++. C++ includes additional keywords and features, such as classes and namespaces, that are not present in C. Additionally, C++ also has stricter type checking and different rules for variable scoping.

4. Which language is more commonly used - C or C++?

Both C and C++ are widely used in the programming world, but C++ is more commonly used in modern applications. This is because C++ offers more features and is better suited for complex programs, while C is often used for system programming and low-level tasks.

5. Is it necessary to know C before learning C++?

No, it is not necessary to know C before learning C++. While having a basic understanding of C may make it easier to learn C++, it is not a requirement. C++ can be learned and used independently, and many programmers start with C++ without prior knowledge of C.

Similar threads

  • Programming and Computer Science
Replies
7
Views
683
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
2
Replies
69
Views
4K
  • Programming and Computer Science
Replies
16
Views
2K
  • Programming and Computer Science
Replies
8
Views
878
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
666
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
4
Replies
118
Views
6K
Back
Top