Constants and variables are the fundamental elements of each program. Simply speaking, a program is nothing else than defining them and manipulating them. There isn't a big difference between constants and variables, except that variables allows that their value can change during run time.
All variables have three essential attributes:
Changing the value of a variable (aka to calculate with it) is done with the asignment command, which is indicated by the equal sign in the source text. A typical asignment is
a = 10 ;
where the value 10 is stored in the memory for the variable
a. Note that accessing the value of
a is equivalent to accessing the reserved memory space, assigned to the variable.
The asignment operated divide the command in a left side and right sight. It is mandatory that the left side always points to a variable, so that the content of the right side can be stored. The instruction '
10=a;' would cause a compilation error because the value 10 is not asigned to a memory space (this is typical for a literal constant). The right hand side can be any expression, which yield a value suitable for the memroy space to hold it.
a = 10;
b = a +5;
a = 2*a -1;
The first line asigned the value 10 to the
a (= the memory space of
a). The second line asign a different value to the variable
b. Here it uses the value of variable
a and adds 5 to it. Thus the asigned value is 15. Although the value of
a is used in the right hand side of the asignment, it does not affect the value of
a itself. The reason is that every right hand side expression is temporarily stored in local memory of the CPU chip, calculated/modified there and then at the end of the calculation (the end of the right hand side expression) copied to the memory space (here of
b). This allows us to understand the behavior of the third line. Note that it is not an algebraic relation with a=1 as its solution. The last value of
a is loaded in CPU memory, multiply by two and subtracted by 1. The resulting value of 19 is stored back to
Algebraic espressions are very intuitive and evaluated the same way as we would do on a paper, with multiplication and division have a higher priority than addition and subtraction but a lower priority than grouping with round brackets. Another type of an expressions are Boolean expressions. Here the value can be either true or false. The operator are
&& for the and-operation,
|| for the or-operation and
! for the not-operation. Example:
d = ! ( a && b ) || c; With
a being true,
c being false the value of
d would be true. Similar to the Boolean algebra is the bitwise Boolean algebra with integer number, but it is not further explained here. A complete list of all operation can be found here.
In the examples above we used
b to hold numeric and boolean values. From the variable name it is not clear, which type the variables are (what is the boolen value for '10' ?). To avoid this ambiguity, every variable has to be declared before using it for the first time. The declaration defines its type and the require space in memory to store its value. The 4 basic types are integer numbers, floating point numbers, boolean numbers andcharacter values, with different degrees of precission. All other types are derived from these basic types. Another basic data type is a pointer, which will be explained in detailed in another section.
The declaration is done with keywords and the list of the variables name at the beginning of each function. A minimal programs, which uses variables is
b are integer numbers,
c a boolean number and
e are floating point numbers with a higher precision (double precission). A list of all data types can be found here. Note that the 3.45e-2 in the last line is the scientific notation for 0.0345.
Sometimes the compiler allows the automatic conversion between two types different types, when the result is not ambiguis. Referring to the example above, the statement
e = d / a; is allowed because the interger value 10 can be reinterpretated as 10.0 (or in a different notation 1.e1) without any loss of precission. This is called a promotion to a higher data type. The rules of promotions are complicated but as a general rule promotion is allowed from integer to floating point number and from a lower to higher precission. If promoting is not allowed (e.g. the statement
b = 0.5*d; where a conversion from a floating point number to an integer number is required), the compiler would mark it as an error. Promotion or the lack of it can be a source of many errors, e.g. the statement
e=(a/4); is valid but results in the assignment of 2 to
a is an integer variable and 4 is an integer constant and thus the promotion is done after the integer devision. The statement
e=(a/4.); yields the value 2.5, because 4. is a floating point constant and the promotion of
a to a floating point is required prior to the calculation of the division.
Any conversion can be enforced by type casting, where the data type keyword is put in round brackets in front of the number. Floating point numbers get truncated, char variables are resulting in the ASCII number of the character and bool variablles have the values 0 and 1 for false and true, respectively. An example is
b=a/((int)d + 2); resulting in the value of 3 for
b. Another method of casting uses pointers, which enforces the reinterpretation of the memory content as another type.