Skip to content

Latest commit

 

History

History
202 lines (162 loc) · 2.25 KB

6-C Operators.md

File metadata and controls

202 lines (162 loc) · 2.25 KB

C Operators

Video Link: https://youtu.be/_57FcSBtJNU Tutorial Link: https://www.programiz.com/c-programming/c-operators

Arithmetic Operator

Addition        +
Subtraction     -
Multiplication  *   
Division        /   
Remainder       %   
Increment       ++
Decrement       --

Addition Operators

Addition of int variables:

#include <stdio.h>

int main() {

  int x = 12;
  int result = x + 8;
  
  printf("%d", result);
  return 0;
}

Another way:

#include <stdio.h>

int main() {

  int x = 12;
  
  printf("%d", x+8);
  return 0;
}

Output

20

Addition of double variables:

#include <stdio.h>

int main() {

  double x = 12.57;
  double result = x + 8.67;
  
  printf("%.2lf", result);
  return 0;
}

Output

21.24

Addition of double and int variable:

#include <stdio.h>

int main() {

  double x = 12.57;
  int y = 8;
  double result = x + y;
  
  printf("%.2lf", result);
  return 0;
}

Output

20.57

Division Operator

Division with integer numbers:

#include <stdio.h>

int main() {

 int x = 12;
 int result = x / 8;
  
  printf("%d", result); 
  return 0;
}

Output

1

Division with floating-point numbers:

#include <stdio.h>

int main() {
  double x = 12.00;
  double result = x / 8.00;
  
  printf("%.2lf", result);
  return 0;
}

Output

1.50

Remainder Operator

#include <stdio.h>

int main() {

  int x = 12;
  int result = x % 8;
  
  printf("%d", result);
  return 0;
}

Output

4

Increment and decrement Operator

Increment Operator

#include <stdio.h>

int main() {

  int x = 12;
  
  printf("%d", ++x);
  return 0;
}

Output

13

Decrement Operator

#include <stdio.h>

int main() {

  int x = 12;
  
  printf("%d ", --x);
  return 0;
}

Output

11

Multiple Operators

#include <stdio.h>

int main() {

  int x = (4 / 2) + (6 * 5) - 1;
  
  printf(" %d", x);
  return 0;
}

Output

31

Programiz Quiz

Q. What is the value of x in the following code?

int x = 5 + 2 * 9 / 3 - 3;

Options

  1. 18
  2. 11
  3. 8
  4. Error

Answer: 3