学习《C++ Primer Plus》02

第一个程序:

#include<iostream>

int main()

{

    using namespace std;  //make definitions visible

    cout<< "come up and C++ me sometime.";

    cout<< endl;       //start a newline

    cout<< "you won't regret it!" << endl;

    cin.get();

    return 0;           //terminatemain()

}

C++输出函数使用cout函数,也可使用C语言的printf()、scanf()和其他所有标准C输入和输出函数,只需要包含常规C语言的stdio.h文件。

通常,IDE允许在辅助窗口中运行程序。程序执行完后,有些IDE将关闭该窗口,而有些不关闭。如果编辑器关闭窗口,将难以看到输出。为查看输出,必须在最后加上一些代码:

cin.get();   //add this statement

cin.get();   //and maybe this too

return 0;

}

    cin.get()语句读取下一次键击,直到按Enter键退出。如果程序在其常规输入后留下一个没有被处理的键击,则第二条语句是必须的(例如输入一个数字后按Enter键)。

 

该示例非常简单,只包含一个名为main()的函数。该示例包含以下元素:

l  注释//

l  预处理器编译指令#include

l  函数头:int main()

l  编译指令:using namespace

l  函数体{}

l  使用cout输出

l  结束main()函数的return语句;

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/ly_222222/article/details/81053525