C/C++ Crash Course

C and C++ are general-purpose programming languages. They are old but mastering them is still great to have. They are hard to replace. We can replace Java, Python, PHP, and NodeJS to Go for back-ends. That was what most startups did. But in the operating system and game development industries, C/C++ is a preferable programming language. Especially C++ is the favorite programming language for competitive programming.

C/C++ is the first programming language that I learned. At that moment, the resources to learn those languages are still limited. I thought they were hard to learn. In this article, I hope we can learn the basics of C and C++. Welcome to the C/C++ crash course.

Prerequisites

Text Editor

I used VS Code here along with the C/C++ extension from Microsoft. You can use another text editor such as Code::Blocks.

Compiler

We need to install compilers like g++ and gcc because C/C++ is a compiled programming language. They are pre-installed for Linux and macOS. For Windows, you can use MinGW. You can use g++ to compile C files. But you will a warning. So, that's why I compile C files with gcc.

Hello World!

Let's get started with Hello World! Write the code below, let's say hello.cpp, and compile it with the command g++ hello.cpp -o hello. The -o is to name the executable file to be hello.

hello.cpp

1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;

int main() {
    cout << "Hello World!" << endl;

    return 0;
}

hello.c

1
2
3
4
5
6
7
#include <stdio.h>

int main() {
    printf("Hello World!\n");

    return 0;
}

Variables

variables.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>

using namespace std;

int main() {
    int a = 0, b = 1, c = 2;

    float pi = 3.141592653589793;
    const double real_pi = 3.141592653589793;

    cout << pi << " " << sizeof(pi) << endl; // 3.14159 4
    cout << real_pi << " " << sizeof(real_pi) << endl; // 3.14159 8

    char hello[] = "Hello";
    string world = "World";
    char exclamation = 33;

    cout << hello << " " << world << exclamation << endl; // Hello World!

    return 0;
}

variables.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>

int main() {
    // int a = 0, b = 1, c = 2;

    float pi = 3.141592653589793;
    const double real_pi = 3.141592653589793;

    printf("%f %ld\n", pi, sizeof(pi)); // 3.141593 4
    printf("%lf %li\n", real_pi, sizeof(real_pi)); // 3.141593 8

    char hello[] = "Hello";
    char world[6] = {'W', 'o', 'r', 'l', 'd', '\0'};
    char exclamation = 33;

    printf("%s %s%c\n", hello, world, exclamation); // Hello World!

    return 0;
}

Input

input.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#include <iostream>

using namespace std;

int main() {
    string name;

    cout << "What's your name? ";
    cin >> name;
    cout << "Hello " << name << "!" << endl;

    return 0;
}

input.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <stdio.h>

int main() {
    char *name;

    printf("What's your name? ");
    scanf("%s", name);
    printf("Hello %s!\n", name);

    return 0;
}

We can get input from a user in the terminal by using cin in C++ and scanf() in C.

Control

control.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <ctime>

using namespace std;

void isEven(int num) {
    if (num % 2 == 0) {
        cout << num << " is even." << endl;
    } else {
        cout << num << " is odd." << endl;
    }
}

void greetings() {
    time_t now = time(0);
    tm *ltm = localtime(&now);

    switch (ltm->tm_hour) {
        case 1 ... 11:
            cout << "Good morning!" << endl;

            break;
        case 12 ... 16:
            cout << "Good afternoon." << endl;

            break;
        default:
            cout << "Good evening." << endl;
    }
}

int main() {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j <= i; j++) {
            cout << "*";
        }
        cout << endl;
    }

    int sum = 1;
    while (sum < 1000) {
        sum += sum;
    }
    cout << sum << endl;
    
    int num;
    cout << "Put a number: ";
    cin >> num;
    isEven(num);

    greetings();

    return 0;
}

control.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <time.h>

void isEven(int num) {
    if (num % 2 == 0) {
        printf("%d is even.\n", num);
    } else {
        printf("%d is odd.\n", num);
    }
}

void greetings() {
    time_t now = time(0);
    struct tm *ltm = localtime(&now);

    switch (ltm->tm_hour) {
        case 1 ... 11:
            printf("Good morning!\n");

            break;
        case 12 ... 16:
            printf("Good afternoon.\n");

            break;
        default:
            printf("Good evening.\n");
    }
}

int main() {
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    int sum = 1;
    while (sum < 1000) {
        sum += sum;
    }
    printf("%d\n", sum);
    
    int num;
    printf("Put a number: ");
    scanf("%d", &num);
    isEven(num);

    greetings();

    return 0;
}

