CodeNewbie Community 🌱

Cover image for C++ Hello World
Tristan
Tristan

Posted on

C++ Hello World

Introduction

  • This series is going to be dedicated to my reading of the book, C++ programming principles and practice by Bjarne Stroustrup. The resources I used to create this post can be found on ticketnote or HERE and in C++ programming principles and practice by Bjarne Stroustrup.

Getting started

  • I first want to point out that some syntax in C++ programming principles and practice is a little our of date but the underlying principles taught in it are tried and true. Also, Bjarne Stroustrup is a world class computer scientist and he invented C++, so there is literally no other person more qualified to write a book about programming in C++.

  • I also want to point out that if you need to set up your code editor or are unsure how to create a new file, then I would recommend following this tutorial HERE

The code

  • I am just going to paste the code down below and then we will walk through the code line by line.
//this program outputs the message "Hello, World" to the screen
#include <iostream>

int main()
{
    std::cout << "Hello, World!";
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

The comment

  • Now, //this program outputs the message "Hello, World" to the screen is considered a comment. A comment is indicated by two slashes //, comments are ignored by the compiler and are written for the benefit of the programmers who read the code. The first line of a program is typically a comment that tells the human reader what the program is supposed to do.

#include < iostream >

  • #include is how we instruct the compiler to include a library and in our case we are instructing the compiler to include the iostream library. The iostream library is part of the standard C++ library that deals with basic input and output. This library is used to get input from the keyboard and the output data to the console.
  • so with #include < iostream > we are instructing the compiler to include the iostream library that will allow us to output data to the screen.

int main()

  • This marks the start of our function, a function is basically an named sequence of instructions for the computer to execute. A function has 4 parts to it:

1) Return type : specifies what type the function will return. For us we specified a return type of int meaning integer and it is the reason we return 0.

2) A name : simply the name of the function and for us the name is main (more on main later).

3) A parameter list : indicated by enclosed parentheses (), ours is empty to signify that this function takes no extra values when called.

4) A function body : a function body is everything that is enclosed within curly brackets {} and holds all the operations for the the function to perform.

  • In this case the name of the function is very important. In every C++ program there must be a function called main. It will act as a starting point of execution for our project.

std::cout << "Hello, World!";

  • the std prefix is a way to indicate that we are using the std namespace. Generally in programming a namespace is used to promote encapsulation and organization within a program. With std:: we are saying that we are going to use the std namespace.
  • cout pronounced see-out is the part of the iostream library that we use to send data to the console for display.
  • We then use the output operator << to display a string of characters to the console. You can think of cout as the actual console and we use << to send data to the console.
  • In C++ a string is determined by double quotes "Hello, World!" that is how we know Hello, World! is a string.
  • Notice that each line ends with a semicolon ;. Without it our program will not compile.

return 0;

  • lastly we have the return statement which is used to return the type that our function declared in its return type. Note, when we return something, it must match the return type of the function.

Conclusion

  • Thank you for taking the time out of your day to read this blog post of mine. If you have any questions or concerns please comment below or reach out to me on Twitter.

Top comments (0)