January 08, 2010

Difference between C and C++?

The first major difference between C and C++ is that C is a POP(Procedure Oriented Programming) Language whereas C++ is a OOP.

C uses Top Down aproach and focuses on procedures.Data moves as such
Whereas C++ uses bottom up approach and focuses on data(Data hiding,Abstaction)
Dyanimic memory allocation in C by calloc() and malloc() In C++ by new and delete.
C recognizes only first 32 char of string whereas C++ do not pose this limit.
Prototyping is optional in C mandatory in C++
main() may not have a return type in C but return type mandatory in C++
We can assign a void pointer to a non-void pointer in C .No such concept in C++
void *ptr1="vaibhav";
char *ptr2="Vai"
ptr2=ptr1 // valid in C

-In C, a void* can be implicitly converted to any pointer type, and free-store allocation is typically done using malloc() which has no way of checking if "enough" memory is requested

-When converting from C to C++, beware that C++ has more keywords than C:
int class = 2; /* ok in C. Syntax error in C++ */
int virtual = 3; /* ok in C. Syntax error in C++ */
-Calling an undeclared function is poor style in C and illegal in C++. So is passing arguments to a function using a declaration that doesn't list argument types
- int main() {
int s = sizeof('a'); /* silent difference: 1 in C++ ,sizeof(int) in C */
const int i=1; /* valid in c but not c++(extern const int i=1; volid in c++) */
}
More ................
1. C source code file name should be *.c and *.h for implement file and header file.

2. Variables reference is not supported, so you should not use it in C.
3. Structure definition is not the same in C++ and C
4. new and delete operator is not supported in C, you should use malloc and free c function to replace it.
5. In C, you cannot use template and template class.
6. From C++ to C, you should exclude every STL, ATL, COM, MFC functions, classes usage. You should write your own code to replace the above functionality.
7. In C, variable definition lies in the beginning of each function, whereas C++ you can put variable definition wherever you want. Therefore, You should move all variable definition that doesn’t lie in the beginning of each function to the beginning.
8. Class is not supported in C, so you should convert all of class definition in C++ to structures and functions in C. You should also know there have automatic initialization and releasing resource procedure in C++ as constructor and destructor implementation but C has not. The following is a rule to port:
9. The sizeof keyword gives the amount of storage, in bytes, associated with a variable or a type. In C, in default environment setting, sizeof returns the actual size include padding bytes inserted for alignment, whereas C++ doesn’t. So you should be very carefully about this situation.
10. The int type is not a hardware-independent type. You should know, in 16-bit system such as Windows 3.1 environment, sizeof(int) = 2, whereas in 32-bit system such as Windows 2000 & XP, sizeof(int) = 4. Therefore, in C, you should try your best not use int. You can use long (4 Bytes) or short (2 Bytes). This is a critical problem especially for file read and write.



urs ramanujadasu

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts