//程序清单2.2
//carrots.cpp -- food processing program
//uses and display a variable
#include <iostream>
int main()
{
using namespace std;
int carrots; //declare an interger variable
carrots = 25; //assign a value to the variable
cout<<"I have";
cout<<carrots; //display the value of the variable
cout<<"carrots";
cout<<endl;
carrots=carrots - 1; //modify the variable
cout<<"Crunch, crunch. Now I have " << carrots << "carrots."<<endl;
return 0;
}
注:int 表示整数的,是最基本的数据类型;对于声明变量,应尽可能在首次使用变量前声明它;
赋值语句 = (可以进行连等,如vamaha=baldwin=steinway=88,赋值将从左向右进行)
cout 在本程序中的新使用方式:用来打印变量(之前用来打印字符串)
<<(运算符)可以在同一行中连续使用,从而不必换行再次输入cout,节省时间
本文通过一个简单的C++程序示例介绍了如何声明和使用整型变量,包括变量的赋值、显示及修改过程。此外还展示了cout如何用于输出变量值。
537

被折叠的 条评论
为什么被折叠?



