C - IntroductionC - Hello World ProgramC - Exercise 1C - Basic structure of a C programC - TokensC - Data TypesC - Type ConversionC - Exercise 2C - Character Input Output OperationsC - Input Output operation using scanf and printf functions

Operators

C - Arithmetic OperatorsC - Relational OperatorsC - Logical OperatorsC - Assignment OperatorsC - Increment Decrement OperatorsC - Bitwise Operators

Precedence and Associativity

C - Precedence and AssociativityC - Exercise 3

Conditions

C - If Else decision making statementsC - Switch Case decision making statements

Loop

C - While LoopC - Do While LoopC - For LoopC - Exercise 4

Array

C - ArraysC - Two Dimensional ArraysC - Multi Dimensional ArraysC - Exercise 5

String

C - StringC - Exercise 6C - String Manipulation

Functions

C - FunctionsC - Functions CategoryC - Function Call - Flow of ControlC - RecursionC - Functions and ArraysC - Functions and Strings

Structures

C - StructuresC - Structures and ArraysC - Passing structure to functionC - Function returning structureC - Structure in Structure

Pointers

C - PointersC - Pointers and VariablesC - Pointers and Variables Memory RepresentationC - Pointers Chaining

Pointers and Arrays

C - Pointers and One Dimensional ArrayC - Pointers and Two Dimensional ArrayC - Array of Pointers

Pointers and Strings

C - Pointers and Strings

Pointers and Functions

C - Pointers and Functions - Call by Value and Call by ReferenceC - Function returning pointer

Pointers and Structures

C - Pointers and StructuresC - Pointers and Array of StructuresC - Passing structure pointer to function

Handling Files

C - File Handling - Getting StartedC - File Handling - Read and Write CharactersC - File Handling - Read and Write IntegersC - File Handling - Read and Write multiple dataC - File Handling - Randomly Access Files

Command Line Arguments

C - Command Line Arguments

Dynamic Memory Allocation

C - Dynamic Memory Allocation - Getting StartedC - Dynamic Memory Allocation - malloc functionC - Dynamic Memory Allocation - calloc functionC - Dynamic Memory Allocation - realloc function

C - Arithmetic Operators

C Programming

In this tutorial we will learn about arithmetic operators in C programming language.

C programming language provides us with all the basic arithmetic operators.

OperatorDescription
+Addition or unary plus
-Subtraction or unary minus
*Multiplication
/Division
%Modulo division

Addition Operator

In the following example we will add two numbers using the addition operator.

#include <stdio.h>
int main(void)
{
  int
    a = 10,
    b = 20;
    
  int sum = a + b;
  
  printf("Sum: %d\n", sum);
  return 0;
}

Output

Sum: 30

Subtraction Operator

In the following example we will subtract two numbers using the subtraction operator.

#include <stdio.h>
int main(void)
{
  int
    a = 10,
    b = 20;
    
  int diff = a - b;
  
  printf("Difference: %d\n", diff);
  return 0;
}

Output

Difference: -10

Multiplication Operator

In the following example we will multiply two numbers using the multiplication operator.

#include <stdio.h>
int main(void)
{
  int
    a = 10,
    b = 20;
    
  int prod = a * b;
  
  printf("Product: %d\n", prod);
  return 0;
}

Output

Product: 200

Division Operator

In the following example we will divide two numbers using the division operator.

#include <stdio.h>
int main(void)
{
  int
    a = 100,
    b = 10;
    
  int quot = a / b;
  
  printf("Quotient: %d\n", quot);
  return 0;
}

Output

Quotient: 10

During integer division, if both the operators are of the same sign, the result is truncated towards zero. If one of them is negative, the direction of truncation is implementation dependent.

Example:

6 / 7 = 0 and -6 / -7 = 0

But -6 / 7 may be 0 or -1 (machine dependent)

Mixed-mode Arithmetic

When one of the operand is real and the other is integer, the expression is called mixed-mode arithmetic. If either of the operand is of the real type, then only the real operation is performed. If both operand is integer then fractional part is truncated.

Example:

5 / 10.0 = 0.5

Whereas, 15 / 10 = 1 (fractional part 0.5 is truncated)

Modulo Operator

Modulo operator gives us the remainder.

In the following example we will divide two numbers and get the remainder using the modulo operator.

#include <stdio.h>
int main(void)
{
  int
    a = 9,
    b = 4;
    
  int rem = a % b;
  
  printf("Remainder: %d\n", rem);
  return 0;
}

Output

Remainder: 1

Modulo operator % can't be used on floating point value.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT