initialize variable c++ code example

Example 1: initialize int c++

// operating with variables

#include <iostream>
using namespace std;

int main ()
{
  // declaring variables:
  int a, b;
  int result;

  // process:
  a = 5;
  b = 2;
  a = a + 1;
  result = a - b;

  // print out the result:
  cout << result;

  // terminate the program:
  return 0;
}

Example 2: initialisation of a c++ variable

//<Data type> <variable name>;
//static initialisation:
int acceleration=4;
//alternative:
int acceleration;
acceleration=4;
//dynamic \initialisation:
int acceleration;
std::cout<<"Enter the value of the acceleration\n";
std::cin>>acceleration;

Tags:

Cpp Example