(C++) Variables. Output formats


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!

Let's try to write a calculator for primes

Our task is to display some arithmetic expression on the screen and make the computer count it.
For example, this:
5 + 7 = 12
Moreover, instead of 5 and 7, there may be different numbers, depending on the values ​​of the variables a and b in the program.

In the output operator, you can output not just text, but also the values ​​of variables, as well as the result of an arithmetic expression. Moreover, the output sequence may be different.

For example, you can output the above expression by writing:
cout << a << "+" << b << "=" << a + b << endl;
If you want to output the value of a variable, then you just need to specify its name without quotes. If we want to output the result of an arithmetic expression, then it is enough to simply write the arithmetic expression correctly.

Note:
Variables, text and arithmetic expressions are separated from each other by the operator <<

 

Advanced material
The number of output specifiers in the format string is not limited, the main thing for each qualifier is to list all the values ​​that will be substituted for the template after the format string, by comma.

For example:
printf("%d+%d=%d\n",a,b,a+b);

The format string indicates three qualifiers for displaying integer values ​​instead. The substitution order is direct:  instead of the first template %d, the value of the variable a is displayed; instead of the second, the value of the variable b; and instead of the third, the value of the expression a + b

Now let's write a program that outputs the result of basic arithmetic operations with these variables.

The input statement

In order for the user to be able to set the value of the variable himself, it is necessary to be able to input values from the keyboard.
 Handling the standard input in C++ is done by applying the operator cin >> variable_name .
cin >> a; 
After this operator, the data that is input from the keyboard to store it in a a-variable.
It is possible to combine several cin statements into one
For example:
cin >> a;
cin >> b;
performs the same actions as the record
cin >> a >> b;
that is, the first data entered is entered into the variable a, the second into the variable b
 
Advanced Material
For input data from the keyboard an input operator is used, which in general has the following structure
scanf ("<input formats>", <variable's address>);
Input formats  – this is a quoted string listing one or more data input formats.
For example, the most commonly used
%d input an integer variable (variable of type int)
%f input a real variable (variable of type  float)
input a one symbol (variable of type char)

For example,
scanf ("%d%d", &a, &b);


This operator, waits for an input from the keyboard the values of two integer variables. The first number input from the keyboard will store to cell a, the second to cell b.
 
After the input format, the addresses of the memory cells to which the inputed values are written are listed with a comma. Variable values are inputed by specifying the address of this variable. Therefore, it is necessary to put an ampersand sign in front of the variable name: &a is the address of the variable a.
The number of input formats and and the number of variables must match!

Advanced Material
Additional output specifiers allow you to control the output of numbers in certain formats.


Minimum field width
For example:
%04d  - the number will be outputed in 4 positions, if the numbers are less than four, then the first will be zeros
 
int a=34; printf("%04d\n",a); //on the screen: 0 0 3 4   

The underscore is specially designed to visually display the output of the number.

%4d – same, only spaces instead of zeros
int a=34; printf("%4d\n",a); // on the screen: _ _ 3 4

Conclusion with a certain accuracy - used to output real numbers. By default, real numbers are displayed with an accuracy of 6 decimal places. But there are cases that need to be deduced with a different accuracy. In this case, it is necessary to indicate how much familiarity to allocate for the number itself and how much after the decimal point.

For example
%9.3f   - the real number will be displayed in 9 positions, with three decimal places.
 
double a=34.24356; 
printf("%9.3f\n",a); // on the screen: _ _ _ 3 2 . 2 4 4
 
Now, let's try it. 

Advanced material. How to output the value of a variable on the screen

For this, inside the format string in the output operator, you must specify a certain template, in the place of which the variable value will be outputed.
Which format specifier to use depends on the type of variable.
These patterns are called output format specifiers and are presented in the table. A large enough quantity is given by the specifier. Remembering all of them is optional.
In most cases, we will use qualifiers to output integers, real numbers, as well as characters and strings. But you can return to this lesson at any time and see the format specifier you need.

Here’s a quick summary of the available printf format specifiers:
%c character
%d decimal (integer) number (base 10)
%e exponential floating-point number
%f floating-point number
%i integer (base 10)
%o octal number (base 8)
%s a string of characters
%u unsigned decimal (integer) number
%x number in hexadecimal (base 16)
%% print a percent sign
\% print a percent sign

Additions
To output variables of type short int, the specifier is used (for example,)
To output the values of variables of type long int, the l specifier is used (for example,)
The specifier L can be used as a prefix before the specifiers e, f, g. It means that the screen outputs a value of type long double. (eg, )