Year-End Discount: 10% OFF 1-year and 20% OFF 2-year subscriptions!

Home/Blog/C vs C++: Core language differences explained

C vs C++: Core language differences explained

Jan 18, 2021 - 7 min read
Maryam Sulemani
editor-page-cover

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.

This article covers the following:


Learn C++ the easy way.

This path will get you up to speed with C++. By the end, you’ll be able to confidently solve real-world problems in C++.

C++ for Programmers


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.

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

Enjoying the article? Scroll down to sign up for our free, bi-monthly newsletter.


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 a 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++.

#include <iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}
Hello World for C++

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


Keep the learning going.

Learn C++ without scrubbing through videos or documentation. Educative’s text-based courses are easy to skim and feature live coding environments, making learning quick and efficient.

C++ for Programmers



Differences between C and C++

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


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 paradigms. It focuses on using objects and classes.

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


Exception Handling

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


File Extensions

All C programs are saved with a .c extension. C++ uses the .cpp extension.


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.


Data Types

With C, you can define your own type using struct, union, or enum.

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

C++ supports user-defined data types as well. C++ user-defined data types include:

// Classes
class <classname>
{
private:
Data_members;
Member_functions;
public:
Data_members;
Member_functions;
};
// Structures 
struct stud_id
{
char name[20];
int class;
int roll_number;
char address[30];
};
// Unions
union employee
{
int id;
double salary;
char name[20];
}
// Enumerations 
enum week_days{sun, mon, tues, wed, thur, fri, sat};
int main()
{
enum week_days d;
d = mon;
cout << d;
return 0;
}
//Typedef
typedef <type> <newname>;
typedef float balance;

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:

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

In C++, strings are represented as follows:

string s1;
string s2("hello");
string s3 = "hello";
string s4(s2);
string s5 = s2;
Strings in C++

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 involves creating and using thread objects to carry out sub-tasks.


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 example below, we have the same function names but different data types.

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;
}
Function overloading in C++

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.


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.


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.


Input and Output Operations

C uses printf and scanf for input and output respectively. C++ uses cin and cout.


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.

widget

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 developers recommend starting with C for the following reasons:

  • Basic exercises in C are the same as C++
  • Pointers are error-prone in C, and you can avoid them in C++ for a while
  • Strings are hard 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 familiar 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 is 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.


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. 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. This comprehensive and detailed course will introduce you to all the basic and advanced programming concepts of C language.

Happy learning!


Continue reading about C and C++


WRITTEN BYMaryam Sulemani

Join a community of more than 1.4 million readers. A free, bi-monthly email with a roundup of Educative's top articles and coding tips.