In this tutorial objective will be to understand this C++ code line by line:
#include <iostream>
using namespace std;
int main() {
cout << "Our custom string to print" << endl;
return 0;
}
Explanation
#include <iostream>
In the most programming languages to activate additional functionalities that are writen by other programmers we need to use some form of "include" instruction.
For example, In C++ that's #include and in Python "import". After we type #include in our editor, on right side from it we must state what we need to import, in this case that's iostream (with arrows). Iostream contains source that will help us to display output on the screen and read input from the keyboard.
In general, lines staritng with # are called a preprocessor directives. They must be typed at the beggining of program, because other source will depend on it.
using namespace std;
This line means that we need "std" namespace with all functionalities in it. It's not enough to have iostream file included in our program, we also must say where those individual commands that we will use from iostream exactly resides in that file. All those functionalities are grouped into "std" namespace.
Consider namespaces as borders around specific piece of code that is located in some external file. Namespaces are like source locators in external files, so we call them to use them with lines like "using namespace std;".
Std means "Standard". Individual instructions from "std" namespaces are cin, cout, cerr, clog. They do specific tasks. We will talk about them more, don't worry.
int main() {
}
Every C++ program must have main function. That's place where all useful operations will be done. Name for function is "main" and after it we have parentheses. We need to have parentheses because that's the place where we will put "food" for functions so they can grab it and do something with it, some form of processing.
Functions in C++ must have return type. In this case that's "int" as "integer". That means that result of function will be some whole number, in this case that's 0, which means that everything is ok with program.
cout << "Our custom string to print" << endl;
cout is command from std namespace from iostream. It means "whatever is right from me it will be printed on screen". After that we must type 2 left-oriented arrows.
After arrows we have some string. String is defined with 2 double-quotes and bunch of text in-between. This is what we must see on screen printed.
After that we have again 2 left-oriented arrows, and endl with semicolon. Endl in C++ will be used to insert a new line characters and flushes the stream. That just means we are going into another line.
We must use semicolon after end of any instructions. This is how we instruct C++ to finish that one, and go to next operation.
return 0;
With "return 0" we are stating manually that our program is 100 % correct.
int main() {
cout << "Our custom string to print" << endl;
return 0;
}
Please note, all our custom code will go inside pair of braces (Curly brackets). They are like border around our own code.
No comments:
Post a Comment