C - Introduction Arithmetic Operations

Introduction

An arithmetic statement is of the following form:

variable_name = arithmetic_expression;

The arithmetic expression on the right of the = operator specifies a calculation using variables or explicit numbers that are combined using arithmetic operators such as addition (+), subtraction (?), multiplication (*), and division (/).

The following code is an arithmetic statement:

total = cats + dogs + bugs + others;

The effect of this statement is to calculate the value of the arithmetic expression to the right of the = and store that value in the variable specified on the left.

The = symbol in C defines an action. It doesn't specify that the two sides are equal.

It assigns the value that results from evaluating the expression on the right to in the variable on the left.

For example:

total = total + 2;
total = cats + dogs + bugs + others;
total = total + 2;
printf("The total number of pets is: %d", total);

Demo

// Simple calculations
#include <stdio.h>

int main(void)
{
      int total;//ww  w  . j  a v a  2s  .  c  om
      int cats = 2;
      int dogs = 1;
      int bugs = 1;
      int others = 5;
      // Calculate the total number of pets
      total = cats + dogs + bugs + others;
      total = total + 2;
      total = cats + dogs + bugs + others;
      total = total + 2;

      printf("We have %d pets in total\n", total);   // Output the result
      return 0;
}

Result

Related Topics

Quiz