Basic Of C++ | How to code in C++ | Learn C++ | C++ Basics || C++ For Beginners | C++ Tutorials For Beginners | Cpp Tutorial | Cpp Tutorial For Beginner

 

by - Yash Ukalkar |


Prerequisites-

Yes, this is supposed to be your 'FIRST' program but we will get right to it in a bit. Now, before we write anything, you need to install a C++ compiler and a code editor on your device. We will be using 'VS Code' and of course we will help you with THE SETUP.
DONE!
That's it! Now let's write your first C++ program.

First program-

Open VS Code and create a new file





Save the file with desired name and the extension '.cpp'. For example, you could save the file as 'Hello.cpp'





Now, in the code editor, write the following code, save the file, and press the 'Run Code' button in the top right of the window.





You will see 'Hello World!' written in the terminal window. That was your first program which displays something on the screen.






Understanding the program-

Only writing some code will not help us. We won't be diving too deep but let's understand little bit about how we got the program to do something.

(P.S: this might be long enough to bore you, but it will be helpful, trust me on this.)


1) #include<iostream>

This is known as a 'header file' and is something that you need to write at the top of every C++ program. The '#include' part instructs the compiler to add the file written inside the angular brackets '<>' to the program. In this case, we add the 'iostream' file which includes information that helps us perform basic input-output operations. We will encounter more such header files with specific uses in the future.

2) using namespace std;

This is a statement that declares the 'namespace' of functions we will be using in the program(in this case, the 'namespace' used is 'std' which stands for 'standard'). The keyword 'using' is used to tell the compiler that we will be specifying a namespace ahead.
The given definition of 'namespace' is that it is a region where the functionality of a library(header file), but this doesn't clarify much. We can explain it with a scenario.
We know that a 'header file' provides us with functionality to write programs, and that there are multiple 'header files' in C++. In case two of such files offer some functionality with the same name and we use that, then the compiler has no idea from what file do you want the function to be used. The 'namespaces' are provided to solve this issue. Now, if you want to use some function, say f(x) from 1st header file, while another function with the same name exists in a second header file, you can specify the 'namespace' associated with f(x) in 1st file, and use the function without problem.

3) int main(){}

This is how we declare a function in C++(something we will see in another post). The important thing to know is that every C++ program needs to have a function named main because the compiler starts the execution of the program from this function. The code that is to be executed is written inside the curly braces {}int is a datatype that we will see in another post.

4) cout<<

This is a function provided in the iostream library that lets us display something on the screen. What we want to display is up to us but here, we have displayed "Hello World!" which is known as a string(something we will see in another post) that essentially is a combination of certain characters written inside double quotes ""

5) return 0;

This is known as a 'return statement'(obviously!). A return statement is written inside a function and it marks the end of the execution of a function and gives out some value while doing so. In this case, this statement marks the end of the int main() function and returns a 0(zero) value.


Comments

We can add some lines starting with // and /*...*/ . These are used to write comments in a C++ program. Comments are lines that are not read by the compiler but exist for another person to look at. Comments are used(widely) to explain some part of the written program.
// are used to write something in a single line.
To write more lines of comments, we use /*...*/ and the words are written between the asterisk marks(*).






NOTE: You can see a semicolon(;) being used in this program, this is something that has to be written at the end of each statement(another thing you will see later, sorry!) in C++. This helps the compiler understand when the execution of one complete statement is done. 

One last thing, we can avoid using namespace declaration by using a scope resolution operator(::) and doing so is considered good practice in programming with C++. Below is how to do so in this 'Hello World!' program-




Phew!! That was your first C++ program. I hope you understood what I was intending to explain. To keep learning, click on this link.
THANK YOU!

Comments

Popular posts from this blog

How to install C++ || Step-By-Step Tutorial || How to install C++ In Windows 10 || Installation of C++ || How to install C++ MinGW || How to install C++ In VS CODE || How to use C++ In VS Code || VS Code Installation

Basics Of Operators||Basics of operators in C++||Basics of operators in cpp||Types of operators in C++||Arithmetic Operators||Comparision Operators||Binary Operators||Logical Operators||Bitwise Operators||Assignment Operators||Miscellaneous Operators||Bitwise Explained||Operators Precedence||

Conditional Statements in C++