Cpp - Introduction Variable Creation

Introduction

A variable must be defined before you can use it.

When you define a variable the type is specified and an appropriate amount of memory reserved.

A simple definition has the following syntax:

type name1[, name2,...]; 

EXAMPLES:

char c; 
int i, counter; 
double x, y, size; 

Variables can be defined either within the program's functions or outside of them.

  • a variable defined outside of any functions is global, i.e. it can be used by all functions
  • a variable defined within a function is local, i.e. it can be used only in that function.

The following code calculates circumference and area of a circle with radius 2.5

Demo

#include <iostream> 
using namespace std; 

const double pi = 3.141593; 

int main() /*from www.j a v a2 s .com*/
{ 
    double area, circuit, radius = 1.5; 

    area = pi * radius * radius; 
    circuit = 2 * pi * radius; 

    cout << "\nTo Evaluate a Circle\n" << endl; 

    cout << "Radius:        " << radius    << endl 
          << "Circumference: " << circuit   << endl 
          << "Area:          " << area      << endl; 

    return 0; 
}

Result

Related Topics