Main menu

Pages

C++ Writing a program - Learn CPP, Before you start programming, you should know that programming depends on logic only, meaning that 1 + 1 = 2, and now we will learn how to write the first program in the C++ language.


C++ Writing a program - Learn CPP


For example, if we ask the program to print the message “hello world”, we will follow these steps:


Explain the parts of the program



#include<iostream>

using namespace std;

int main()

{

court<<"hello world";

return 0;

}


Part one: #include<iostream>


The # symbol is pronounced hash or pound, which means directive.

include means to include.


#include: prompts the compiler to include a main file within the main file, and the main file must be enclosed between <> tags.


The <> tags: enclose the name of the directory file, meaning that the compiler starts by searching for the directory file in the index or main folder (folder) containing all the directory files called include.


iostream: an abbreviation for Input Output Stream, i is short for input, o is short for output, and stream: a general library of input and output commands, without which it is not possible to enter values for the program or print on the screen; Because this library includes the cin input command and the cout output command, and the inclusion of the iostream is mandatory in the program if we want to do the input and output.


Part two: using namespace std;


As for using namespace, it means using the namespace of std, and it is called the declared field technique where we use namespaces in our programs using the using the reserved word, that is, using means the continuity of using the namespace in all parts of the program.


And if this line had not been set, we would have had to write cout like this std::cout, in other words cout is originally std::cout, that is, we would have had to put std:: before every command in the program, and std is an abbreviation of the word Standard.


Third part: int main()


(main): Functions are one of the most important components of the program in the C++ language, and we find that the program can consist of one or more functions and that each function must have a name that indicates it when it is used and called, and main is the basic core of any program. The first point in the program is the one from which the execution of any program in the C++ language begins. Without it, there is no program, as the execution of commands starts after, even if they are written in the middle of the program.


The brackets () are very important, as they indicate that it is a function and not a variable. In a clearer sense, it means that this name is a name for a function. Without these brackets, the translator translates it as a name for a variable, and for this reason, the parentheses are extremely important.


As for the difference between int and void: int H means that the main function will return an integer value, and void means that this function does not return any value.


And if we want to replace int with void, we delete the statement ;return 0.


It also begins with a curly bracket {, and ends with a closing parenthesis}, these two brackets are considered the body of the main function, and the required code is written inside it and it is not permissible to write a code outside of them, otherwise an error will appear.


And finally, the semicolon (;), which is necessary at the end of each line in parentheses, tells the main function that the command is finished and that it must go to the next command.