C vs C++: core language differences explained 
Tags: languages, programming, cpp
Published: 2021-01-22 18:11:36

Both C and C++ are two of the oldest surviving programming languages. Though C++ is derived from C, it is known to be more efficient and offer modern tools. Of course, both languages have their own advantages and disadvantages over one another.

In this article, we will go over a brief history of both languages, followed by their similarities and differences, and what language you should start learning first.

___

## What is C?

C was developed by Dennis Ritchie in 1972 for making utilities capable of running on Unix. C is a systems programming language, meaning it works in the **lowest level of abstraction**. It is a low-level procedural language. C programs are high speed, so they let developers handle the computer hardware manually. 

The strength of C programming language lies in performance and has the ability to be used for coding for a wide **variety of platforms**. It's used commonly for operating systems, interpreters, compilers, and microcontrollers. 

Nowadays, we have many specialized programming languages to pick from, but C was once unmatched in its early years.

```c
#include<stdio.h>
int main() {
    printf("Hello World\n");
    return 0;
}
```

> Output: Hello World


___

## What is C++?

C++ was developed by Bjarne Stroustrup in 1979 while working at Bell Labs. He wanted an extension of C that was both flexible and efficient. C++ is **object-oriented**, but like C can be used for development on a diverse range of platforms. It also supports manual memory management. C++ is great for networks, server-side, and gaming applications. 

The programming language is  lightweight, compiled, and can be used for wide range of platforms. In fact, the C++ programming language has almost everything as C, but it extends its functionality. 

> C++ influenced the creation of C# and Java. If you know Java, you can easily read C++.

```cpp
#include <iostream>
using namespace std;

int main() {
  cout << "Hello World";
  return 0;
}
```

> Output: Hello World



___

## Similarities between C and C++

Now that we know a little bit about both languages, we will now look at the similarities between the two. C++ is a superset of C, so both languages have similar syntax, code structure, and compilation. Almost all of C's **keywords** and **operators** are used in C++ and do the same thing. 

C and C++ both use the **top-down execution flow** and allow procedural and functional programming. Both languages also use `;` as the statement terminator. They also have the same notions of stack, heap, file-scope, and static variables.

The following keywords are **common to both languages**:
* `auto` 
* `break` 
* `case` 
* `char` 
* `const` 
* `continue`
* `default` 
* `do` 
* `double`
* `else` 
* `enum` 
* `extern` 
* `float` 
* `for` 
* `goto`
* `if` 
* `int` 
* `long`
* `register` 
* `return` 
* `short` 
* `signed` 
* `sizeof` 
* `static`
* `struct` 
* `switch` 
* `typedef` 
* `union` 
* `unsigned`
* `void`
* `volatile` 
* `while`

___

## Differences between C and C++

In this section, we will look at the most important differences between the two languages. 

<br>

#### Definition
C is a structural programming language, so everything is broken into functions that get the work done. C does not support objects and classes.

