DEV Community

Discussion on: C vs C++: core language differences explained

Collapse
 
pgradot profile image
Pierre Gradot • Edited

Hello

C++ is compatible with most other programming languages, but C is not.

I don't really get this. From what I've heard, ABI compability with other languages is better with C.

In C, need to declare all variables at the beginning of the function block

This is wrong since............1999 !

C only supports primitive and built-in data types. C++ on the other hand, supports user-defined data types as well

You can define your own type in C with struct, union or enum.

// Structures 
struct stud_id
{
char name[20];
int class;
int roll_number;
char address[30];
};
Enter fullscreen mode Exit fullscreen mode

In C++, it is impossible to have a field named class because this a reserved keyword in C++.

C++ uses the new operator and free() for memory allocation and the delete operator for memory de-allocation.

Since C++11, we should use smart pointers as much as possible (hence avoiding directly calls to newand delete).

If you are a beginner programmer, it's recommended to start with C. [...] In fact, C++ can be complicated and overwhelming for beginners.

I tend to strongly disagree :)

Everything (almost?) you can do in C can be done in C++. Learning C++ can be easier than C:

  • Basic exercises that you can do in C are the same in C++.
  • Pointers are error-prone. You can avoid them for a long time in C++ thanks to references.
  • Strings are one of the most painful things in C. Let's use std::string from C++ instead.
  • printf() and scanf() are much more complicated to use than std::cout and std::cin.
  • C++ is not complicated: some features are complicated. Beginners can avoid variadic template and fold expressions for parameter packs ;)

Learn C if you really need C. Otherwise, learn C++.