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

2. OPERATIONS AND OPERATORS of the C language

Lecture



C operations

Any expression of a language consists of operands (variables, constants, etc.) connected by operation signs. An operation sign is a symbol or a group of symbols that tell the compiler to perform certain arithmetic, logical, or other actions.

Operations are performed in strict sequence. The value that determines the preferential right to perform a particular operation is called a priority. In tab. 2 lists various C operations. Their priorities for each group are the same (groups are highlighted in color). The greater the advantage of the corresponding group of operations, the higher it is located in the table. The order of operations can be adjusted with parentheses.

table 2

Operation mark Purpose of operation
() Function call
[] Selecting an array element
. Highlight item
-> Highlight item
! Logical negation
~ Bitwise negation
- Change sign
++ Increment by one
- Decrease by one
& Taking address
* Contact at
(type of) Type conversion (i.e. (float) a)
sizeof () Determining the size in bytes
* Multiplication
/ Division
% Determining the remainder of the division
+ Addition
- Subtraction
<< Shift left
>> Shift right
< Less than
<= Less or equal
> More than
> = More or equal
= = Equally
! = Not equal
& Bitwise logical "AND"
^ Bitwise exclusive "OR"
| Bitwise logical "OR"
&& Logical "and"
|| Logical "OR"
?: Conditional (ternary) operation
= Assignment
+ =, - =, * =, / =,% =, << =,
>> =, & =, | =, ^ =
Compound assignment operations (for example, a * = b
(i.e. a = a * b), etc.)
, Operation comma

To eliminate confusion in terms of "operation" and "operator", we note that the operator is the smallest executable unit of the program. There are expression operators whose action consists in calculating given expressions (for example: a = sin (b) + c; j ++;), declaration operators, compound operators, empty operators, label operators, loop operators, etc. To mark the end of the operator in the C language uses a semicolon. As for the compound operator (or block), which is a set of logically related operators placed between the opening ({) and closing (}) curly brackets ("operator brackets"), there is no semicolon behind it. Note that the block differs from the compound operator by the presence of definitions in the body of the block.

Let us characterize the main operations of the C language. First, consider one of them - the assignment operation (=). Expression type

  x = y;  
 

sets variable x to value y. Operation "=" is allowed to be used repeatedly in one expression, for example:

  x = y = z = 100; 
 

There are unary and binary operations . The former have one operand, and the latter have two. We begin their consideration with the operations assigned to the first of the following traditional groups:

  1. Arithmetic operations.
  2. Logical operations and relational operations.
  3. Operations with bits.

Arithmetic operations are defined by the following symbols (Table 2): +, -, *, /,%. The last of them cannot be applied to real type variables. For example:

  a = b + c;  
       x = y - z;  
       r = t * v;  
       s = k / l;  
       p = q% w; 
 

The logical operations of a relationship are specified by the following characters (see Table 2): && ("And"), || ("OR"), ! ("NOT"),>,> =, <, <=, = = (equal),! = (Not equal). Traditionally, these operations should give one of two values: true or false. The following rule is accepted in the C language: truth is any non-zero value; false is a zero value. Expressions that use logical and relational operations return 0 for false and 1 for true. Below is the truth table for logical operations.

Table 3

x y x && y x || y ! x
0 0 0 0 one
0 one 0 one one
one 0 0 one 0
one one one one 0

Bit operations can be applied to variables that have types int, char, as well as their variants (for example, long int). They cannot be applied to float, double, void (or more complex types) type variables. These operations are defined by the following characters: ~ (bitwise negation), << (left shift), >> (shift right), & (bitwise "AND"), ^ (bitwise exclusive "OR"), | (bitwise "OR").

Examples: if a = 0000 1111 and b = 1000 1000, then

  ~ a = 1111 0000, 
       a << 1 = 0001 1110, 
       a >> 1 = 0000 0111, 
       a & b = 0000 1000, 
       a ^ b = 1000 0111, 
       a |  b = 1000 1111.  
 

The language provides two non-traditional increment operations (++) and decrement (-). They are intended to increase and decrease per unit operand value. The ++ and - operations can be written both before the operand and after it. In the first case (++ n or - n), the value of the operand (n) is changed before its use in the corresponding expression, and in the second (n ++ or n--) - after its use. Consider the following two lines of the program:

  a = b + c ++; 
       a1 = b1 + ++ c1; 
 

Suppose that b = b1 = 2, c = c1 = 4. Then after performing the operations: a = 6, b = 2, c = 5, a1 = 7, b1 = 2, c1 = 5.

