C is the foundation for C++. While C++ is more widely used for desktop apps and games, it is an excellent option to start with C, especially if you are new to the software world. Read on to know why.

What is C?

C is a high-level structural programming language. Programs written in C are portable. C is still one of the top programming languages today because it is robust. It is used for complex programs like embedded systems, drivers, kernels, system applications, operating systems like Microsoft Windows, Apple OS X, databases like MySQL, and some IoT applications.

C is a compiled language, thus providing a layer of abstraction between the machine code and the program.

A simple C program to add two numbers will look like this.

#include<stdio.h>
int main()
{
int a, b, sum;
printf("Enter two numbers to add: ");
scanf("%d%d", &a, &b);
sum = a + b;
printf("\nSum of %d and %d is %d", a, b, sum);
return 0;
}

Here is the output:

Output of the example C program

C programs include stdio.h – the standard input output.

STDIO provides basic input-output functions like printf and scanf. printf is used to print something (message, output) on the console, while scanf is used to take inputs from the console. We are using %d to indicate that the numbers are integers.

If you give decimals, you will get some weird answers. To avoid this, you can use %f. The main() function is the first function that is executed when the program runs. The syntax is very simple – declare the variable types and then use them.

What is C++?

C++ is based on object-oriented programming principles like abstraction, polymorphism, inheritance, and encapsulation. You can think of C++ as an extension of C with the concept of classes and objects.

Having objects to store data gave a neat structure to the programs. For example, if you want to store details of a student, you can create a Student class and create attributes like name, age, hobbies, marks etc., under the class. You can create a real student object whenever required!

class Student {
public: 
char name[20];
int age;
float marks;
};

//This will create an object
Student student1 = new Student();

In reality, the object will be created and memory allocated only during runtime.

C++ provides high performance, which is why it is the most popular choice even today for developing high-performance game engines, embedded systems, browsers, compilers, and graphics-based applications like image processing.

Few databases like MongoDB are written in C++. Just like C, C++ is portable.

Let’s write our previous addition program in C++ – notice the different functions.

#include <iostream>
using namespace std;
int main() {
int a, b;
cout << "Enter the numbers: "; 
cin >> a >> b;
int sum = a + b;
cout << a << " + " << b << " = " << sum;
return 0;
}

Note that we are using cout and cin instead of printf and scanf. Also, type declarations can be done anywhere in the program before the variable is used (for example, the variable sum). The print statement is quite simple with just the variable names. Note that we are using the namespace std from the iostream header. std has the methods like cout, in, and many more.

Similarities between C and C++

You can say that C is a subset of C++. There are many similarities between C and C++, be it in the way programs are written or the applications that they are used for. Both are robust, portable, and highly performant. Some important similarities are:

Sno.FeatureExplanation
1.SyntaxBoth have the same syntax, for example, variable declaration, end of line semi-colon, naming conventions, etc.
2.Structural and proceduralEach line of code is executed one by one. The programs are structured as follows – first the imports, then variable declarations, and then the main code.
3.Main() functionAll the code that needs to be executed should be inside the main() function. main() is the first function call during program execution.
4.PointersBoth C and C++ use pointers in the same way. A pointer is a variable that stores the memory address of another variable. For example, int a = 1;. As soon as this code is executed, a memory (say, XX0011) will be allocated for a. The memory location of ‘a’ can be accessed by using the ampersand (&) as int ptr_a = &a;
5.Keywords and operatorsAll the keywords and operators present in C are valid for C++ as well. For example, scope, static, public, int, etc. C++ has additional operators and keywords too.
Similarities between C and C++

Differences between C and C++

C++ was created to overcome some of the shortcomings of C and is a superset of C. So, any program written in C will work in C++ – but not vice versa! The main difference between C and C++ is that C++ is based on object-oriented principles (OOP) of programming. Also, there is more emphasis on type checking in C++. There are also a few more subtle differences as listed below:

CC++
Was developed between 1969-1973 by Dennis Ritchie at AT&T Bell labsDeveloped by Bjarne Stroustrup in 1979.
Does not follow object-oriented programming principlesBased on the OOPS concepts, like encapsulation, polymorphism, and inheritance
C contains a total of 32 keywords like char, switch, int, static, union, and othersAll the C keywords are valid in C++, and 31 additional keywords are also present.
Supports only procedural programmingC++ supports multiple programming paradigms, like OOP, generic and functional programming
We cannot implement features of OOP in C.Features like friends, virtual functions in C++ enhance the essence of OOP.
C supports built-in data types.C++ support both built-in and user-defined data types through the concept of classes
There is provision for operator or function overloadingC++ supports both operator and function overloading (polymorphism)
Memory allocation is done through malloc() and calloc() functions, and deallocation using free()Memory allocation happens using new operator, and deallocation using delete operator
C doesn’t support exception handlingSupports exception handling
Focuses on the procedure or method more than dataMore focused on data
Differences between C and C++

Some other important features present only in C++ are:

  • Using namespace keyword, we can create variables of the same name in different namespaces.
  • We can use functions inside a structure. Structures can also have access modifiers.
  • Supports reference variables.

When to use C or C++

This is a very common debate amongst programmers – why should I learn C when I can do everything in C++?

Learning C will give you a solid foundation on data structures, pointers, keywords, concepts of stack, heap, and memory allocation.

Besides that, C is still widely used for high-performance apps, as the C compiler is faster than the C++ compiler. So, if you want to write chunks of code that do not require objects and classes, virtual functions, or templates, go for C because C++ might be overkill with its extensive libraries.

Most low-level coding like kernels, operating systems, and databases are still maintained in C, so knowing C will also help you learn C++ faster.

C++ is considered one of the fastest and most efficient languages – which is why it is still one of the top programming languages, especially for high-performance applications like game engines, IoT devices, and desktop apps. Many applications use a combination of C and C++ code – to achieve optimum performance and the benefit of object-oriented programming.

Summary

In this article, we learned the basics of C and C++ with a simple example program. We discussed the main differences and similarities between both languages and when to use each.

If you are just beginning your software development journey, starting with C will give you a confidence boost, as it is easy and covers all the programming concepts, like data structures, pointers, memory, and so on.

You may be interested in using Geekflare’s online C Compiler and C++ Compiler.