Pointer

pointer.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main() {
    string name = "John Doe";
    string *pointer = &name;

    *pointer = "John Cena";

    cout << name << endl; // John Cena

    // string* p1, p2;
    // p2 = &name;

    return 0;
}

pointer.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#include <stdio.h>

int main() {
    char *name = "John Doe";
    char **pointer = &name;

    *pointer = "John Cena";

    printf("%s\n", name); // John Cena

    return 0;
}

Struct

struct.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

struct person {
    string name;
    int age;
};

int main() {
    person p1;

    p1.name = "John Doe";
    p1.age = 1e1;

    person p2 = {"", 999};
    
    p1.age += 8;

    cout << p1.name << " " << p1.age << endl; // John Doe 18
    cout << p2.name << " is " << p2.age << " years old." << endl; //  is 999 years old.

    return 0;
}

struct.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

struct person {
    char *name;
    int age;
};

int main() {
    struct person p1;

    p1.name = "John Doe";
    p1.age = 1e1;

    struct person p2 = {"", 999};
    
    p1.age += 8;

    printf("%s %d\n", p1.name, p1.age); // John Doe 18
    printf("%s is %d years old.\n", p2.name, p2.age); //  is 999 years old.

    return 0;
}

In C and Go, we can create an object from the struct. But in C++, there is a syntax for that, class. We will cover that later.

Union

union.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

union person {
    char initial;
    int age;
};

int main() {
    person p1;

    p1.initial = 'J';
    p1.age = 1e1;

    person p2 = {.age = 999};

    p1.age += 8;

    cout << p1.initial << " " << p1.age << endl; //  18
    cout << p2.initial << " is " << p2.age << " years old." << endl; // � is 999 years old.

    return 0;
}

union.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

union person {
    char initial;
    int age;
};

int main() {
    union person p1;

    p1.initial = 'J';
    p1.age = 1e1;

    union person p2 = {.age = 999};
    
    p1.age += 8;

    printf("%c %d\n", p1.initial, p1.age); //  18
    printf("%c is %d years old.\n", p2.initial, p2.age); // � is 999 years old.

    return 0;
}

Enum

enum.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>

using namespace std;

enum Color {
    RED,
    GREEN,
    BLUE
};

int main() {
    int number;
    cout << "Input a number between 0-2: ";
    cin >> number;

    Color color = static_cast<Color>(number);

    switch (color) {
        case RED:
            cout << "Red" << endl;
            break;
        case GREEN:
            cout << "Green" << endl;
            break;
        case BLUE:
            cout << "Blue" << endl;
            break;
        default:
            cout << "Undefined" << endl;
    }

    return 0;
}

enum.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <stdio.h>

enum Color {
    RED,
    GREEN,
    BLUE
};

int main() {
    int number;
    printf("Input a number between 0-2: ");
    scanf("%d", &number);

    switch (number) {
        case RED:
            printf("Red\n");
            break;
        case GREEN:
            printf("Green\n");
            break;
        case BLUE:
            printf("Blue\n");
            break;
        default:
            printf("Undefined\n");
            break;
    }

    return 0;
}

Map and Set

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <map>
#include <set>

using namespace std;

struct location {
    float latitude, longitude;
};

int main() {
    map<string, location> offices;

    location apple = {37.33199, -122.03089};
    location google = {37.42202, -122.08408};

    offices["Apple"] = apple;
    offices["Google"] = google;

    set<int> uniqueNumbers;

    uniqueNumbers.insert(1);
    uniqueNumbers.insert(2);
    uniqueNumbers.insert(3);

    for (auto num : uniqueNumbers) {
        cout << num << endl;
    }

    cout << uniqueNumbers.count(4) << endl; // 0
    
    return 0;
}

There are no built-in Map and Set in C. We will cover that later. There is only Map in Go, but it is enough because a Set can be built on Map.

Vector

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<string> hello;

    hello.assign(1, "Hello");
    hello.push_back("World!");

    for (auto i = hello.begin(); i != hello.end(); i++) {
        cout << *i << " ";
    }

    return 0;
}

A vector is just like an array in C++. Most C++ developers are more convenient with vectors rather than real arrays.

You can find the completed source code of these examples on https://github.com/aristorinjuang/c-cpp-crash-course. There is a Makefile. That is a shortcut to compile and run those programs. We can run it by typing make <target> like make hello.

Related Articles