This code will not compile if the -Werror and -Wall flags are passed to the compiler.
Without them, it will compile and run, but the result may not be what you expect.
a is an uninitialized variable.
It is important to understand that this does not mean that a has no value.
a must have a value, since a is merely an abstraction of a
memory cell, and a memory cell cannot be empty.
An uninitialized variable in C++ is a variable
into which no value has yet been deliberately written.
Declaring a variable merely allocates a memory cell for that variable.
That cell may have been used by another variable before.
Such memory can retain its old value
from a previous use.
For this reason, a variable may contain any number,
and, as the programmer, you cannot rely on what it will contain.
The contents of an uninitialized variable
are also called garbage data.
Why can a memory cell not be empty?
Because memory consists of bytes, and each byte consists of 8 bits.
Bits can store either 0 or 1, and nothing else.
They cannot store “nothing”.
Accordingly, a byte is made up entirely of bits,
each of which is either 0 or 1.
You might decide that 0 is “nothing”, but that is not always the case.
0 can be deliberately written into a bit.
If 0 and “nothing” were the same thing,
you would not be able to distinguish the two
by reading a bit in isolation.
Was it 0 because nothing has been written there yet,
or because someone deliberately wrote it there earlier?
What happens if you read from variable a?
Since a is uninitialized, you will obtain whatever value
was in the memory before it was allocated to a.
However, reading from an uninitialized variable is considered UB (undefined behavior),
which by definition means that anything can happen,
and the compiler is allowed to assume that such a read is impossible.
On line 2, the result of the expression on the right-hand side of the assignment (a + 6) is written to b.
Evaluating this expression means turning it into a single value.
a + 6 -> 5 + 6 -> 11
The result of evaluating the expression is the value 11, which is written to cell b.
Further changes to a do not affect the previous operation, since its result has already been stored in b.
What counts as an expression here?
A literal (5, 6, 7), a variable read (a).
Each of them evaluates to a single value with its own type (all int here).
Even the whole assignment (a = 7) is an expression.
Why? Because its result can be assigned to e.g. another variable.
For example the following code assigns 7 to a while evaluating a = 7,
which itself evaluates to 7 (whatever both of them became after the assignment),
which is then assigned to c.
intc= (a=7);
Even b on std::cout << b << std::endl; is an expression,
because its value is going to be passed to the printing function,
and it has to be evaluated to e.g. a number before getting sent to the print function.
6. Literals, variables and operators are expressions
It does not compile.
The output stream goes on the left, and output uses <<: std::cout << "ggg";.
Changing both the operand order and the operator does not produce an equivalent expression.
This syntax is largely equivalent to the following:
inta=5;
It differs in that the compiler reports an error
when an assignment could result in a loss of information.
For example, the following code compiles if no compiler flags are provided.
When run, the number 5 will be stored in a (the fractional part will be discarded).
inta=5.6;
If curly braces are used instead, it will not compile.
This strictness can help us notice possible mistakes.
Empty braces mean the variable is value-initialized with a default value.
For int, the default value is 0, so 0 is written into a and 0 is printed.
This is different from int a;, which leaves a uninitialized with garbage data (see example 2).
Unlike an uninitialized variable, reading from a here is allowed.
sizeof is evaluated at compile time and yields the size, in bytes, of a variable or type.
It does not execute at run time: the compiler replaces sizeof(...) with a plain number,
so sizeof itself does not even exist as a function at run time.
For example:
sizeof(int) produces 4;
sizeof(a) is equivalent to sizeof(type of a), that is, sizeof(int), that is, 4;
sizeof(uint8_t) produces 1 (8 bits — 1 byte).
The operand is never actually computed.
In sizeof(a + 1), the expression a + 1 is not evaluated;
the compiler only looks at the type its result would have (here, int),
because types are known only at compile time and do not survive to run time.
auto means that the type is automatically inferred from the type of the initializer expression,
not just from the text on the right-hand side.
Since the expression 5 has type int, a will have type int.
You can think of auto as being replaced with int during compilation.
The inferred type is static (fixed at compile time) and cannot change later:
autoa=5;
a="abc"; // error: `a` is `int`, it cannot become a string later
This will not compile: auto needs an initializer expression to infer the type from.
With no expression, the compiler has nothing to replace auto with.
Assigning 5 on the next line does not fix it —
the type must be fixed at the definition and cannot be inferred retroactively from a later assignment.
auto looks at the type of the whole initializer expression, not just at whether it is a literal.
a + 5 has type int, so b becomes int with value 10.
The same holds for any expression: auto c{ a }; would also give c the type int.