Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Difference Between C and C++ - The Complete List (http://www.go4expert.com/articles/difference-c-cpp-complete-list-t29722/)

shabbir 7Jul2013 10:56

Difference Between C and C++ - The Complete List
 
The misconception that C++ is an advance C still prevails among developers and so let me try to clarify that C++ only inherits some syntax from C and nothing more than that. Before I get into the details about the difference between C and C++ programming let me clarify a myth, which is, you need to know C to know C++ and this is completely false. You can very well start with C++ and you will have no issues whatsoever.

1. Procedural and Object Oriented Programming



C is a procedural language where as C++ is an object oriented programming language. The main reason its needs to be stated each time C and C++ are compared because as C++ derives basic syntax from C programming i.e. the way C declare basic variables, use loops and conditional statement which makes C++ to also supports procedural way of programming as well but C++ just supports C ways of programming but that does not make C++ a procedural language.

Now as C++ is an object oriented programming language, it supports Encapsulation, Abstract Data Types and Polymorphism etc.

2. Levels



Many people considers C to be low-level language where as others consider C to be high-level language as well. There are many schools of thoughts when it comes to C Programming. The level of programming language is actually an abstraction level that it provides to the final assembly code and going by that definition C++ has higher level of abstraction than C Programming because it provides abstract data types and is more closer to the real world objects where as C only supports structures and does not support objects.

3. Overloading



C++ supports function overloading, which means you can have same name of a function with varying parameter type but C does not support function overloading. People are confused with the use of function overloading in the syntax of C Programming but if you are using a C++ compiler and using overloading, you are actually using a C++ feature and not C. Most compilers will not allow this if you name your c program with .c file extension instead of .cpp file extension. The idea of how each compiler behaves is beyond the scope of this article. ;P

4. Struct



C struct does not support functions but a C++ structure does support functions. C++ structure also supports private, public and protected member variables as well as functions but C struct does not have any such thing as private and protected and everything inside a struct is public.

I can sense a question creeping in your mind about difference between class and struct that has a point which says struct variables and functions are public by default where as class variables and functions are private by default. The question is difference between C++ class and C++ struct and not C++ class and C struct.

5. Data Types



C does not provide a boolean or string datatype where as C++ provides those datatype. Boolean in is implemented in C using Enum and string as character pointer but C++ provides them as datatype.

6. Class and Namespace



We don't have classes in C viz a viz the namespace. As we don't have classes in C, we don't have templates either.

7. C++ Operators



In C Programming functions were used for many operations like Input (scanf), output (printf), memory allocation (malloc) and de-allocation (free) but in C++ they were replaced with operators like << and >> new and delete operators.

There is one more difference when it comes to use of malloc and new with free and delete. malloc is used for both single elements and arrays the same way.
Code:

int *a = malloc( sizeof(int) );
int *arr = malloc( sizeof(int) * 10 );

And there is no difference to the call to free function for both single element and array.
Code:

free( a );
free( arr );

But in C++ we have new and new[] as well as delete and delete[]
Code:

int *a = new int;
int *arr = new int[10];
.
.
delete a;
delete[] arr;

Note delete[] does not need the array count.

8. Operator Overloading



Like function overloading can extend the functionality for various kind of data, operator overloading can extend the functionality of operators.

9. Exception



C++ supports exceptions but C does not support exceptions.

10. Changes in Behavior of Functions



In C programming main is yet another function that can be called from other functions or can be executed recursively from main itself but in C++, you cannot call main from other functions including main.

Apart from main function all C++ functions must be declared before they can be used where as in C you can use the function before it is declared.

11. Top-down & Bottom-up Approach



C uses the top-down approach while C++ uses the bottom-up approach

12. Reference Variable



C++ allows reference variables whereas C only supports pointers.

Conclusion



If you try to find similarities between C and C++, you will the list being lot shorter and simpler than the differences. If you think I have missed any thing please share them in comments below and I will be more than happy to add them to the list of differences.

lionaneesh 7Jul2013 11:39

Re: Difference Between C and C++ - The Complete List
 
Nice article. But you missed templates, the auto keyword, foreach, range-for, constexpr, operator overloading and some more.

shabbir 7Jul2013 13:14

Re: Difference Between C and C++ - The Complete List
 
Quote:

Originally Posted by lionaneesh (Post 99787)
Nice article. But you missed templates, the auto keyword, foreach, range-for, constexpr, operator overloading and some more.

Templates added in the classes point. Thanks for pointing that out.

Auto, static keywords are part of C as well but correct me if I am wrong.

range-for, constexpr I am not aware of and if you can share some context, I will be more than happy to add it.

I had operators and overloading and so missed the operator overloading but added that. Thanks for pointing that out.

lionaneesh 7Jul2013 13:30

Re: Difference Between C and C++ - The Complete List
 
Quote:

Originally Posted by shabbir (Post 99788)
Templates added in the classes point. Thanks for pointing that out.

Auto, static keywords are part of C as well but correct me if I am wrong.

range-for, constexpr I am not aware of and if you can share some context, I will be more than happy to add it.

I had operators and overloading and so missed the operator overloading but added that. Thanks for pointing that out.

Sorry for not pointing this out clearly: I am talking about auto in C++11. The keyword is used to deduce types automatically from initialization expression. In terms of code:

Code:

auto integer = 100;
auto character = 'c';

Check this for more info.

Here are good references for Range-for and Constexpr. Again, these were added in C++11, so you might wanna point that out when you add these in your article.

Also, you should specify the C++ standard with you are comparing C with. If you are just comparing the ISO C++ standard. Constexpr, Range-for, auto are not a part of it.

shabbir 7Jul2013 13:39

Re: Difference Between C and C++ - The Complete List
 
Quote:

Originally Posted by lionaneesh (Post 99789)
Sorry for not pointing this out clearly: I am talking about auto in C++11. The keyword is used to deduce types automatically from initialization expression. In terms of code:

Code:

auto integer = 100;
auto character = 'c';

Check this for more info.

Here are good references for Range-for and Constexpr. Again, these were added in C++11, so you might wanna point that out when you add these in your article.

Also, you should specify the C++ standard with you are comparing C with. If you are just comparing the ISO C++ standard. Constexpr, Range-for, auto are not a part of it.

Yes the comparison is from ISO C++ because lot has been added to C++ as we have moved along and it makes very little sense to be comparing it with C++ 11. :D


All times are GMT +5.5. The time now is 07:06.