Best post to learn Operators in C part 1

Operators in C

Operators are symbols which are use to perform operations on data, operations like arithmetic, assignment, unary, etc.

Operators helps us to manipulate, comparison, Calculate data in program. This help us to make decision and control the flow of program.

Following are the types of operators in C program.

  1. Unary Operators
  2. Arithmetic Operators
  3. Relational Operators
  4. Logical Operator
  5. Ternary Operator
  6. Assignment Operator
  7. Bitwise Operator
All operators has their own precedence and associativity. 
ITVoyagers

Unary Operators

unary-operator-itvoyagers

As the name suggests unary operator works on one operand, in simple terms unary operator works with only one variable.

Minus “-“

When we have to change sign of value we can use minus operator. It can change sign from positive to negative or vice versa.                      

Syntax :

  -operand

Example :

  int a = 8, b;

  b = -a;

  printf(“b = %d”,b);

Output :

  b = -8

 

Address “&”

Address operator is use to get memory location of a variable. Variable which store another variables’s address is known as pointer.

Syntax :

  &operand

Example :

  int a = 8, *b;

  b = &a;

  printf(“b = %d”,b);

Output :

  b = 102031 //address of a

 

NOT “!”

NOT operator will return 0(false) when condition is true or value of variable is non-zero else it will return 1(true)                                       

Syntax :

  !condition   OR   !variable

Example :

  int a = 8, b=3, c; 

  c = !(a < b);   

  printf(“%d”,c); 

Output :                         

  1                                    

 

sizeof “sizeof()”

It will return the size of variable. sizeof() operator will not execute any operations mention in its parenthesis, it will return size. 

Syntax :

  sizeof(operand)

Example :

  int a = 8, b;

  b = sizeof(a);

  printf(“b = %d”,b);

Output :

  b = 2

Increment “++”

Increment operator will increment value by 1, in simple words it will add 1 to the value. There are 2 types of increment operator.

  1. Prefix Operator(Pre-Increment)
  2. Postfix Operator(Post-Increment)

Pre-Increment

Prefix operator will increment value before variable is used.

 Syntax :

  ++operand

 Example :

  int a = 8, b;

  b = ++a;

  printf(“b = %d”,b);

Output :

  b = 9

Post-Increment

Postfix operator will increment value after variable is used.

 Syntax :

  operand++

 Example :

  int a = 8, b, c;

  b = a++;

  c = a;

  printf(“b = %d”,b);

  printf(” c = %d”,c);

Output :

  b = 8 c = 9

Decrement “——“

Decrement operator will decrement value by 1, in simple words it will subtract 1 from the value. There are 2 types of decrement operator.

  1. Prefix Operator(Pre-Decrement )
  2. Postfix Operator(Post-Decrement )

Pre-Decrement

Prefix operator will decrement value before variable is used.

 Syntax :

  ——operand

 Example :

  int a = 8, b;

  b = ——a;

  printf(“b = %d”,b);

Output :

  b = 7

Post-Decrement

Postfix operator will decrement value after variable is used.

 Syntax :

  operand——

 Example :

  int a = 8, b, c;

  b = a——;

  c = a;

  printf(“b = %d”,b);

  printf(” c = %d”,c);

Output :

  b = 8 c = 7

ITVoyagers

Arithmetic Operators

arithmetic-operator-itvoyagers

When we want to perform basic mathematical operations(addition, subtraction, multiplication, division, modulus) on values or variables in program we have to use arithmetic operators.

Arithmetic operators are binary operators, they work with two variables.

Addition “+”

As name says addition operator is use to add 2 values, or in simple words it will return sum of two values which we can catch in variable.

Syntax :

  operand_1 + operand_2

Example :

  int a = 8, b=3, c;

  c = a + b;

  printf(“a + b = %d”,c);

Output :

  a + b = 11

 

Subtraction “-“

As name says subtraction operation use to perform subtraction between two values, it will return the result which we can catch in variable

Syntax :

  operand_1 – operand_2

Example :

  int a = 8, b=3, c;

  c = a – b;

  printf(“a – b = %d”,c);

Output :

  a – b = 5

 

Division “/”

Division operator is use to perform division operation between values. It will return quotient from calculation which we can save in variable.

Syntax :

  operand_1 / operand_2

