C or C++ which is better ?

Which is better to participate and solve more problems easily - C or C++, and why ?

C++ because of STL. STL involves many inbuilt functionality which is otherwise time consuming if implemented in C. For example algorithms like Dijkstra’s Algorithm can be easily implemented with the help of STL in C++.

1 Like

Thanks sir…but I have seen many codes written in both C and C++…and the same code written in c++ are longer then C ??

The code you write depends on your algorithm and the length doesnt matter. Moreover I think the codes in C++ that you have seen might have included many libraries and macros thus making the code look bigger.

Thanks sir…what exactly is STL and macros are ??

STL stands for standard template library . It gives you in-built functions that you may use time and again.For example if you have a vector say vector vec (that’s the syntax for a vector) and you want to sort it . In normal codes, you’ll perform merge sort etc. , which is time consuming.
But, if you are using STl , then your vector gets sorted by the function called sort() . And, syntax would be , sort(vec.begin(),vec.end()) . So, that’s all you would have to type and you get a sorted vector. Plus, using vector’s and standard containers you get lesser segmentation faults.
Similarly, there are many inbuilt templates for functions like sort,count, for data structures like stack etc.

Here are some links , that may prove helpful :
1.http://www.sgi.com/tech/stl/
2.http://www.cplusplus.com/reference/stl/

I guess stl will help you code faster and it gives more focus on your problem , as you don’t need to worry about algo’s for functions like sort etc.

Regarding macro’s , it’s another way to reduce your code , say you are using you want to find the maximum of two numbers again and again , you need not call a function , instead you can invoke the pre-processor directive #define MAX(a,b) (a > b) ? a : b , which will return maximum of two numbers.
But, use of macro’s is your own choice.

So, I think c++ might be a better option considering the presence of stl.

1 Like

When dealing with macros, use proper parenthesis. Otherwise, you get unwanted results!!!

#define MAX(a,b) ((a) > (b) ? (a) : (b))

I would say also c++ because of Standard Template Library.

functionality vise C++ is better than .
but to prepare for studies and to start programming i would recommend C…
Java can be used instead of C++, But C has no alternate.
C is the obvious choice to many code starters and build programming skill and Mostly C is used.

C++ is better than C because its support object oriented programming we create class and object of that class. there are many useful concept in C++ like friend function, Inheritance, polymorphism,Interface. also we can protect our data members

C++ is better because of STL and object oriented programming support and also you can use it like C(scanf and printf).

4 Likes

There is no such thing like comparing C with C++. C stands for kernel, drivers and Operating Systems where C++ doesn’t. Please stop comparing them…or saying that C++ is better than C.

Advantages of C++ over C -

  1. Supports OOP(Object Oriented Programming)
  2. Standard Template Library
  3. Many other in-built templates
3 Likes

C is widely used for Operating Systems, Kernels, other system softwares etc but for competitive programming, C++ is better as it gives OOP support, STL and you can use fast I/O even while writing cin/cost using “ios:sync _ with _ stdio(false)” or other related statements.

3 Likes

C++ was developed by Bjarne Stroustrup in 1983 at Bell Labs , as a “better C”. C++ was an attempt to extend and to some extent change the C programming language to overcome some of the common problems with the language. i.e C++ is C incremented.

Features of C++

  • C++ is an intermediate-level language

  • C++ Supports principle of object oriented paradigm.

  • C++ gives access to lower-level functionality than other language

  • C++ have huge Function Library

  • C++ joins three separate programming categories:

       - the procedural language, represented by    C.
    
       - the object-oriented language, represented by the class  enhancements C++ adds to C.  
    
       - generic programming, supported by C++ templates. 
    
  • C++ is very easy to read and write.