Thursday, January 14, 2010

Difference between C and C++:



  • C++ is object oriented while C is function or procedure oriented.

Object oriented programming paradigm is focused on writing programs that are more readable and maintainable. It also helps the reuse of code by packaging a group of similar objects or using the concept of component programming model. It helps thinking in a logical way by using the concept of real world concepts of objects, inheritance and polymorphism.

On the other hand, functional and procedural programming focus primarily on the actions and events, and the programming model focuses on the logical assertions that trigger execution of program code.


  • C++ allows the programmer to create classes, which are somewhat similar to C structures

However, to a class can be assigned methods, functions associated to it, of various prototypes, which can access and operate within the class, somewhat like C functions often operate on a supplied handler pointer.

  • C++ applications are generally slower at runtime, and are much slower to compile than C programs.
The low-level infrastructure for C++ binary execution is also larger. For these reasons C is always commonly used even if C++ has a lot of popularity, and will probably continue to be used in projects where size and speed are primary concerns, and portable code still required (assembly would be unsuitable then).

  • Implicit Assignment from void*
You cannot implicitly assign from a void* to any other type. For instance, the following is perfectly valid in C (in fact, it's arguably the preferable way of doing it in C).

int *x = malloc(sizeof(int) * 10);

but it won't compile in C++.

What this means is that you can have a void* that points to anything at all, and if you then assign the address stored in that void* to another pointer of a different type, there isn't any warning at all about it.

Consider the following:

int an_int;
void *void_pointer = &an_int;
double *double_ptr = void_pointer;
*double_ptr = 5;

When you assign *double_ptr the value 5, it's writing 8 bytes of memory, but the integer variable an_int is only 4 bytes. Forcing a cast from a void pointer makes the programmer pay attention to these things.

  • Freeing arrays: new[] and delete[]

In C, there's only one major memory allocation function: malloc. You use it to allocate both single elements and arrays:

int *x = malloc( sizeof(int) );
int *x_array = malloc( sizeof(int) * 10 );

and you always release the memory in the same way:

free( x );
free( x_array );

In C++, however, memory allocation for arrays is somewhat different than for single objects; you use the new[] operator, and you must match calls to new[] with calls to delete[] (rather than to delete).

int *x = new int;
int *x_array = new int[10];
delete x;
delete[] x;

The short explanation is that when you have arrays of objects, delete[] will properly call the destructor for each element of the array, whereas delete will not.

  • You must declare functions before use

Although most good C code will follow this convention, in C++ it is strictly enforced that all functions must be declared before they are used. This code is valid C, but it is not valid C++:

#include
int main()
{
foo();
return 0;
}

int foo()
{
printf( "Hello world" );
}

  • No Boolean టైపు
C does not provide a native boolean type. You can simulate it using an enum, though:

typedef enum {FALSE, TRUE} bool;

  • main Doesn't Provide return 0 Automatically

In C++, you are free to leave off the statement 'return 0;' at the end of main; it will be provided automatically:

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

but in C, you must manually add it:

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