Arithmetic Instructions

Arithmetic Instructions

Arithmetic Instructions An arithmetic statement C consists of a variable name on the left a = and variable names & constant on the right a =. The variables and constants that appear on the right side of = are connected by arithmetic operators such as +, -, *, and /.

Ex.: int ad ;
 float kot, deta, alpha, beta, gamma ;
 ad = 3200 ;
 kot = 0.0056 ;
 deta = alpha * beta / gamma + 3.2 * 2 / 5 ; 

here,

*, /, -, + are the Arithmetic Instructions operators.
= is the assignment operator. 2, 5 and 3200 are integer constants. 3.2 and 0.0056 are real constants. the ad is a whole variable. kot, deta, alpha, beta, gamma are real variables.

Variables and constants are called “operands” which are operated by “arithmetic operators” and the result is allocated, using the assignment operator, to the variable on the left.

An arithmetic statement C can be of three types. These are the following:

Full Arithmetic Instruction – This is an Arithmetic Instructions statement in which all operands are integer variables or integers.

Ex.: int i, king, issac, noteit ;
 i = i + 1 ;
 king = issac * 234 + noteit - 7689 ; 

Real mode arithmetic instruction – This is an Arithmetic Instructions statement in which all operands are either real constants or
real variables.

Ex.: float qbee, antink, si, prin, anoy, roi ;
 qbee = antink + 23.123 / 4.5 * 0.3442 ;
 si = prin * anoy * roi / 100.0 ; 

Mixed Arithmetic Instruction – This is an Arithmetic Instructions statement where some of the operands are integers and some of the operands are real.

Ex.: float si, prin, anoy, roi, avg ;
 int a, b, c, num ;
 si = prin * anoy * roi / 100.0 ;
 avg = ( a + b + c + num ) / 4 ; 

t is very important to understand how the execution of an arithmetic statement takes place. First, the right-hand side is evaluated using constants and numerical values ​​stored in the variable name. This value is then assigned to the variable on the left.

Although the Arithmetic Instructions look simple to use, sometimes they make mistakes in their writing. Let’s take a closer look these statements. Please note the following points carefully.

  • C allows only one variable to the left of a. That is, z = k * l is legal, while k * l = z is illegal.
  • In addition to the division operator C it also offers a modular division operator. This operator returns the remainder when dividing an integer by another. Thus, the expression 10/2 gives 5, while 10% 2 gives 0. Note that the module operator (%) cannot be applied to a float. Also, keep in mind that when using% the rest sign is always the same as the numerator sign. Thus –5% 2 yields –1, while 5% –2 yields 1.
  • An arithmetic instruction is often used to store character constants in character variables.
char a, b, d ;
a = 'F' ;
b = 'G' ;
d = '+' ;

When we do this, the ASCII values ​​of the characters are stored in variables. ASCII values ​​are used to represent anyone character in memory. The ASCII values ​​of “F” and “G” are 70 and 71 (see the ASCII table in Appendix E).

char x, y ;
int z ;
x = 'a' ;
y = 'b' ;
z = x + y ; 

Arithmetic Instructions operations can be performed on ints, floats and chars.
Thus, the statements are perfectly valid because the addition is made on the ASCII values ​​of the characters and not on the characters themselves. The ASCII values ​​of “a” and “b” are 97 and 98 and can therefore be added with certainty.

It is not assumed that no operator is present. It must be written explicitly. In the following example, the multiplication operator after b must be written explicitly.

a = c.d.b(xy) usual arithmetic statement
b = c * d * b * ( x * y ) C statement 

Unlike other high-level languages, there is no operator performing exponential operations. Thus, the following statements are invalid.

a = 3 ** 2 ;
b = 3 ^ 2 ; 

If we want to make exponents, we can do it as follows:

#include <math.h>
main( )
{
 int a ;
 a = pow ( 3, 2 ) ;
 printf ( “%d”, a ) ;
} 

Here the pow () function is a standard library function. It is used to raise 3 to the power of 2. #include is a preprocessor directive. It is used here to make sure that the pow () function is working correctly.

Integer and Float Conversions

To effectively develop C programs, you will need to understand the rules that are used for the default conversion of floating values ​​and integers in C. These are mentioned below. Write them down carefully.

  • An arithmetic operation between an integer and an integer always gives a complete result.
  • An operation between the real and the real always gives a real result.
  • An operation between an integer and a real number always gives a real one result. In this operation, the whole number is first promoted to a real one and then the operation is performed. From here the result is real.

I think some practical examples are presented in the following figure it would undoubtedly pose the problem.

Type Conversion in Assignments

It can happen so that the type of expression and the type of the variable to the left of the assignment operator cannot be the same. In such a case, the value of the expression is promoted or downgraded depending on the type of variable on the left.
=.

For example, consider the following assignment instructions.

int   i ;
float   b ;
i = 3.5 ;
b = 30 ; 

Here, in the first statement of attribution, although the expression is the value is a float (3.5) it cannot be stored in i, because it is an int. In the in such a case, the float is reduced to an int and then its value is stored. Therefore, what is stored in i is 3. Exactly the opposite happens in the following statement. Here, 30 is promoted to 30.000000 and then stored in b, because b being a float variable cannot hold anything
except for a float value.

Instead of a simple expression used in the above examples, if a complex expression appears, the same rules continue to apply. For for example, consider the following program snippet.

float   a, b, c ;
int   s ;
s = a * b * c / 100 + 32 / 4 - 3 * 1.1 ; 

Here, in the attribution statement, some operands are int others are floating. As we know, during expression evaluation the ints will be promoted in floating and the result
the expression would be a float. But when this float value is assigned at s, it is again withdrawn in int and then stored in s.

Notice the results of the Arithmetic Instructions statements shown in the figure 1.7. It has been assumed that k is an integer variable and a is a real variable variable.

Note that although the following statements give the same result, 0, the results are obtained differently.

k = 2 / 9 ;
k = 2.0 / 9 ; 

In the first statement, because both 2 and 9 are integers, the result is an integer, ie 0. This 0 is then assigned to k. In a second instruction 9 is promoted to 9.0 and then division is performed. The division obtains 0.222222. However, this cannot be stored in k, k being an int. Therefore, it is reduced to 0 and then stored in k.

You can find more C related topics here

http://projugaadu.com/category/topics/c-language/

Leave a Comment