Holla Tech - Learn

Arithmetic Operators
 

C supports arithmetic operators + (addition), – (subtraction), * (multiplication), / (division), and % (modulus division).
Operators are often used to form a numeric expression such as 10 + 5, which in this case contains two operands and the addition operator.
Numeric expressions are often used in assignment statements.
For example

#include <stdio.h>

int main() {
  int length = 10;
  int width = 5;
  int area;

  area = length * width;
  printf(“%d \n”, area);  /* 50 */

  return 0;

 

Division
C has two division operators: / and %.
The division / operator performs differently depending on the data types of the operands. When both operands are int data types, integer division, also called truncated division, removes any remainder to result in an integer. When one or both operands are real numbers (float or double), the result is a real number.
The % operator returns only the remainder of integer division. It is useful for many algorithms, including retrieving digits from a number. Modulus division cannot be performed on floats or doubles.
The following example demonstrates division:

#include <stdio.h>

int main() {
  int i1 = 10;
  int i2 = 3;
  int quotient, remainder;
  float f1 = 4.2;
  float f2 = 2.5;
  float result;

  quotient = i1 / i2; // 3
  remainder = i1 % i2; // 1
  result = f1 / f2; // 1.68

  return 0;

 


Operator Precedence code
 

C evaluates a numeric expression based on operator precedence.
The + and – are equal in precedence, as are *, /, and %.
The *, /, and % are performed first in order from left to right and then + and -, also in order from left to right.
You can change the order of operations by using parentheses ( ) to indicate which operations are to be performed first.
For example, the result of 5 + 3 * 2 is 11, where the result of (5 + 3) * 2 is 16.

For example:

#include <stdio.h>

int main() {
  int a = 6;
  int b = 4;
  int c = 2;
  int result;
  result = a b + c; // 4
  printf(“%d \n”, result);

  result = a + b / c; // 8
  printf(“%d \n”, result);
 
  result = (a + b) / c; // 5
  printf(“%d \n”, result);

  return 0;
}  

 

NOTE!
C may not evaluate a numeric expression as desired when the associative property allows any order. For example, x*y*z may be evaluated as (x * y) * z or as x * (y * z). If order is important, break the expression into separate statements.

Type Conversion
 

When a numeric expression contains operands of different data types, they are automatically converted as necessary in a process called type conversion.
For example, in an operation involving both floats and ints, the compiler will convert the int values to float values.
In the following program, the increase variable is automatically converted to a float:

#include <stdio.h>

int main() {
  float price = 6.50;
  int increase = 2;
  float new_price;

  new_price = price + increase;
  printf(“New price is %4.2f, new_price);
  /* Output: New price is 8.50 */

  return 0;
}

 

Note the format specifier includes 4.2 to indicate the float is to be printed in a space at least 4 characters wide with 2 decimal places.
When you want to force the result of an expression to a different type you can perform explicit type conversion by type casting, as in the statements:

float average;
int total = 23;
int count = 4;

average = (float) total / count;
/* average = 5.75 */ 

 

Without the type casting, average will be assigned 5.
Explicit type conversion, even when the compiler may do automatic type conversion, is considered good programming style.

NOTE!
You can Try it yourself using a Code editor that is responsive to C language. 

Assignment Operators
 

An assignment statement evaluates the expression on the right side of the equal sign first and then assigns that value to the variable on the left side of the =. This makes it possible to use the same variable on both sides of an assignment statement, which is frequently done in programming.
For example:

int x = 3;
x = x + 1;  /* x is now 4 */ 

 

To shorten this type of assignment statement, C offers the += assignment operator. The statement above can be written as

x += 1;  /* x = x + 1 */ 

 

Many C operators have a corresponding assignment operator. The program below demonstrates the arithmetic assignment operators:

int x = 2;

x += 1;  // 3
x -= 1;  // 2
x *= 3;  // 6
x /= 2;  // 3
x %= 2;  // 1
x += 3 * 2;  // 7 

 

Look carefully at the last assignment statement. The entire expression on the right is evaluated and then added to x before being assigned to x. You can think of the statement as x = x + (3 * 2).

NOTE!
Try it yourself using a code editor that is responsive to C language. 

Increment & Decrement
 

Adding 1 to a variable can be done with the increment operator ++. Similarly, the decrement operator — is used to subtract 1 from a variable.
For example:

z–;  /* decrement z by 1 */
y++; /* increment y by 1 */ 

 

The increment and decrement operators can be used either prefix (before the variable name) or postfix (after the variable name). Which way you use the operator can be important in an assignment statement, as in the following example.

z = 3;
x = z–;  /* assign 3 to x, then decrement z to 2 */
y = 3;
x = ++y;  /* increment y to 4, then assign 4 to x */

 

The prefix form increments/decrements the variable and then uses it in the assignment statement.
The postfix form uses the value of the variable first, before incrementing/decrementing it.


BACK  NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE C MAIN COURSE PAGE. 

C MAIN COURSE PAGE

 


© License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT