Module: (C++) Variables. Output formats


Problem

1/7

Variables

Theory Click to read/hide

Variables

A computer would not be needed if it were not possible to store various information in its memory and be able to process the same type of information using the same algorithms.
In order to create more interesting programs, you need to learn how to save information in computer memory. In this case, we need to learn how to somehow access the computer's memory cells.
In programming, as in life, so that access to any part of the computer’s memory occurs by name. Using this name you can both read information and write it there.
 
Variable is a storage location (identified by a memory address) paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a value

The word "variable" tells us that its meaning can change during program execution. When saving a new variable value - the old one is erased

For a computer, all the information is the data in its memory — sets of zeros and ones (if simpler, then any information on the computer is just numbers, and it processes them the same way). However, we know that integers and fractional numbers work differently. Therefore, in each programming language there are different types of data for the processing of which different methods are used.

For example,
- integer variables - type int (from integer), occupy 4 bytes in memory;
- real variables, which may have a fractional part (type float -  floating point), occupy 4 bytes in memory
- characters (type char - from character), occupy 1 byte in memory

Let's try to add some kind of variable to our program.
Before using a variable, you must tell the computer to allocate a place in memory for it. To do this, you must declare a variable, that is, indicate what type of value it will store, and give it a name.
Also, if necessary, you can assign it the initial values.

Let's analyze the program as an example
#include <iosrtream> 
using namespace std; 
int main()
{ 
  // two variables of an integer type were declared, 
  // into the variable 'a' and immediately saved the value 6.  
  int a=6, b;  
  // the variable 'b' was not set to the initial value; 
  // what will be in the memory in this case we do not know.. 
  return 0;
} 
Now try it yourself!

Problem

In the third line, declare a variable a of integer type with an initial value of 7.