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

C vs C++ Differences

C++ grew from C but added new things like objects, references, and overloading, like a newer model of a car. This lesson compares them.

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 standard input/output (printf, scanf)
#include <stdio.h>
int square(int n) {
	// Send n * n back to the caller
	return n * n;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", square(5));
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d\n", 42);
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
	// Declare x and set it to 5
	int x = 5;
	// Print formatted text to the screen
	printf("%d", x);
	// Declare y and set it to 10
	int y = 10;
	// Print formatted text to the screen
	printf("%d", y);
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
int addInts(int a, int b) {
	// Send a + b back to the caller
	return a + b;
}
// Program execution starts in main()
int main() {
	// Print formatted text to the screen
	printf("%d", addInts(2, 3));
	// Return 0 to signal that the program finished successfully
	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 standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Allocate memory on the heap
	int *ptr = malloc(sizeof(int));
	*ptr = 5;
	// Print formatted text to the screen
	printf("%d", *ptr);
	// Release the heap memory so it can be reused
	free(ptr);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Assuming C code always compiles as C++, when C++ is stricter about things such as void* conversion.
  2. Using printf in C++ where cout is safer for types.
  3. Forgetting that C++ does not allow implicit conversion from void* returned by malloc without a cast.
Chapter Summary
  • Additional C topics include goto, header files, multi-file programs, and static and inline functions.
  • Signal handling, date and time functions, and random numbers add system-level features.
  • Comparing C with C++ shows how the two languages differ.

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.