}

Programming language C (III). Agents and Expressions

1991/04/01 Alegria Loinaz, Iñaki | Maritxalar, Montse Iturria: Elhuyar aldizkaria

In language C, an operator is used to represent an operation. The combination of variables and/or constants with agents generates expressions.

After analyzing the variables and constants in the previous chapter, the objective of this chapter is to see how the variables and constants mentioned can be combined to form expressions.

In language C, an operator is used to represent an operation. The combination of variables and/or constants with agents results in expressions consisting of one or more expressions, combined with agents, in an order. There are many more operators = (assignment) + (addition) – (subtraction) and * (multiplication) that we have used in the programs of the previous chapters. Before analyzing one by one, we will analyze two main features of the agents: priority and compatibility.

Table . Agents and their priority and compatibility.

Characteristics of agents

When there is more than one agent in an expression, the evaluation order depends on the priority of the agents. The highest priority operations are carried out obtaining the first work results. Subsequently, following the decreasing order of priority, the following agents apply on the previous work results. Parentheses, as in other languages, modify the order of evaluation. For example, the evaluation of 2 + 3 * 4 generates 14 because multiplication has a higher priority degree than the sum.

Convergence tells us how the evaluation is done, from left to right or vice versa. For example, while the operator has the right left compatibility, common arithmetic operators (+, –, *) have the left to right.

a = b = c

is evaluated by the right. Therefore, b first and a later take the value of c.

a + b - c

on the contrary, it will be evaluated from left to right. Table 1 shows the most relevant agents and their compatibility in priority order.

We will analyze the agents that appear in the table.

Table . Arithmetic operators.

Arithmetic operators

As can be seen in Table 2, apart from the usual operations, two more are presented, the increase and the decrease. In both cases the assignment occurs and although they can be expressed otherwise, they have been introduced into the language C for the sake of comfort and effectiveness, sometimes difficult to read programs.

It is worth noting the existence of a single agent for the division, both total and real (against PASCAL, div and / or operators differ). If the operands are complete, the result will be that of the entire division and in the rest the real (see table 3).

Table . Total and real division.

The module, the operator%, can only be used with complete operands.

Example:

Condition number pair (x 2%) = = Ø

Bit-handling agents

Among the characteristics of language C is the ability to capture bits, for which the agents described in table 4 are available. As is known, the AND operation is used to put multiple bits in Ø, to put the OR operation in state 1 and change the xor state (bits). To do this, a variable type character (8 bits), short (16 bits) or long (32 bits) is matched with a mask indicated in hexadecimal (prefix Øx). The bits that you want to keep in the mask are assigned the value Ø and those that you want to put in one (OR) or change (XOR) the value. The AND operation is performed backwards and the operator can be used for this purpose.

Table . Bit-handling agents.

For example, if the n variable is made up of 8 bits (without knowing its value) and is placed in a bit to the left, it is set to zero the right and you want to change the two centers, you should:

Table . Relational factors.
n = n | Øx8Ø n = n ( ØxØ1) n = n^ Øx18

/* n ?ó /* n ?ó /* n ?ó

XXXX and XXXX */

1xxx xxx Ø */ 1xxx– x–xxØ */

Displacements serve primarily to use bit handling in repetitive structures, as we will see in chapter 5.

Relationship factors

Relational operators are mainly used to represent conditions (see table 5). In any case, language C can be any expression condition; the assignment, ...

When a non-relational expression is a condition, the evaluation of the expression is considered false when Ø is and in the rest of the cases as true. Therefore, we must be very careful with the equality relationship, since it is very easy to put = = instead of = (assignment), as is done in other languages. Consequently, the result of the condition to be verified will depend on the value assigned.

int n = Ø

if (n=Ø) /* always false, since the assignment is */ if (n=Ø) /* in this case the real thing is n Ø already */

It should be noted that character strings cannot be combined with the observed operators, since character strings are not basic data (see chapter 7).

Other agents

These agents have a lot to do with data structures and demonstrators and will delve into chapter 7 (see table 6).

Table .

Combined agents

These agents combine an operation with the assignment, being the result and operating 1 always the same variable. This way you can replace x=x+by x+= a, x=x 3 with x =3 etc.

These agents are:

+= % =Recent searches

Related links


/= What is it?

Gai honi buruzko eduki gehiago

Elhuyarrek garatutako teknologia