logo codesdope

Let's start


In the last chapter, you saw how to print "Hello World" on the screen. Now let's see how the code worked by understanding the significance of each of its line.

#include <iostream>
int main()
{
   std::cout << "Hello World";
   return 0;
}
Output
Hello World

On compiling the above code, Hello World will be printed on the screen.

So, you have just written a code that can print anything on the screen. Sounds good! Now, let's understand this code in detail so that we can print any message.

Let's start with std::cout << "Hello World"; - std (short form of standard) is a namespace and cout is defined in this std namespace. Now a question comes in your mind that what is a namespace?

Basically, a namespace is a special area inside which something is defined. So, in this case, cout is defined in std namespace. Thus, std::cout states that cout is defined in the std namespace or to use the definition of cout which is defined in std namespace.

So, std::cout is used to use the definition of cout from std namespace.

<< is the output operator.
You will learn about operators in the next chapters. For now, just understand that here << is used to pass "Hello World" to cout. Thus, std::cout << "Hello World"; passes "Hello World" to cout and cout will print this Hello World on the screen.

using std::cout in C++

One more thing you need to know here is that std::cout is an object in the iostream library. You will learn about objects in the later chapters. For now, just understand that the whole std::cout thing resides inside iostream library and for this we need to include iostream in our code.
To include the iostream library, we write the following code.

#include <iostream>

Here, #include links our program to the iostream library or it will make iostream library available for our use.

So after including iostream, we are ready to use cout in our program.

So, #include <iostream> will make std::cout available for use and std::cout<<"Hello World" will use the definition of cout from std namespace to print Hello World (passed by <<) on the screen.

Let's come to int main. main is a function.
Now, what is a function?

A function is like a machine which takes something from us (not necessarily) and then performs an operation on it. In simple words, a function takes input from the user and gives back output. Simple, isn't it?

functions in c++

int main() - main is a function or main is the name of the function.
int main() means that the function main gives us an integer (It will return an integer value as output).

return 0; - As mentioned earlier, the function main returns an integer value (int main()), therefore here we are returning 0. return is a keyword which is used to return some value from a function. It indicates that our program has been run successfully and we terminate our main function with this return statement.

{ } - Curly braces '{}' following the main function represents the body of the main function. All the statements written inside this curly braces are in the body of the main function.

use of {} in c++ body

Why do we use the main function?


When a C++ code is executed, the main function is executed first. Thus the statements written inside the main function (within the curly braces following the main function) will be executed first. So to execute any statement, we put it inside the main function.

The execution of every C++ program starts from this main function. All C++ programs must have this main() function.

So, our workflow is -
#include<iostream> included the iostream library. After this in the main function, std::cout<<"Hello world" printed Hello world on the screen and return 0 returned 0 from the main function indicating the successful execution of our code.

Printing a new line


In our previous example, we printed "Hello World". What if we want to print "Hello" and "World" in two separate lines?

We can do this by using std::endl which is used to change line. Just like cout, endl is also defined in the std namespace and thus written as std::endl.

Let's see the following example.

#include <iostream>
int main()
{   std::cout << "Hello" << std::endl;
    std::cout << "World";
    return 0;
}
Output
Hello
World

Here, after "Hello" got printed, std::endl changed the line and thus "World" got printed on a separate line. If we had not written std::endl, then Hello and World would have got printed in the same line as follows.

#include <iostream>
int main()
{
   std::cout << "Hello";
   std::cout << "World";
   return 0;
}
Output
HelloWorld

In the above example, std::cout << "Hello"; printed "Hello" and after that std::cout << "World"; printed "World". Thus, World got printed just after Hello with no space between the two.

We can also use multiple << to print the above result as shown below.

#include <iostream>
int main()
{
   std::cout << "Hello" << "World";
   return 0;
}
Output
HelloWorld

Another way to print newline


Let's see this example first:

#include <iostream>
int main()
{
   std::cout << "Hello\nWorld";
   return 0;
}
Output
Hello
World

\n is newline character which is used for changing line. It is just like another character but prints a new line.
In Hello\nWorld, when compiler printed H when it encounters 'H', e when 'e'. Similarly, it printed a new line when it encountered \n

new line in C++

Commenting


Comments are statements which are written inside our code which are just ignored by the compiler while compiling our code. These are written to make our code more readable. Comments are written between '/* */' or after //.

Why comments?


As mentioned earlier, it makes our code more readable. Assume that you have written a software and after releasing it, you hired a few good programmers for the maintenance. Without comments, it would be a very difficult task for them to understand your code. And most of the time it happens that the person who has written a code is not the one who is going to modify it. So, make it a habit of writing comments.

Comments written after // are single line comments. Thus if you change line, then the new line will not be a part of your comment.

//This is single line comment
//This is also a comment
#include <iostream>
int main()
{
   std::cout << "Hello World";
   return 0;
}
Output
Hello World

Comment can also be multi-lined by enclosing it between /* and */ as shown in the following example. But we can't put one comment inside another. e.g. - /* This is a /*comment*/ */ is invalid.

/*Hello World code*/
/* Comments will not be compiled and will be ignored */
/* It is a
multiline
comment*/
#include <iostream>
int main()
{
   std::cout << "Hello World";
   return 0;
}
Output
Hello World
comments in C++

Coding is all about practice and solving questions. So, solve questions as much as you can. Solve questions after completing every chapter then only go for the next chapter.

In future, when writing long codes, make sure that you run your code from time to time while writing instead of completing your whole code and running at last. This will make debugging your code easier if you have made some errors.

The best way to predict the future is to create it.
-Peter Druker

Share what you know
Doubt? Ask question
Close

Welcome.please sign up.

Close

Welcome.please login.