Differences between C and C++

C and C++ both are programming languages. Actually, C++ was based on C & retains a great no of functionality. Both of them have some similarities and differences. In this page, some of the differences between C and C++ are noted below:

OOP (Object Oriented Programming) concepts:

C++ supports object oriented programming concepts. But C doesn't support OOP (Object Oriented Programming). C is actually a POP (Procedure Oriented Programming) language.

Memory allocation

n C, to allocate memory space (both single elements and arrays) malloc(), calloc() function are used and you can also free the memory using free() fucction. For example:

int *a = malloc( sizeof(int) );
int *a_array = malloc( sizeof(int) * 20 );
free( a );
free( a_array );

But for C++ the situation is a bit different. For the allocation of memory use new[] operator and use delete[] to free the memory space. For example:

int *a = new int;
int *a_array = new int[20];

delete a;
delete[] a;

Predecessor:

C is the predecessor of C++. A lot of C remains in C++. C++ allows programmer to program more easily then using C.

No Boolean Type

C++ can provide a native boolean type. But C does not provide a native boolean type. To simulate it, you have to use an enum, though:

typedef enum {FALSE, TRUE} bool;

Level of language

C++ is high level language. Whereas C is a low level language.

Syntax supporting:

C++ support all the syntaxs of C. But C doesn't support all the syntax of C++

Function supporting:

C++ can support of all function of C language, but C can't support of all function of C++ language.

Function declaration before to use

Most good C programs follow that the function should be declared before to use. You may also define other places also. But for C++, it's strictly enforced that all the functions must have to be declared before to use.

C++ contains a largeer library

C++ has a much larger library than C. Moreover, some things may be automatically linked in by C++ when they are not with C. For instance, if you're used to using g++ for math-heavy computations, then it may come as a shock that when you are using gcc to compile C, you need to explicitly include the math library for things like sin or even sqrt:

% g++ foo.cc

main Doesn't Provide return 0 Automatically

In C++, if you don't mention return 0 at the end of main, it is provided automatically. For example:

int main()
{
    printf( "Hello, World" );
}

But in C, it must be added manually. Otherwise, the copiler shows an error during the compile time and running time. For example,

int main()
{
    printf( "Hello, World" );
    return 0;
}

Creating Classes:

C++ allows the programmer to create classes (Classes are similar to C structures). But C doesn't allow programmers to create classes.

Operator and function overloading

Operator and function overloading is not supported by C. If a function has a name, then that function can't use in the program again. But using C++, that is possible. You can use the same function name with different arguments.

Char string limit

C can only recognizes first 32 char of string. But C++ doesn't pose this type of limitation.

Top down or bottom up approach

C programming follows Top Down aproach and focuses on procedures. Whereas C++ is a bottom up approach and focuses on data (data hiding, abstaction).

Prototyping

Prototyping is an optional in for C programming. But it's a mandatory in C++ programming.

Void pointer assigning to a non-void pointer

Using C, assigning a void pointer to a non-void pointer is possible. But C++ does not bearing such type of concepts.

     void *ptrx="vaibhav";
     char *ptry="Vai" ;
     ptry=ptrx                                 // only valid in C

All these are the differences between C and C++. If you want to include many other differences, please add it to the comment section.

© Written by rancidTaste

If you are enjoyed this post, please consider to leave a comment at the comment section of this page or Subscribe to rancidTaste's RSS feed to get new pages which will be delivered to your feed reader. You can also read more hubs by rancidTaste.

More by this Author


Comments 2 comments

bfg 6 years ago

realy gud one


kumar 6 years ago

simple and very good

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working