转到内容

C++ 编程 / 代码 / 变量 / 示例

来自维基书籍,面向开放世界的开放书籍

变量

加两个数字并打印其和

// This program adds two numbers and prints their sum.
#include <iostream>
 
int main()
{
  int a;
  int b;
  int sum;

  sum = a + b;
 
  std::cout << "The sum of " << a << " and " << b << " is " << sum << "\n";
 
  return 0;
}

// This program adds two numbers and prints their sum, variation 1
#include <iostream>
#include <ostream>

using namespace std;

int main()
{
  int a = 123, b (456), sum = a + b;
 
  cout << "The sum of " << a << " and " << b << " is " << sum << endl;
 
  return 0;
}
华夏公益教科书