Back
Close

Hello World in C++, the long way

Matthew-S
33.3K views

The simplest program

No, this is actually not hello world. The simplest program actually only returns an exit code (like described in the previous step).


int main()
{
   return 42;
}

This code declares a function whose name is main, who returns an integer (in C++, called int), and will always return the number 42. Whenever your computer compiles code into an executable, it looks for a function with the name main and will make sure to call that to start the program. That return value is the exit code. Let's try running this program. Linux:

$ ./program
$ echo $?
42

There we go! In only four lines of C++ we've already answered the ultimate question of life, the universe and everything. Now let's take it a step further.


int main()
{
    int theAnswer = 42;
    return theAnswer;
}

This declares an integer (whole numbers) that holds the value 42 and then returns it.


int main()
{
    int sum = 0;
    sum += 1;
    sum += 2;
    sum += 3;
    return sum;

Returns 6. sum += 1 is the same as sum = sum + 1.


int main()
{
    int sum = 0;

    int i = 1;
    while (i <= 3)
    {
        sum += i;
	++i;
    }

    return sum;
}

Same exact thing. For the most part, ++i is the same as i++ which is also the same as i += 1. In this, the variable i increases by 1, starting at 1 and ending at 3. The while will keep running the code between { and } as long as i <= 3 is true. The moment i is 4, it skips pas the brackets and continues to return sum;.


int main()
{
    int sum = 0;

    for (int i = 1; i <= 3; ++i)
    {
        sum += i;
    }

    return sum;
}

Yet again, this does the same thing. for is almost like an enhanced while loop that runs int i = 1 in the beginning, once, will loop between { and } as long as i <= 3 is true, and will run ++i at the end of every loop.


int main()
{
    int myVar = 7;
    int anotherVar = 3;
    return myVar + anotherVar;
}

The exit code for this will always be 10.


int main()
{
    int arr[2];
    arr[0] = 7;
    arr[1] = 3;
    return arr[0] + arr[1];
}

Exactly the same as above but declaring an array of size 2 instead of two separate variables. An array is a group of variables. Each variable is at a particular position or index. The index number always starts at 0 in C++ (and most prgramming languages). in the above example, since the array only has 2 values in it, and the first value is at index 0, the second or last index lies at 1. Therefore, arrays start at 0 and end at size - 1 where size is how large the array is.


int main()
{
    int arr[2] = {7, 3};
    return arr[0] + arr[1];
}

Same thing.


int main()
{
    int arr[] = {7, 3};
    int sum = 0;
    
    sum += arr[0];
    sum += arr[1];

    return sum;
}

Same thing. The 2 that used to be inside the [] will be inferred since there are 2 values between the { and }.


int main()
{
    const int arrSize = 2;
    int arr[arrSize] = {7, 3};
    int sum = 0;

    for (int i = 0; i < arrSize; ++i)
    {
        sum += arr[i];
    }
    
    return sum;
}

This uses a for loop as explained above to to the same thing. const means arrSize must always stay as 2. This is necessary because the compiler has to exactly how large the array, arr is. By declaring it as const, the compiler can pretty much replace all occurances of arrSize with 2 so it becomes int arr[2] = .... Now if we want to add up more numbers:


int main()
{
    const int arrSize = 5;
    int arr[arrSize] = {7, 3, 2, 8, 12};
    int sum = 0;

    for (int i = 0; i < arrSize; ++i)
    {
        sum += arr[i];
    }

    return sum;
}

Notice, all we had to do was change arrSize to 5 and add 3 more numbers. Finally, to write the same thing more concisely, we could do:


int main()
{
    int arr[] = {7, 3, 2, 8, 12};
    int sum = 0;

    for (int val : arr)
        sum += val;
    
    retur sum;
}

for (int val : arr) will loop the code below once for every value inside the array, arr. The first time it runs, val holds 7. The next time, 3, and so on. When there is only one line of code, the { and } can be omitted. However, be careful because the compiler does not care about indentation; even if two lines below for are indented, if there are no brackets around it, the compiler will only run the first one in the for loop.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io