Hello World
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World"<<endl;
return 0;
}
和C语言类似,我们打印了Hello World,我们使用cout函数相当于printf。
endl意思是end line,表示一行结束,类似于C语言中的\n。
using namespace std;其实是一种省略,如果不使用也可以换一种写法。
#include <iostream>
//using namespace std;
int main(){
std::cout<<"Hello World"<<std::endl;
return 0;
}
cin
我们来计算任意两个数字的和并输出
#include <iostream>
using namespace std;
int main(){
int a,b;
cin>>a>>b;
cout<<"sum is "<<a+b<<endl;
getchar();
getchar();
return 0;
}
两个getchar()是为了让第一次键入数字后的enter被读取后仍然窗口能停留,直到我们在键入任意键。
cin的作用是从键盘读入的参数传给我们提前声明的变量a和b。