The Structure type of any programming language (for instance, in our case, we’ll be learning C++ structure) is the first thing to learn in any programming language; similarly, the “Hello, World!” program is also the first program in any programming language. We will discuss each line of this program in detail to understand it more profoundly and well.
Let’s check how C++ “Hello, World!” program works.
If you haven’t set up the environment to run C++ on your device, visit Installing VS code on Your device
First C++ Program
// Your First C++ Program
#include <iostream>
using namespace std;
int main() {
cout << "Hello World!";
return 0;
}
Output
Hello World!
C++ STRUCTURE For “Hello World!”
// Your First C++ Program
In C++ any line starting with // is comment. Comments are intended for the person reading the code to better understand the program’s functionality. The C++ compiler overlooks it. A Programmer can add as many comments in the program for better understanding. Click here to understand more about Comments.
#include
The #include is a preprocessor directive used to include header files in our program. The above code includes the contents of the iostream file. This permits us to use cout in our code to print the output on the terminal or screen.
Remember that we must use “#include <iostream>” to use cout that permits us to print output on the screen.
using namespace std;
“using namespace std” means we use the namespace entity in C++ (we’ll study namespace later in detail) named std. “std” is an abbreviation for standard. So that implies we use all the things within the “std” namespace. If we don’t want to use this line of code, we have to use the things in this namespace like this. std::cout, std::cin,std::endl etc. Don’t worry; this may look a bit heavy for now, but if you go through the complete Basics of C++, you’ll gain crystal clear clarity.
Main Function: int main() {…}
A valid C++ program must have the main() function. The curly braces indicate the start and the end of the function.
The int here is the return type which demonstrates to the compiler that this function will return an integer value, and that’s why we have a return 0 statement at the end of the primary function.
cout << “Hello World!”;
cout belongs to the header file iostream. cout is used to print the statement in the inverted commas, which will provide an output of any statement, followed by the << and ended by the semicolon ‘;’ symbol. Anything inside the inverted commas will the displayed in the program’s output.
return 0;
The “return 0;” argument is the “Exit status” of the program. In basic terms, the program closes with this argument.
There are various return types in C++, but this is the most basic and useful. We must include “return 0;” in every program. You’ll understand the core concept behind this as you’ll learn.
FOLLOW-UP
Great, you successfully understood your first program and are ready to begin your coding; just hoping you won’t find any errors or warnings in your first code; if you find any “finger cross🤞,” then you may ask any question feed in the comments below. You may also check our Error and Warning blog for your clarification.
Similarly, you must also know that GeekonPeak has just begun and is looking for some loyal supporters so we, the new-gen geeky friends, can grow and create more extensive and easy-to-grasp courses for you (for free). You can follow us on Facebook, Instagram & Twitter as well. Also, don’t miss our newsletter subscription. We provide exclusive blogs, tips, tricks, and advice to program efficiently and market smartly. We also promote some of our partners with excellent experiences to share with you. Until next time 🙂