[C++ basic knowledge] A simple C++ program

C++ source program composition

First look at a simple C++ program

//sum.cpp
#include<iostream>                      //编译预处理命令
using namespace std;                    //使用命令空间std
int main()                              //主函数   
{
    
       int x,y,sum;                        //定义三个整型变量x,y,sum
    cin>>x;                             //从键盘输入x
    cin>>y;                             //从键盘输入y
    sum=x+y;                            //将x与y和赋给sum  
    cout<<"x+y="<<sum<<endl;            //输出两个整数和sum
    return 0;                           //程序结束,返回0
}

The first line of the program is a c++ style comment, from "//" to the end of the line, and the content of the comment shows that the file name of this program is sum.cpp.

The second line is to compile the preprocessing command, and the third line is to use the namespace std instruction.

The "cin>>" on the 6th and 7th lines assigns the value entered by the keyboard to the variable.

The 9th line is to output the specific value of "x+y=" and sum. "Endl" is an output manipulator, which functions as a newline, which is the same as "\n" in C language.

Look at the output of the program:

Insert picture description here

cin cost

The keywords cin, cout and operators "<<", ">>" used in this program are not in the C language. They are new input and output methods provided by C++.

Cin is the standard input stream object, cout is the standard output stream object, ">>" is the extraction operator (also called the input operator), and "<" is the insertion operator (also called the output operator).

expression:

cin>>variable means to read data from the keyboard to the variable.

The cout<< variable means that the data is written to the output stream object (can be understood as printing to the screen).

Note: ">>" allows the user to input a series of data continuously, but every two data must be separated by a blank character (space, carriage return or Tab), for example: cin>>a>>b>>c;

iostream

The "#include" in the second line of the program is a compilation preprocessing instruction, which is used to instruct the compiler to embed the code of the file iostream into the program when preprocessing the program. Iostream is a header file defined by the C++ system. In this folder, information about the input and output operations required by the program is declared. The definitions of stream objects cin, cout and operators "<<" and ">>" are all included in the file iostream.

std instruction

"Using namespace std;" is an instruction for the namespace std, which means to use the namespace std. It can ensure that each feature of the C++ standard library operation is unique and prevent naming conflicts. If the header file uses "#include<iostream.h>", the std instruction may not be used. But note that the two methods cannot be mixed.
If you use "#include<iostream.h>", you don't need to use the std instruction. But note that the two methods cannot be mixed.

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/110700822