DEV Community

Cover image for C++ Hello World
Tristan Elliott
Tristan Elliott

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 (6)

Collapse
 
thefluxapex profile image
Ian Pride

A quick note:
You'll usually want to add a newline on output so either

std::cout << "Hi all!\n";
Enter fullscreen mode Exit fullscreen mode
std::cout << "Hi all!" << '\n';
Enter fullscreen mode Exit fullscreen mode

or

std::cout << "Hi all!" << std::endl;
Enter fullscreen mode Exit fullscreen mode

and depending on your compiler/environment the return 0 is not always needed, but I recommend always adding it anyway, especially if you are developing cross-compiler/cross-platform.

Collapse
 
theplebdev profile image
Tristan Elliott

Awesome, I had no idea. Thank you :)

Collapse
 
thefluxapex profile image
Ian Pride • Edited

You're welcome, also, a newline will usually (but not always) flush the buffer, printing all output to the console. Forcing the buffer to flush can be achieved with std::flush, but when you need to print a newline AND need to force the buffer to flush you have the 2-in-1 command (as shown above) std::endl which is equivalent to '\n' << std::flush.

Thread Thread
 
theplebdev profile image
Tristan Elliott

ok, Awesome. Can you explain the concept of a buffer to me? I have read a little about it when dealing with streams in java but I still can't wrap my head around it.

Thread Thread
 
thefluxapex profile image
Ian Pride • Edited

Well, I won't get deep into it, but it's sort of just like a buffer for video when you watch anything that streams to you, it is the place that stores the data before it is displayed, with video it's video data (still just data as far as a machine is concerned), here we are speaking about string or character data (text, from cout, to cin etc...).

In video data if the content didn't come to you and be stored in the buffer first it would continuously stutter showing one or few frames at a time before it had to download the next set of frames. The buffer here is what is put into cout in the stream and unless you send a newline (in most cases, but not always automatically flushing) or flush the buffer, it may not display when you want it to. It really depends on a variety of things mostly depending on what you're doing.

There is more to it than this, of course.

So TLDR;

It is the place where the string or character data for a stream is stored before it is displayed; buffering the data.

Thread Thread
 
theplebdev profile image
Tristan Elliott

Ok, that actually helps a lot. Thank you again. You have been a huge help!