DEV Community

Cover image for Cracking the Code: Decoding the 'Hello World' Program in C++"
Tina Popli
Tina Popli

Posted on • Edited on

Cracking the Code: Decoding the 'Hello World' Program in C++"

The C++ Programming language was developed at AT&T Bell Laboratories in the early 1980s. The name C++ was coined by Rick Mascitti where "++" stands for the C increment operator.

Let's code!

A FIRST LOOK AT C++ PROGRAMMING

Let us start with a basic, common and a popular example of a C++ Program that prints a string on the screen.

//My First C++ Program
#include <iostream>

int main()
{
    std::cout << "Hello World";
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Let us have a look at all these elements one-by-one:

(i) //My first C++ Program
A Comment in C++ starts from "//". Compiler does not execute or compile these statements. These are generally included for the programmers to explain the logic or give a short explanation.

(ii) #include < iostream >
The line #include is like a special instruction in C++. It tells the compiler to include a bunch of useful tools for input and output operations from a file called . This file is part of the C++ Standard Library, which is like a collection of pre-made code that we can use in our programs. Inside , there are things like std::cin for getting input from the user, and std::cout for displaying output on the screen. If we don't include , the compiler won't know about std::cout and will give us an error if we try to use it. So it's important to include to make sure we can use std::cout and other useful features for input and output.

(iii) int main()
This is used as the entry point of any program. The content of main() function is where the execution begins from. It is essential for a C++ Program to have a main() function.

(iv) cout<<"Hello World";
This is the most important element of the program in order to print the string on the screen. It is like a command in C++ that tells the computer to display the message "Hello World" on the screen. In this line, std::cout is a special object that represents the standard output stream, which is basically a way to send information to the console. It's like a communication channel between our program and the screen.

The << operator is used to send the message "Hello World" to std::cout. It's like putting the message into the communication channel.

So, when the program executes this line, it takes the message "Hello World" and sends it to std::cout. Then, std::cout displays the message on the screen for us to see.

This way, we can use std::cout and the << operator to print out any messages or information we want to show to the user while our program is running.

(v) return 0
The return statement instructs the main() function to stop and return a value. Here, it is returning 0(zero) which means the program has terminated normally without any errors during the execution.

Some NOTES to keep in mind:

(a) Every executable statement in C++ ends with a semicolon ";".
(b) Every C++ program must have a main() function, without a main() function, the program would not have a designated starting point, and the compiler would not know where to begin the execution. Therefore, having a main() function is a fundamental requirement for a valid C++ program.
(c) Comments can be made using "/..../" as well, this type of syntax is usually used at the time of multiline or block comments. For single line comments, we usually use "//.."
(d) A Program in C++ usually includes a header file "iostream.h" for standard stream input/output facilities.

TIME FOR SOME EXERCISE, ARE YOU READY?

#include <iostream>

int main()
{
    int num1, num2;
    std::cout << "Enter 2 numbers: ";
    std::cin >> num1 >> num2;

    // Sum of 2 numbers
    int sum = num1 + num2;
    std::cout << "Sum = " << sum << std::endl;

    // Continue for Product of 2 numbers

    // Continue for Subtraction of 2 numbers

    // Continue for Division of 2 numbers

    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Drop your snippets in the comments section below, looking forward!! :)
Any questions? Drop them in the comments section below, Let's discuss! :)

Top comments (4)

Collapse
 
pauljlucas profile image
Info Comment hidden by post author - thread only accessible via permalink
Paul J. Lucas
  1. The .h extension has been deprecated. Should be just <iostream>.
  2. Your sample program won't compile. (You apparently didn't actually try it.) Why not is left as an exercise.
  3. You forgot the terminating newline in the output.
  4. You don't explain what << actually does.
  5. For main() only, return 0 is optional.
  6. For note (b), if you don't include main(), you can't even link your program into an executable. You can't even get to where it "does not begin."

Mistakes aside, this information is already widely available. What did you bring new to the table by writing this?

Collapse
 
tina_popli profile image
Tina Popli

Thanks for your review, i have edited it accordingly :)

Collapse
 
pauljlucas profile image
Paul J. Lucas

Now your indentation is wrong. It's like you don't even look at your own post to see how it looks.

Thread Thread
 
tina_popli profile image
Tina Popli

I believe it's indented now.

Some comments have been hidden by the post's author - find out more