Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟢 Foundation  ·  Lesson 02

C vs C++ – 15 Key Differences

In this lesson: Difference between C and C++ — procedural vs OOP, printf vs cout, malloc vs new, struct vs class — table and code.

C and C++ in one paragraph

C was created by Dennis Ritchie at Bell Labs in 1972 to write the UNIX operating system. It is small, fast and close to the hardware. C++ was created by Bjarne Stroustrup, starting in 1979, as "C with Classes": it keeps nearly all of C and adds object-oriented programming and many safety and productivity features. C++ is standardised by ISO; major versions are C++98, C++11, C++14, C++17, C++20 and C++23.

Think of C++ as C plus a large toolbox. Almost everything you know in C still works, but C++ usually offers a safer, shorter way to do it.

Difference between C and C++ (table)

#PointCC++
1ParadigmProceduralMulti-paradigm: procedural + object-oriented + generic
2Classes and objectsNo (only struct with data)Yes — classes with data and functions
3Encapsulation / data hidingNo access controlprivate, protected, public
4Inheritance and polymorphismNot supportedSupported (including virtual functions)
5Input / outputprintf(), scanf() in <stdio.h>cout, cin in <iostream> (C functions still work)
6Dynamic memorymalloc(), calloc(), free()new, delete; smart pointers
7Function overloadingNot allowedAllowed
8Default argumentsNoYes
9ReferencesNo (pointers only)Yes: int& r = x;
10NamespacesNoYes: std::, your own
11Exception handlingNo (error codes)try, catch, throw
12Templates / generic codeNo (macros only)Function and class templates
13Standard libraryC library (string.h, math.h …)C library + STL: vector, map, sort…
14String typechar arraysstd::string class
15File extension.c.cpp, .cc, .cxx

Side by side: input and output

C++
/* C */
#include <stdio.h>
int main(void) {
    int age;
    printf("Enter age: ");
    scanf("%d", &age);
    printf("Next year you will be %d\n", age + 1);
    return 0;
}
C++
// C++
#include <iostream>
int main() {
    int age;
    std::cout << "Enter age: ";
    std::cin >> age;
    std::cout << "Next year you will be " << age + 1 << "\n";
    return 0;
}

C++ streams know the type of each variable, so there are no format specifiers like %d to get wrong, and no & needed for input.

Side by side: dynamic memory

C++
/* C */
int *marks = (int *) malloc(5 * sizeof(int));
/* ... */
free(marks);
C++
// C++ (classic)
int* marks = new int[5];
// ...
delete[] marks;

// C++ (modern — no manual free at all)
std::vector<int> marks(5);

new calls constructors and needs no cast or sizeof. Modern C++ goes further: containers and smart pointers free memory automatically.

struct in C vs class in C++

C++
/* C: data only; functions live outside */
struct Student { char name[30]; int marks; };
void print(struct Student s) { printf("%s %d\n", s.name, s.marks); }
C++
// C++: data and behaviour together, with access control
class Student {
    std::string name;
    int marks;
public:
    Student(std::string n, int m) : name(n), marks(m) {}
    void print() const { std::cout << name << " " << marks << "\n"; }
};

In C++, a struct can also have member functions; the only difference from class is that members are public by default.

Overloading and references

C++
// Only possible in C++
int area(int side)            { return side * side; }
int area(int l, int b)        { return l * b; }
void addBonus(int& marks)     { marks += 5; }   // reference: no * or &x needed

int m = 70;
addBonus(m);                  // m becomes 75

In C you would need two function names (area_square, area_rect) and a pointer parameter (void addBonus(int *m) called as addBonus(&m)).

What is the same in both

  • Basic data types (int, char, float, double), operators and expressions.
  • if, switch, for, while, do-while, break, continue.
  • Arrays, pointers and pointer arithmetic.
  • Functions, recursion and the preprocessor (#include, #define).
  • The C standard library, available in C++ as <cstdio>, <cmath>, <cstring> and so on.

Which one to use?

Use C when…Use C++ when…
Writing very small embedded/firmware code or an OS kernelBuilding larger applications, games, desktop software
The platform only has a C compilerYou want classes, STL containers and algorithms
You are studying how memory and hardware work at the lowest levelYour syllabus (CBSE Class 11–12 CS, many B.Tech courses) uses C++

Common misconceptions

  • "C++ is just C with classes" — modern C++ also has templates, the STL, lambdas, smart pointers and much more.
  • "C code always compiles in C++" — not if it uses C++ keywords as names or relies on implicit void* conversion.
  • "cout is slower, so C++ is slower" — I/O speed depends on settings like std::ios::sync_with_stdio(false); computation speed is the same.
  • "struct in C++ cannot have functions" — it can.
🏋️ Practice

Write a program that stores 5 students' names and marks. First write it in C using struct, char[] and printf. Then rewrite it in C++ using a class, std::string, std::vector and cout. Count how many lines each version needs and note which one was easier to get right.

Summary

  • C is procedural; C++ adds OOP, generic programming and many safety features.
  • C++ has classes, references, overloading, namespaces, exceptions, templates and the STL.
  • printf/scanf → cout/cin; malloc/free → new/delete or, better, containers and smart pointers.
  • Most C code is valid C++, but not all.
  • For the same algorithm, both are equally fast.

Related: introduction to C++, OOP concepts, C tutorial: C vs C++.

Frequently Asked Questions

What is the main difference between C and C++?
C is a procedural language: programs are built from functions that act on data. C++ keeps almost all of C but adds object-oriented programming (classes, inheritance, polymorphism) plus features such as references, function overloading, templates, exceptions, namespaces and the Standard Template Library.
Can a C program run as a C++ program?
Most C code compiles as C++, but not all. C++ is stricter: for example, assigning the void* returned by malloc to an int* needs a cast in C++, and names like new, class or delete are keywords in C++, so C code using them as variable names will fail.
Which is faster, C or C++?
For the same algorithm both compile to similarly fast machine code. C++ features like templates and inline functions often cost nothing at run time. Speed differences usually come from how the program is written, not from the language choice.
Should I learn C before C++?
It is not required. Learning C first teaches memory, pointers and how the machine works, which helps later. Starting directly with modern C++ (using std::string, std::vector and classes) is also fine, and many schools teach C++ first in Class 11–12.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicC++ Setup: GCC, MinGW, VS Code and Online Compiler Your progress: 2 / 81 (2%) · Lesson 3 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free