C Operators

An operator is a special symbol that tells the compiler to perform mathematical or logical operations.

C programming language supports a rich set of operators that are classified as follows.

  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Increment & Decrement Operators
  • Assignment Operators
  • Bitwise Operators
  • Conditional Operator
  • Special Operators(Misc operator)

  Arithmetic Operators

The arithmetic operators are the symbols that are used to perform basic mathematical operations like addition, subtraction, multiplication, division and percentage modulo.  



Relational Operators
 
The relational operators are the symbols that are used to compare two values.Every relational operator has two results TRUE or FALSE.
 

 
Logical Operators
 
The logical operators are the symbols that are used to combine multiple conditions into one condition. 
 
 


Increment & Decrement Operators
 
The increment and decrement operators are called unary operators because both need only one operand. The increment operators adds one to the existing value of the operand and the decrement operator subtracts one from the existing value of the operand.
 

Pre-Increment or Pre-Decrement
 
In the case of pre-increment, the value of the variable is increased by one before the expression evaluation. In the case of pre-decrement, the value of the variable is decreased by one before the expression evaluation.
 
Example 
 
#include<stdio.h>
#include<conio.h>
void main()
{
   int i = 12,j;
   
   j = ++i;
   
   printf("i = %d, j = %d",i,j);   
   getch();

Output



Post-Increment or Post-Decrement
 
In the case of post-increment, the value of the variable is increased by one after the expression evaluation. In the case of post-decrement, the value of the variable is decreased by one after the expression evaluation.
 
Example



Difference between pre/post increment & decrement operators 
 

Assignment Operators

The assignment operators are used to assign right-hand side value (Rvalue) to the left-hand side variable (Lvalue). The assignment operator is used in different variants along with arithmetic operators.
 
 

Bitwise Operators
 
The bitwise operators are used to perform bit-level operations.When we use the bitwise operators, the operations are performed based on the binary values.
 

Conditional Operator

The conditional operator is also called a ternary operator because it requires three operands. This operator is used for decision making. In this operator, first we verify a condition, then we perform one operation out of the two operations based on the condition result. If the condition is TRUE the first option is performed, if the condition is FALSE the second option is performed.

Condition ? TRUE Part : FALSE Part;
Example 
X = (12<20)?300:600; ⇒ X value is 300
 
Special Operators
 
sizeof operator

This operator is used to find the size of the memory (in bytes) allocated for a variable. This operator is used with the following syntax.

sizeof(variableName); 
 
Pointer operator (*)

This operator is used to define pointer variables in c programming language.

Comma operator (,)

This operator is used to separate variables while they are declaring, separate the expressions in function calls, etc.

Dot operator (.)

This operator is used to access members of structure or union.

 
 
 






No comments:

Post a Comment