by Oleksi Derkatch, coderprofile.com
keywords: C, C++, C plus plus, development
There have always been debates about the differences between C and C++. Hopefully this article will clear somethings up. I’ve seen a lot of content online, where C++ programmers assume that they know everything about C, just because they know C++. This is not the case. Hopefully this article will illuminate these differences. Knowledge of C and C++ is recommended for this article.
C++ was created by Bjarne Stroustrup as a superset to C. He originally called it “C with Classes”. After C++ became more and more popular, C++ began a standardization process and the name became C++. C++ contains all the features of C. That is to say that every C program is a C++ program, save a few specific times. All C++ programs are not C programs.
Object Oriented Programming
One of the most commonly known differences between the two programming languages is that C++ supports OOP and C does not. Although this is true, it is a very basic definition. C was originally built to be a procedural programming language. You can actually implement OOP in C, but it’s not true OOP and it’s not very clean. C++ supports multiple programming paradigms. OOP is simply one of them. For example, C++’s parameterized templates allow for generic programming. C++ also has support for procedural. It even has a few characteristics of functional programming (in the form of function pointers). Of course, you can assume that any C++ feature relating to classes does not exist in C, including things like friends, and virtual functions. I suppose that one could summarize the difference as such:C++ has better support for multiple programming paradigms when compared to C
C++ has support for generic programming in several different forms. First, C++ allows for function overloading. That is, creating two functions with the same name, that operate on different types of data. This allows a programmer to create a general interface acomplish a task. The second main support of generic programming comes in the form of templates. This features will generate overloaded functions at compile time for you, which saves you from writing out all type permutations yourself. Templates also allow you to generalize classes. The last way that generic programming can be achieved is through virtual functions. You can have a single method name work on several different derived classes, again allowing a programmer express behavior without specifying low-level types. C++ allows for generalizing work in the form of function overloading, parametrized templates, and virtual functions.
Macros and their uses
Any C programmer knows the importance of Macros in C. They are mainly used to create constants and to generate inline functions. C++ added several features to help eliminate the use of macros. For example, C++ added a const qualifier to define constants within a program. It also included the inline modifier to make functions inline. Although C++ still supports macros, it is recommended that you avoid using them. In C++, you should avoid using macros, and use C++ specific features to achieve the same results in a cleaner way.
In C, all libraries declare all their global variables in the global namespace. Any variable declared in a library can be accessed simply by including that file. Although this seems like a good feature, it makes it very simple for name collisions to occur. To solve this C++ implemented a namespace, which in essence modifies the scope of variables. Variables declared in a namespace can only be used through their namespace. That is what’s happening when you see something like “std::cout<<x<<std::endl;”. std is the namespace in which cout and endl are declared. Namespaces are another C++ specific feature that helps prevent name collisions.
C++ is more strict on type safety than C is. For example, in C, one does not need to cast in order to assign pointers of different types to one another. In C++, an explicit cast is necessary. C++ actually defines 4 different types of casting on top of the C-style cast. They are static_cast, dynamic_cast, static_cast, and reinterpret_cast. I will not get into the differences between these casts, as it is outside the scope of this article. Note that the C-style casting is depreciated in C++ and it is recommended that use the C++ casts. Also, C allows the int-by-default rule, while this is not allowed in C++. This rule is the rule that says that if a functions return type is not specified, it is assumed to be int. As a general rule, C++ is more strict on types than C.
C++ has it, C doesn’t. This is very simple. C++ allows you to catch and throw exceptions of different types. This gives C++ a better way of handling errors. C++ has support for exception handling, unlike C
C++ added references to it’s arsenal. References are similar in nature to pointers, only references are easier to use (and less powerful). References let a C++ program pass by reference without using pointer syntax. This is their main use in C++. As mentioned, references are rather limited in power. For example, you cannot do any of the following with references: change what they refer to, have a pointer to a reference, have an array of references. You must make references refer to something in their declaration, as they cannot be changed after that point. In that sense, references are similar to constant pointers. Of course C++ still has full support for pointers. C++ added references to let programmers pass by reference easier.
They way things are identified
In C, the full type of of any structure (or union) includes the word struct (or union). In C++, the name of the struct/union is sufficient. This is one of the ways C identifies things a little differently than C++. Another little naming difference is encountered when it comes to naming standard libraries. In C, you will always include standard libraries using the .h extension, but in C++, this is not the case. All standard C libraries are re-identified in C++. For example, the file stdio.h becomes cstdio. The general rule for including standard C libraries in C is simple. Prefix the library with the c character, and lose the .h. This becomes an identifier, instead of a file. The C++ compiler links the identifier cstdio to the file stdio.h and includes it. This is how that works. So when you include iostream, the C++ compiler looks for the file behind the identifier iostream. In C, you include the file directly. Of course, this only applies to standard libraries. For user-defined header files, the process is still the same (except that C++ headers have a .hpp extension rather than .h, but C++ can work with either one). C has a slightly different way of identifying aggregate types and standard files.
C++ adds a little more power to the struct. For example, structs in C++ can have methods, constructor and destructors. C does not have this power. C++ adds a little more features to structures.
Another simple difference. In C function prototyping is optional, in C++ it is mandatory.
Function taking no parameters
In C++ when you leave the parameter list of a function empty, you are telling the compiler that this function does not take any parameters. Doing the same in C implies that the function can take any number of parameters. In C, you need to then add the void to the parameter list to tell the compiler that this function is not taking parameters. In C++, this is not needed, although it is not an error to include it anyway. Functions taking no parameters are declared differently in C than in C++
Manual Memory Allocation
In C, you must use the malloc and free keywords to manually allocate and deallocate memory. In C++, you can use the new and delete keywords. They provide a cleaner way of manually working with memory. You can still use malloc and free, but new and delete are preferred. C++ provides a cleaner way to work with manual memory management.
Declarations of local variables
C dictates that all local variables must be declared at the top of their containing block, before any non-declarative statements. Although this is changing because of C99 standard, you will still see C code where all local variables are declared at the top of their block. In C++, you are allowed to declare a local variable anywhere in a block. C++ allows you do declare local variables anywhere in a block, but C forces you to declare all local variables at the top of the block.
Primitive boolean type
When it came to working with boolean types, you had to use an integer, where 0 was false and non-zero values were true. C++ added a primitive bool type that does not exist in C.
C++ allows a programmer to overload a lot of operators such as +, -, * and /. This features does not exist in C. C++ allows for operation overloading
C++ allows a programmer to specify default arguments for a function. C does not have this power. C++ allows default values for functions parameters.
Run Time Type Identification
Because C++ allows a programmer to have a lot more control with types, the type of an object cannot always be determined at compile-time. Because of this, C++ adds a system for determining the type of objects at runtime.
As you can see, there are a lot of differences between the two programming languages. I hope you learned something from this article. C is not C++ and vis versa. They are related, but contain a variety of differences. Feel free to PM me any comments or concerns about this article, especially if you spot something that I may have missed or misstated.
Did you like this? Share it: