← Back to C Course | Chapter 14: Additional Topics | Lesson 9 of 9

C vs C++ Differences

Procedural vs. Object-Oriented

C is a procedural language built entirely around functions operating on data step by step, while C++ was designed as an extension of C that layers object-oriented programming on top, letting you bundle data and the functions that operate on it together into classes and objects.

Example: Procedural vs. Object-Oriented

c
#include <stdio.h>
int square(int n) {
	return n * n;
}
int main() {
	printf("%d", square(5));
	return 0;
}

Standard I/O Differences

C relies on printf() and scanf() from stdio.h for all console input and output, using format specifiers like %d and %s to interpret the data, whereas C++ additionally offers the stream-based std::cout and std::cin from iostream, which use the << and >> operators instead of format strings.

Example: Standard I/O Differences

c
#include <stdio.h>
int main() {
	printf("%d\n", 42);
	return 0;
}

Variable Declaration Rules

Strict older C standards (like C89) required every variable in a function to be declared at the very top of its block before any executable statements, whereas C++ (and modern C standards like C99 and later) both relax this rule, letting you declare a variable right where you first need it.

Example: Variable Declaration Rules

c
#include <stdio.h>
int main() {
	int x = 5;
	printf("%d", x);
	int y = 10;
	printf("%d", y);
	return 0;
}

Function Overloading

C requires every function in a file to have a unique name, since it has no mechanism to tell two same-named functions apart, while C++ supports function overloading, letting you define several functions that share a name as long as their parameter lists differ in type or count.

Example: Function Overloading

c
#include <stdio.h>
int addInts(int a, int b) {
	return a + b;
}
int main() {
	printf("%d", addInts(2, 3));
	return 0;
}

Memory Management

C manages dynamic memory through explicit library function calls — malloc() to allocate and free() to release — while C++ additionally provides the new and delete operators, which combine memory allocation with automatically calling a class's constructor or destructor at the same time.

Example: Memory Management

c
#include <stdio.h>
#include <stdlib.h>
int main() {
	int *ptr = malloc(sizeof(int));
	*ptr = 5;
	printf("%d", *ptr);
	free(ptr);
	return 0;
}

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.