Expressions with another unconventional ternary or conditional operation?:. In the formula

  y = x?  a: b;  
 

y = a if x is not zero (i.e. true), and y = b if x is zero (false). Following expression

  y = (a> b)?  a: b;  
 

allows you to assign the variable y the value of the larger variable (a or b), i.e. y = max (a, b).

Another difference of the language is that an expression of the form a = a + 5; can be written in another form: a + = 5; Instead of the + sign, you can use symbols of other binary operations (see Table 2).

Other operations from the table. 2 will be described in the following paragraphs.

Type conversion

If operands of various types appear in the expression, they are converted to some general type, and the following sequence of rules is applied to each arithmetic operand:

  1. If one of the operands in the expression is of the type long double, then the others are also converted to the type of long double.
  2. Otherwise, if one of the operands in the expression is of type double, the others are also converted to type double.
  3. Otherwise, if one of the operands in the expression is of type float, then the others are also converted to type float.
  4. Otherwise, if one of the operands in the expression is of the unsigned long type, the others will also be converted to the unsigned long type.
  5. Otherwise, if one of the operands in the expression is of the long type, the others are also converted to the long type.
  6. Otherwise, if one of the operands in the expression is of the unsigned type, the rest are also converted. to type unsigned.
  7. Otherwise, all operands are converted to type int. In this case, the type char is converted to signed int; type unsigned char to int, whose high byte is always zero; type signed char to int, for which the character from сhar is transferred to the sign bit; type short in int (signed or unsigned).

Suppose that the value of an expression on the right side of an assignment operator is calculated. On the left side of the assignment operator there is a variable, and its type differs from the type of the result on the right side. Here, the transformation rules are very simple: the value to the right of the assignment operator is converted to the type of the variable to the left of the assignment operator. If the size of the result on the right side is larger than the size of the operand on the left side, then the oldest part of this result will be lost.

In C, you can explicitly specify the type of any expression. To do this, use the conversion operation ("cast") type. It is applied as follows:

  (type) expression  
 

(here you can specify any type that is allowed in C).

Consider an example:

  int a = 30,000;  
       float b;  
       ........ 
       b = (float) a * 12;  
 

(the variable a of the integral type is explicitly converted to the type float; if this is not done, the result will be lost, since a * 12> 32767).

Type conversion can also be used to convert argument types when calling functions.

Pointers and operations with them

Pointers are variables that indicate the location or address of the memory where other objects are located (variables, functions, etc.). Since the pointer contains the address of some object, through it you can access this object.

The unary operation & gives the address of the object, so the operator

  y = & x;  
 

assigns the address to variable x to variable y. The & operation cannot be applied to constants and expressions; constructions of the form & (x + 7) or & 28 are not allowed.

The unary operation * takes its operand as the address of some object and uses this address to fetch the content, therefore the operator

  z = * y;  
 

assigns z the value of the variable written at address y. If a

  y = & x;  
       z = * y;  
 

then z = x.

Objects consisting of a * sign and an address (for example, * a) must be determined. This is done, for example, as follows:

  int * a, * b, * s;  
       char * d;  
 

The definition of the form char * d says that the value written at address d is of type char.

Pointers can also appear in expressions. If y is a pointer to an integer, i.e. there was an int * y declaration, then * y may appear in the same place as any other variable that is not a pointer. Thus, the following expressions are perfectly valid:

  * y = 7;  
       * x * = 5; 
       (* z) ++;  
 

The first of them puts the number 7 in the memory cell at address y, the second increases the value at address x five times, the third adds one to the contents of the memory cell with address z. In the latter case, parentheses are necessary, since operations with the same priority are performed from right to left. As a result, if, for example, * z = 5, then (* z) ++ will result in * z = 6, and * z ++ will only change the z address itself (the ++ operation is performed on the z address, and not on the value * z at this address).

Pointers can be used as operands in arithmetic operations. If y is a pointer, then the unary operation y ++ increases its value; now it is the address of the next item. Pointers and integers can be added. The construction y + n (y is a pointer, n is an integer) specifies the address of the n-th object pointed to by y. This is true for any objects (int, char, float, etc.); the translator will scale the address increment according to the type specified in the object definition.

Any address can be checked for equality (==) or inequality (! =) With a special value NULL, which allows you to define a non-addressing pointer.

Cycle operators

