C is the "minimalist" foundational language, while C++ is the "feature-rich" extension of it.
C (Procedural): Focuses on functions and steps. It is designed to be close to the hardware, simple, and fast. It is often called "portable assembly."
C++ (Multi-paradigm): Focuses on objects and data. It includes everything in C but adds Object-Oriented Programming (OOP), allowing for better organization of large, complex projects.
Both languages give you direct control over the computer's memory (RAM), which is why they are so fast. However, they handle it differently:
In C: You use pointers to manually "grab" memory using malloc() and must remember to "give it back" using free(). If you forget, you get a memory leak.
In C++: You can still do it the C way, but you also have RAII (Resource Acquisition Is Initialization). When an object is destroyed, its "destructor" automatically cleans up its own memory. Modern C++ also uses Smart Pointers (std::unique_ptr), which handle cleanup for you automatically.
Use C for:
Operating Systems: The Linux kernel, Windows, and macOS cores are written primarily in C.
Embedded Systems: Microchips in cars, microwaves, and medical devices.
Hardware Drivers: Communicating directly with graphics cards or printers.
Use C++ for:
Game Development: Most major game engines (Unreal Engine) and high-performance games are C++.
Browsers: The "engines" behind Google Chrome and Firefox.
Finance: High-frequency trading platforms where microseconds matter.
AI & Graphics: Photoshop, Maya, and the back-end of heavy AI frameworks.
#include <stdio.h>
#include <stdlib.h>
void multiplyAndPrint(double a, double b) {
double result = a * b;
// 1. Output to Display Screen (stdout)
printf("\n--- Result ---\n");
printf("Input 1: %.2f\n", a);
printf("Input 2: %.2f\n", b);
printf("Product: %.2f\n", result);
// 2. Output to Printer
// Note: "stdprn" is an old DOS constant. In modern systems,
// we open the printer port as a file.
// Windows: "LPT1" or "PRN" | Linux: "/dev/lp0"
FILE *printer = fopen("LPT1", "w");
if (printer == NULL) {
printf("\n[Error] Could not connect to printer (LPT1).\n");
} else {
fprintf(printer, "Calculation Report\n");
fprintf(printer, "Result: %.2f * %.2f = %.2f\n", a, b, result);
fprintf(printer, "\f"); // Sends a form-feed to eject the paper
fclose(printer);
printf("[Success] Data sent to printer.\n");
}
}
int main() {
double num1, num2;
printf("Enter first number: ");
scanf("%lf", &num1);
printf("Enter second number: ");
scanf("%lf", &num2);
multiplyAndPrint(num1, num2);
return 0;
}
Code snippet (c++) to accept 2 numeric inputs, store them, multiple them, display the results on screen and send it to printer:
#include <iostream>
#include <fstream> // Required for printer/file output
void calculateAndOutput() {
double num1, num2;
// Input
std::cout << "Enter the first numeric value: ";
std::cin >> num1;
std::cout << "Enter the second numeric value: ";
std::cin >> num2;
double result = num1 * num2;
// 1. Output to Display Screen
std::cout << "\n--- Calculation Result ---\n";
std::cout << num1 << " multiplied by " << num2 << " is: " << result << std::endl;
// 2. Output to Printer
// On Windows, "PRN" or "LPT1" are virtual files for the default printer.
// On Linux, use "/dev/usb/lp0" or similar.
std::ofstream printer("PRN");
if (printer.is_open()) {
printer << "C++ Printing Routine\n";
printer << "---------------------\n";
printer << "Input A: " << num1 << "\n";
printer << "Input B: " << num2 << "\n";
printer << "Result : " << result << "\n";
printer << "\f"; // Form feed to eject the page
printer.close();
std::cout << "Success: Data sent to the printer device.\n";
} else {
std::cerr << "Error: Printer not found or access denied.\n";
}
}
int main() {
calculateAndOutput();
return 0;
}