You get a bonus - 1 coin for daily activity. Now you have 1 coin

8.5. Operations, expressions and operators.

Lecture



The main operations of the C language are + , * , - , / , and the assignment operator = . As in Pascal, there is no exponentiation in C. The main operations are performed in C as well as in Pascal, but have several additions. Thus, the sign " - " can be used as a unary operation that changes the sign of a variable to the opposite. Special use distinguishes the division operation. It can be applied to both integers and real numbers. If the result of a division operation must be assigned to an integer variable, then the fractional part is simply discarded. This action is called truncation . If the division is applied to operands of different types, the integer value is converted to a floating point form.
Among the most common C operations are:
1) sizeof - it returns the size of the operand, expressed in bytes. The operand can be a specific data type or data type. When using the latter, it is written in brackets.
2) modulo division ( % ). Used in integer arithmetic. Its analogue in Pascal is the function mod .
3) increment ( ++ ). Performs a simple action: increases the value of the operand by 1. This operation can be written in prefix form, when the " ++ " character precedes the variable, and in the postfix form, when " ++ " follows the variable. These forms differ in the sequence of increment of the operand value.
Example:
a ++;
++ a;
b = a ++;
b = ++ a;
The first two operators have no differences. In the third example, the value of the variable a will first be assigned to the variable b, and then increased by 1. In the fourth example, the value of the variable a will first increase and then assigned.
4) decrement ( - ). Exists in prefix and postfix forms. The result of his action is to decrease the value of the operator by 1.
The operations " ++ " and " - " have a very high priority of execution. The above is only the execution of actions in brackets.
There are several forms of the assignment operator in the C language:

Records Actions
+ = the value of the right side is added to the left side variable
- = subtracts the value of the right side of the variable of the left side
* = multiplies the value of the left variable by the value of the right side
/ = the value of the variable of the left part is divided by the value of the right part
% = the remainder of dividing the left into the right is assigned to the left side variable


An expression in the C language is a combination of operations and operands. An important property of expressions is that it must have a value. You can make blocks from C operators. Operators in a block are combined using curly braces. Usually, variables and constants of only one type should be used in operators and expressions. In C, the use of different types will not stop the execution of the program. In this case, the set of rules for automatic type conversion will be activated:
1) when the types char and short occur in an expression, they are automatically converted to an int . The type float is converted to double . Since such actions are converted to a type that provides a larger data size, they are called type enhancement.
2) if the operation is performed on data of different types, then both values ​​are converted to the highest of these types.
3) the sequence of types ordered by the principle from higher to lower looks like this: long double , double , float , unsigned long , long , unsigned int , int .
4) in the assignment operator, the final result of the calculation is converted to the type of the variable to which the calculated result is assigned.
As far as possible, automatic type conversion should be avoided. When creating an expression, you can request that you perform the type of conversion you need. This method is called ghost types and is defined as follows. Before the specified value in parentheses is written the name of the desired data type.
Example:
b = (int) 3.3 + 4;

created: 2014-10-09
updated: 2021-03-13
132378



Rating 9 of 10. count vote: 2
Are you satisfied?:



Comments


To leave a comment
If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Algorithmization and programming. Structural programming. C language

Terms: Algorithmization and programming. Structural programming. C language