C++ Variables and Expressions

A variable is a named location in memory. Variables are used to give a value a name so we can refer to it later.

The name of a variable is an identifier token. Here are rules for declaring a variable:

  • Identifiers may contain numbers, letters, and underscores(_).
  • Variable name can not start with a number.
  • Keywords can't be used as variable name.

The following table lists the keywords in C++.

alignas alignof and and_eq
asm bitor bool bitand
catch char16_t class compl
constexpr const_cast decltype nullptr
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while

Table: Keywords in C++

All keywords in C++ are here.

 

Variable Declaration and Initialization

You must declare all variables before they can be used. The basic form of a variable declaration is shown here:

 data_type variable [ = value][, variable [= value] …] ;

Here data_type is one of C++'s data types and variable is the name of the variable.

To declare more than one variable of the specified type, you can use a comma-separated list. Following are valid examples of variable declaration in C++:

int a, b, c; // Declares three ints, a, b and c.


There are three different ways to initalize variables in C++.

1. C-like initialization

Syntax:

type identifier = initial_value; 

​Example

int a = 10, b = 10; //Declares variables a & b and initializes them with value of 10 each

And here is  an example of initialization

byte B = 22; // initializes a byte type variable B.

double pi = 3.14159; // declares and   assigns a value of PI.

char a = 'a'; // the char variable a is initialized with value 'a'

 

2. Constructor initialization

Syntax:

type identifier (initial_value); 

Example:

int a(10); //declares an int variable a and initializes with value 10

 

3. Uniform initialization

Syntax:

type identifier {initial_value}; 

Example:

int a{10};  //declare a variable of type int called a and initialize it to a value of 10

 

Expressions

An expression is a statement that has a value – for instance, a number, a string, the sum of two numbers, etc.

Expressions combine variables and constants to produce new values. For example, 4+2, x-1, and "Hello, world!\n" are all expressions.

And, a statement is a unit of code that does something. Statements are a basic building block of a program.

Note: Not every statement is an expression. For instance, it makes no sense to talk about the value of an #include statement.

 

Here, is example program to illustrate all points mentioned above.

// initialization of variables, statement and expression example

#include <iostream>
using namespace std;

int main ()
{
  int a = 10;             // initial value: 10
  int b(30);              // initial value: 30
  int c{20};              // initial value: 20
  int result;             // initial value undefined

  a = a + b;              // statement but a+b is expression
  result = a - c;         // statement
  cout << "Result: "<< result;

  return 0;
}

The output of the program is:

Result: 20

 

Basic Input

Just as cout << is the syntax for outputting values, cin >> is the syntax for inputting values.

The following program illustrates basic input.

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    int my_num;
    cout<<"Enter an integer: ";
    cin >> my_num;
    cout<<"You entered: "<<my_num <<endl;
    string my_string;
    cout<<"Enter a string: ";
    cin >> my_string;
    cout<<"You entered: "<<my_string;
    return 0;
}

The output of the program is:

Enter an integer: 12 
You entered: 12 
Enter a string: We love you all. 
You entered : We love you all

So, cin object can input any type of data.

C++ Data Types
C++ Decision Statements