A Step-by-Step Tutorial for Writing Your First C++ Program
If you are someone trying to pursue a career in the IT or computer science sector, learning and mastering a programming language is necessary as it will give you an extra edge among so many jobseekers. Talking about C++ it is one of the popularly known Object Oriented Programming languages that is widely used and it is also absolutely easier to understand. The “Hello World” program is the most straightforward program you can write using C++ language.
If you are wondering how to write your own first “Hello Program” code, worry not as we will now see how to write one in a snap. The Hello World Program in C++ is the first basic program that we will be writing to demonstrate how the coding processes actually work and after the process completes your “Hello World” sentence will be displayed on your screen.
Writing Your First Program in C++ Programming Language
Writing C++ programmes and executing them requires configuring the local environment on your machine. One option is to write and run your C++ programmes online using an IDE, if you don’t want to set up the local environment on your PC. You can even enroll in a proper C++ training and certification program to learn more about this programming language.
1. C++ program comment to display “Hello World”
You can use comments to display any additional information or notes about your program. A comment is a statement that does not contain any programming logic and when the compiler encounters a comment, the compiler simply skips that line of code. Any line that begins with ‘//’ without quotes or in between /…/ in C++ language is taken as a comment.
2. #include statement
This preprocessor directive, `#include`, instructs the compiler to incorporate the content of a specified file into the source code. For instance, `#include <iostream>` directs the compiler to add the standard iostream file, which contains declarations for all the standard input/output library functions.
3. Usage of using namespace std
This is used to bring the entities from the std namespace into the current namespace of the program. Although using a using namespace std statement is typically discouraged, importing a namespace essentially brings all its definitions into the current scope.
The std namespace is extensive, so instead of using the using namespace statement, it’s recommended to specify the namespace for each identifier with the scope operator (::). For example, std::cout can be used. std::cout is an instance of the std::ostream class, which is used to display output on the screen. Everything enclosed in double quotes “ ” following the << operator is displayed on the output device.
4. Int main() {} function
A function can be defined as a set or group of statements that are created to perform a specific task. Therefore, regardless of where the function is located inside the programme, we must recognise that the main() function serves as the entry point for all C++ programmes.
The program’s opening braces signify the start of the main function, while the closing braces signify its conclusion.
5. cout<< “ Hello World”;
Std::cout is an instance of the std::ostream class that is used to display the output on your screen. Everything that is followed by the character << in double quotes “ ” is displayed on the output device.
The semi-colon character at the end of the statement indicates that the sentence is going to end there.
6. return 0 statement
This return 0 statement indicates that a statement is finished and returns a value from a function. When a function needs to return the outcome of its operations, we use return 0.
7. Indentation
To improve the readability of the code, indentation is required. The return statement and the cout have been shifted to the right or indented, as you can see. To make the code easier to read, we must always utilize comments and indentations.
Points to Keep In Mind
- You should always include the necessary header files for smooth execution of the functions. For example, <iostream> must be included to use std::cin and std::cout.
- Do not forget to add main() function as the execution of the code begins with it
- You have to use Indentation and comments in the program as it is considered as a good practice.
- cout is used for print statements while cin is used to take inputs.
Keeping yourself updated in this competitive digital landscape is the only way in which you can transform and grow in your career. You can achieve this through self-learning or if you feel that you need more guidance then you can enroll in formal C++ training courses to stay ahead among other graduates and job seekers.