Programming with C & C++
C is a procedural programming language. C++ is an extension of C that supports Object-Oriented Programming (OOP).
C Program Structure #include <stdio.h>
int main() { printf("Hello, World!\n"); return 0; } Data Types in C TypeSizeExample int4 bytesint a = 5; float4 bytesfloat b = 3.14; double8 bytesdouble c = 3.14159; char1 bytechar d = 'A'; Control Statements // if-else if (a > 0) { printf("Positive"); } else { printf("Negative"); }
// for loop for (int i = 0; i < 5; i++) { printf("%d\n", i); }
// while loop while (a > 0) { a--; } Functions in C int add(int x, int y) { return x + y; } int main() { int result = add(3, 4); // result = 7 return 0; } C++ & OOP Concepts
class Animal { public: string name; void speak() { cout << "Sound!" << endl; } };
class Dog : public Animal { public: void speak() { cout << "Woof!" << endl; } };