C++ Online Compiler - Run, Test, and Debug Instantly

Write, compile, and run C++ code instantly in your browser with our free online C++ compiler. Perfect for learning, quick testing, and interview preparation — no setup or installation required.

💡 C++ Basics Guide for Beginners

1. Declaring Variables and Constants

C++ requires you to declare the type of each variable. Use const to define read-only values that cannot be changed after initialization.

int age = 30;
double pi = 3.14159;
char grade = 'A';
std::string name = "Alice";
bool isActive = true;

// Constants
const int MAX_USERS = 100;
const std::string COMPANY = "CodeUtility";

// Attempting to modify a const will cause a compile-time error
// MAX_USERS = 200; // ❌ error

2. Conditionals (if / switch)

Use if, else if, else for branching. Use switch for multiple values.

int x = 2;
if (x == 1) {
    std::cout << "One\n";
} else if (x == 2) {
    std::cout << "Two\n";
} else {
    std::cout << "Other\n";
}

// Switch-case
switch (x) {
    case 1:
        std::cout << "One\n";
        break;
    case 2:
        std::cout << "Two\n";
        break;
    default:
        std::cout << "Other\n";
}

3. Loops

Use for, while, and do-while to repeat code blocks.

for (int i = 0; i < 3; i++) {
    std::cout << i << "\n";
}

int n = 3;
while (n > 0) {
    std::cout << n << "\n";
    n--;
}

4. Arrays

Arrays store multiple elements of the same type.

int numbers[3] = {10, 20, 30};
std::cout << numbers[1] << "\n";

5. Array/Vector Manipulation

Use std::vector for dynamic arrays.

#include <vector>

std::vector nums = {1, 2, 3};
nums.push_back(4);
nums.pop_back();

for (int n : nums) {
    std::cout << n << " ";
}

6. Console Input/Output

Use std::cin for input and std::cout for output.

std::string name;
std::cout << "Enter your name: ";
std::cin >> name;
std::cout << "Hello, " << name << "\n";

7. Functions

Functions group reusable logic. Use parameters and return types.

int add(int a, int b) {
    return a + b;
}

std::cout << add(3, 4);

8. Maps

std::map stores key-value pairs like dictionaries.

#include <map>

std::map ages;
ages["Alice"] = 30;
std::cout << ages["Alice"];

9. Exception Handling

Use try and catch to handle runtime errors.

try {
    throw std::runtime_error("Error occurred");
} catch (const std::exception& e) {
    std::cout << e.what();
}

10. File I/O

Use fstream to read from and write to files.

#include <fstream>

std::ofstream out("file.txt");
out << "Hello File";
out.close();

std::ifstream in("file.txt");
std::string line;
getline(in, line);
std::cout << line;
in.close();

11. String Manipulation

C++ strings support methods like length(), substr(), and find().

std::string text = "Hello World";
std::cout << text.length();
std::cout << text.substr(0, 5);
std::cout << text.find("World");

12. Classes & Objects

C++ supports object-oriented programming using classes.

class Person {
  public:
    std::string name;
    Person(std::string n) : name(n) {}
    void greet() { std::cout << "Hi, I'm " << name; }
};

Person p("Alice");
p.greet();

13. Pointers

A pointer stores the memory address of another variable. Use & to get an address, and * to dereference (access the value at the address).

int x = 10;
int* ptr = &x;

std::cout << "Value of x: " << x << "\n";
std::cout << "Address of x: " << ptr << "\n";
std::cout << "Value from pointer: " << *ptr << "\n";

// Modify x through pointer
*ptr = 20;
std::cout << "Updated x: " << x << "\n";