In this tutorial, we will learn about the variables and comments in the C++ language. In our last lesson, we discussed the basic structure of a C++ program, where we understood the working of the C++ code line by line. If you haven't read the previous lecture, make sure to navigate through the course content section.
We are going to cover two important concepts of C++ language:
- Variables in C++
- Comments in C++
Variables in C++
Variables are containers to store our data. To make it easy to understand, I will give a scenario: to store water, we use bottles, and to store sugar, we use a box. In this scenario, the bottle and box are containers that are storing water and sugar; the same is the case with variables; they are the containers for data types. As there are many types of data types, for example, "int" is used for integers, the "float" is used for floating-point numbers, "char" is used for character, and many more data types are available, we will discuss them in upcoming lectures. The main point here is that these variables store the values of these data types. Figure 1 shows an example of a variable. "sum" is taken as an integer variable, which will store a value 6, and writing sum after the "cout" statement will show us the value of "sum" on the output window.
Comments in C++
A comment is a human-readable text in the source code, which is ignored by the compiler. There are two ways to write comments.
Single-Line Comments: 1st way is to use" //" before a single line of text to make it unparsable by the compiler.
Multi-Line Comments: 2nd way is to use "/*" as the opening and "*/" as the closing of the comment. We then write text in between them. If we write text without this, the compiler will try to read the text and will end up giving an error. Figure 1 shows examples of these comments.
Code as described/written in the video
#include<iostream>
using namespace std;
// This program was created by Code With Harry
/* this
is
a
multi
line
comment */
int main(){
int sum = 6;
cout<< "Hello world"<< sum;
return 0;
}