Computer Science, asked by sankhyan62, 1 year ago

computer c and c++ difference

Answers

Answered by saxenaujjwal2003
1

Some differences between C and C++ are:

  • In C, for input or output, we use functions like gets(), puts(), scanf(), printf(), etc. These functions expect values to be of a particular data type, or in the case of scanf() or printf(), we have to specify the data type we want the function to process. In C++, we can and sometimes do use these functions. However, people generally prefer using the objects cin and cout. Note that these are objects of a class, and when we perform I/O, these themselves determine the data type of the variable.
  • C does not support function overloading. You cannot have multiple functions with the same name. So suppose you have a function which calculates the square of an integer number and a function to calculate the square of a floating point number, you have to give them separate names. In C++, give them the same name, and the language will determine which function to use depending on the types of parameters passed to the function.
  • C does not support exception handling. You are supposed to prevent conditions which lead to a runtime error due to some unexpected turn of events. In C++, you can throw exceptions and handle them accordingly. This is extremely useful when making large projects.
  • Besides function overloading, C++ also supports operator overloading not present in C. Ever wanted to concatenate two strings with the + operator? Well, tough luck with C, but with C++, you can do that. Of course, classes make it all possible, and you have to use std::string instead of regular strings, but then, std::string is much better for sanity.
  • And the previous point brings us to this, std::string. This is a (surprise, surprise) class, for strings. Much easier to use and read than C style strings.
  • For dynamic memory allocation, C uses malloc() and free(), but C++ has new and delete operators which are much better.
  • Namespaces are getting popular, especially in application development, and C++ does have support for namespaces, which is lacking in C.
  • An annoying feature of C is that you need to declare all your variables at the start of their scope, i.e, right after the opening brace of the block or function. In C++, define an variable anywhere, preferably closest to where it is used.
  • In C, your global variables can have multiple declarations. C++ is stricter, it doesn't allow this.
  • C++, after all, is object oriented. Things like abstraction, inheritance, polymorphism,  cannot be done in C. It brings into your hands a whole new paradigm. Use it if you like it. If not, you can make do without it (but a lot of features of C++ are built around classes).
  • Ever wanted to create similar functions differing only in the data types they deal with? You can do it with function overloading, but that is generally used for function with the same name but different behavior. What if you want to do exactly the same thing, but for different data types? It doesn't make a lot of sense to write multiple functions for it. And function overloading does not support functions differing only by return type. Here is where templates come in. You can create generic functions and classes and use them with any data type. C++ has it, C doesn't.
  • C++ has STL, an extremely useful list of templated classes (so you can use them with a variety of data types). You have ready made algorithms, data structures, iterators, etc. Perhaps what every C++ programmer would boast about to a C coder.
Answered by aniketpruthvi
0

The major difference between C and C++ is that C is a procedural programming language and does not support classes and objects, while C++ is a combination of both procedural and object oriented programming language; therefore C++ can be called a hybrid language.

Similar questions