C++, however, supports procedural and [object-oriented programming](https://www.educative.io/blog/object-oriented-programming) paradigms. It focuses on using objects and classes. 


<br>

#### Exception Handling
C uses functions for error handling. C++ has well-designed try-catch blocks that make debugging a lot easier. 

<br>

#### File Extensions
All C programs are saved with a `.c` extension. C++ uses the `.cpp` extension. 

<br>


#### Variables
In C, need to declare all variables at the beginning of the function block. In C++, the variables can be declared anywhere as long as they are declared before used in the code.

<br>

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

```cpp
// Classes
class <classname>
{
private:
Data_members;
Member_functions;
public:
Data_members;
Member_functions;
};
```

```cpp
// Structures 
struct stud_id
{
char name[20];
int class;
int roll_number;
char address[30];
};
```

```cpp
// Unions
union employee
{
int id;
double salary;
char name[20];
}
```
```cpp
// Enumerations 
enum week_days{sun, mon, tues, wed, thur, fri, sat};
int main()
{
enum week_days d;
d = mon;
cout << d;
return 0;
}
```
```cpp
//Typedef
typedef <type> <newname>;
typedef float balance;
```

<br>

#### Strings

C represents string literals using `char[]`. In C++, strings are objects of the class string, defined in the header file `<string>`. This is how strings are represented in C:


```c
char s1[20]; 
char s2[20] = { 'h', 'e', 'l', 'l', 'o', '\0' }; 
char s3[20] = "hello"; 
char s4[20] = ""; 
```

In C++, strings are represented as follows:

```cpp
string s1; 
string s2("hello"); 
string s3 = "hello"; 
string s4(s2); 
string s5 = s2; 
```

#### Multithreading

In C, multithreading is not supported natively.  To achieve multithreading, it uses the operating system such as  POSIX Threads with Linux. 

For C++, multithreading was introduced in C+11, which uses the `std::thread`. [C++ multithreading](https://www.educative.io/blog/modern-multithreading-and-concurrency-in-cpp) involves creating and using thread objects to carry out sub-tasks.


<br>

#### Function Overloading
Function overloading is a form of polymorphism that allows a function with the same name to be defined for varying purposes. Overloaded functions have the same name but different parameters. C **does not** support function overloading, but C++ does. 

In the C++ example below, we have the same function names but different data types.

```cpp
int add(int x, int y) // first definition
{
cout<< x+y << endl;

return 0;
}

float add(float a, float b)
{
cout << a+b << endl;
return 0;
}


double add(double x, double y)
{
cout << x+y << endl;
return 0;
}
```

#### Operator Overloading 
Operator overloading allows you to change the way an operator works for user-defined functions. Though C does not support this, C++ does.


<br>

#### The `main()` function
C only allows the `main()` function to be called through other functions used in the code. C++ does not allow the `main()` function to be called through other functions. 

<br>

#### Data Security and Encapsulation

Encapsulation aids in hiding information from users and is s key feature of OOP. C does not support encapsulation. C++ uses classes that bundle data and the functions operating on this data into a single unit. 

<br>

#### Input and Output Operations
C uses `printf` and `scanf` for input and output respectively. C++ uses `cin` and `cout`. 

<br>

#### Memory Management 
As mentioned above, both C and C++ require manual memory management, the difference is how they do it. C uses `calloc()` and `malloc()` functions for dynamic memory allocation. 

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

> Since C++11, it is recommenced to use smart pointers as much as possible to avoid avoiding directly calls to `new` and `delete`.


___

## Which to learn first?

The answer to this question depends on your career and programming goals. It may seem that learning C++ is the better option as it offers more than C, but that's not always the case.

Some recommend starting with C++ for the following reasons:
* Everything you can do with C, you can do with C++
* Pointers are more error-prone in C
* Strings are easier to implement in C++

However, if you are interested in **systems-level programming**, you may want to learn C first to get a solid foundation. If already have some experience with C#, learning C will be familiar. Once you are comfortable with procedural programming in C, you can then move to other languages like C++ or Java. 

For those who already **understand OOP**, or those who are new to programming overall, learning C++ first may be a good option. C++ is also more common in most industries, and it is an accepted language in any coding interview. If you're looking to learn something that can be **immediately used in work**, start with C++.

Similarly, learning C++ first is recommended for programmers who **already have some knowledge** of Java.
<br>

___

## What to learn next

**You can start with the following in C:**
* Decision making
* Loops
* Functions
* Arrays and Pointers
* File I/O
* Memory Management

**You can start with the following in C++:**
* Classes and objects
* Inheritance
* Polymorphism
* Abstraction
* Encapsulation
* Interfaces


Educative has both beginner and advanced courses for C++ and C. The best place to start is Educative's Learning Path **[C++ for Programmers.
](https://www.educative.io/path/cpp-for-programmers)** We'll take you from basic to advanced concepts, all with hands-on practice. By the end, you'll have enough C++ experience to confidently solve real-world problems.

If you want to start with C, check out Educative's free introductory course **[Learn C from Scratch.
](https://www.educative.io/courses/learn-c-from-scratch)** This comprehensive and detailed course will introduce you to all the basic and advanced programming concepts of C language. 

*Happy learning!*