Loops are organized to execute a statement or group of statements a certain number of times. In the C language, there are three loop operators: for, while, and do - while. The first one is formally written in the following form:

  for (expression_1; expression_2; expression_3) body_cycle  
 

The body of the cycle is either one operator or several operators enclosed in curly brackets {...} (no semicolon is inserted after the block). In expressions 1, 2, 3, a special variable appears, called the control variable. According to its value, it is necessary to repeat the cycle or exit from it.

Expression_1 assigns the initial value to the control variable, expression_3 changes it at each step, and expression_2 checks whether it has reached the limit value that establishes the need to exit the loop.

Examples:

  for (i = 1; i <10; i ++)  
       {... 
       } 

       for (сh = 'a'; ch! = 'p';) scanf ("% c", & ch); 
                / * The loop will run until the keyboard  
                   the character 'p' * / will not be entered 
 

Any of the three expressions in the for loop may be missing, but the semicolon must remain. Thus, for (;;) {...} is an infinite loop from which you can exit only in other ways.

The following rule is accepted in the C language. Any expression with an assignment operation enclosed in parentheses has a value equal to that assigned. For example, the expression (a = 7 + 2) has the value 9. Then you can write another expression, for example: ((a = 7 + 2) <10), which in this case will always give the true value. The following construction:

  ((h = getch ()) == 'i')  
 

allows you to enter the value of the variable ch and give a true result only when the entered value is the letter 'i'. In brackets you can write several formulas that make up a complex expression. For these purposes, use the comma. Formulas will be calculated from left to right, and the entire expression will take the value of the last calculated formula. For example, if there are two variables of type char, then the expression

  z = (x = y, y = getch ());  
 

determines the following actions: the value of the variable y is assigned to the variable x; a character is entered from the keyboard and assigned to the variable y; z gets the value of the variable y. The brackets are needed here because the comma operation has a lower priority than the assignment operation written after the z variable. The comma operation is widely used to construct for loops and allows you to simultaneously change the values ​​of several control variables.

Nested constructions are allowed, i.e. In the body of a cycle there may be other for statements.

The while statement is formally written as:

  while (expression) body  
 

The expression in brackets can take a non-zero (true) or zero (false) value. If it is true, the body of the loop is executed and the expression is evaluated again. If the expression is false, the while loop ends.

The do-while operator is formally written as follows:

  do {body}} while (expression); 
 

The main difference between the while and do-while loops is that the body in the do-while loop is executed at least once. The body of the loop will be executed until the expression in brackets takes a false value. If it is false at the entrance to the cycle, then its body is executed exactly once.

Nesting of some cycles into others is allowed, i.e. The for, while, and do-while statements may appear in the body of any loop.

In the loop body new break and continue statements can be used. The break statement provides an immediate exit from the loop, the continue statement causes the termination of the next and the beginning of the next iteration.

Operators of conditional and unconditional jumps

For the organization of conditional and unconditional jumps in the C program, the following statements are used: if - else, switch and goto. The first one is written as follows:

  if (condition check) statement_1;  else operator_2;  
 

If the condition in brackets takes the true value, operator_1 is executed, if false - operator_2. If instead of one it is necessary to execute several statements, then they are enclosed in curly brackets. In the if statement, the word else may be missing.

In the if - else statement, immediately after the if and else keywords, other statements must follow. If at least one of them is an if statement, it is called nested. According to the convention adopted in the C language, the word else always refers to the nearest preceding if.

The switch statement allows you to choose from several alternatives. It is written in the following formal form:

  switch (expression)  
       { 
          case constant_1: operators_1;  
                             break;       

          case constant_2: operators_2;  
                             break;  
          ........ ........ 
          default: operators_default; 
       }  
 

Here, the value of the whole expression in brackets is calculated (it is sometimes called a selector) and it is compared with all constants (constant expressions). All constants must be different. If there is a match, the corresponding variant of operators will be executed (one or several operators). The variant with the default keyword is implemented if none of the others match (the default word may be absent). If default is absent, and all comparison results are negative, then no option is performed.

For the termination of subsequent checks after the successful selection of a certain variant, the break operator is used to immediately exit the switch.

2. OPERATIONS AND OPERATORS of the C language

Nested switch constructs are allowed.

Consider the rules for performing an unconditional transition, which can be represented in the following form:

  goto label;  
 

A label is any identifier followed by a colon. The goto statement indicates that the program must be continued from the statement before which the label is written. A label can be placed in front of any operator in the function where the corresponding goto operator is located. It does not need to declare.


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