C/C++/Objective-C Examples

Syntax highlighting for C, C++, and Objective-C

C Example

Classic C with structs, pointers, and memory management:

#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 #define PI 3.14159 typedef struct { char name[50]; int age; float salary; } Employee; // Function to calculate circle area float calculate_area(float radius) { return PI * radius * radius; } int main(int argc, char *argv[]) { Employee emp; int numbers[MAX_SIZE]; // Initialize employee strcpy(emp.name, "John Doe"); emp.age = 30; emp.salary = 50000.00; printf("Employee: %s\n", emp.name); printf("Age: %d\n", emp.age); printf("Salary: %.2f\n", emp.salary); // Array operations for (int i = 0; i < 10; i++) { numbers[i] = i * 2; } // Pointer example int *ptr = numbers; printf("First element: %d\n", *ptr); // Dynamic memory int *dynamic = (int*)malloc(sizeof(int) * 10); if (dynamic != NULL) { dynamic[0] = 42; free(dynamic); } return 0; }

C++ Example

Modern C++ with classes, templates, and STL:

#include <iostream> #include <vector> #include <string> #include <algorithm> class Calculator { private: std::vector<double> history; public: Calculator() : history() {} double add(double a, double b) { double result = a + b; history.push_back(result); return result; } double multiply(double a, double b) { return a * b; } void printHistory() const { std::cout << "History:" << std::endl; for (const auto& value : history) { std::cout << value << std::endl; } } }; template<typename T> T max(T a, T b) { return (a > b) ? a : b; } int main() { Calculator calc; std::cout << "Sum: " << calc.add(5, 3) << std::endl; std::cout << "Product: " << calc.multiply(4, 7) << std::endl; calc.printHistory(); // STL example std::vector<int> numbers = {5, 2, 8, 1, 9}; std::sort(numbers.begin(), numbers.end()); return 0; }