Example :

  int a = 8, b=2, c;

  c = a / b;

  printf(“a / b = %d”,c);

Output :

  a / b = 4

Multiplication “*”

Multiplication operator will return product of two values or in simple terms it will multiply both the values and returns it’s results which can be store in variable.

Syntax :

  operand_1 * operand_2

Example :

  int a = 8, b=3, c;

  c = a * b;

  printf(“a * b = %d”,c);

Output :

  a * b = 24

 

Modulus “%”

Modulus operator similar as division operator but the difference is it will return remainder instead of quotient which can be stored in variable.

Syntax :

  operand_1 % operand_2

Example :

  int a = 8, b=3, c;

  c = a % b;

  printf(“a % b = %d”,c);

Output :

  a % b = 2

ITVoyagers

Relational Operators

logical-operator-itvoyagers

Relational operators are use to compare values in program. These are use to create condition and also to control flow of program.

Relational operators returns 1(true) if condition is true else it will return 0(false).

Greater than “>”

It will return 1(true) if LHS is greater than RHS, else it will return 0(false).

Syntax :

  operand_1 > operand_2

Example :

  int a = 8, b=3, c;

  c = a > b;

  printf(“%d”,c);

Output :

  1

 

Less than “<“

It will return 1(true) if LHS is less than RHS, else it will return 0(false).

Syntax :

  operand_1 < operand_2

Example :

  int a = 8, b=3, c;

  c = a < b;

  printf(“%d”,c);

Output :

  0

 

Equal to “==”

It will return 1(true) if LHS is equal to RHS, else it will return 0(false).

Syntax :

  operand_1 == operand_2

Example :

  int a = 8, b=3, c;

  c = a == b;

  printf(“%d”,c);

Output :

  0

 

Greater than equal to “>=”

It will return 1(true) if LHS is greater than equal to RHS, else it will return 0(false).

Syntax :

  operand_1 >= operand_2

Example :

  int a = 8, b=3, c;

  c = a >= b;

  printf(“%d”,c);

Output :

  1

 

Less than equal to “<=”

It will return 1(true) if LHS is less than equal to RHS, else it will return 0(false).

Syntax :

  operand_1 <= operand_2

Example :

  int a = 8, b=3, c;

  c = a <= b;

  printf(“%d”,c);

Output :

  0

 

Not equal to “!=”

It will return 1(true) if LHS is not equal to RHS, else it will return 0(false).

Syntax :

  operand_1 != operand_2

Example :

  int a = 8, b=3, c;

  c = a != b;

  printf(“%d”,c);

Output :

  1

 

ITVoyagers

Logical Operators

logical-operator-itvoyagers

Logical works with conditions statements, it check if conditions are true of false and accordingly it will return integer 0 of 1. Where 0 represents false and 1 represents true.

It is use to control flow of program.

Logical OR operator supports Short circuiting which means if first condition is true then it will not check next condition and it will return 1(true).

Logical AND “&&”

Logical AND will check both the conditions and returns 1(true) if both the conditions are true.

If even one of the condition is false then it will return 0(false).                                               

Syntax :

  condition_1 && condition_2

Example :

  int a = 8, b=3, c;

  c = a > b && b == 3;

  printf(“%d”,c);

Output :

  1

 

Logical OR “||”

Logical AND will check both the conditions and returns 1(true) if both the conditions are true and even if one of the condition is true then also it will return 1(true).

If both the conditions are false then it will return 0(false).

Syntax :

  condition_1 || condition_2

Example :

  int a = 8, b=3, c;

  c = a > b || b == 2;

  printf(“%d”,c);

Output :

  1

 

Logical NOT “!”

Logical operator will return 0(false) when condition is true or value of variable is non-zero. 

If condition is false or value of variable is 0 then it will return 1(true)                                       

Syntax :

  !condition   OR   !variable

Example :

  int a = 8, b=3, c;         int a = 8, b;

  c = !(a < b);         OR   b = !a;

  printf(“%d”,c);             printf(“%d”,b);

Output :                         Output :

  1                                    0

 

Try fun practice game on operators

CHECKOUT OTHER RELATED TOPICS

PRACTICALS IN C PROGRAM

We are aiming to explain all concepts of C in easiest terms as possible

Leave a Comment