Home Show/Hide Menu
C Tutorials
➜ C Language
  - Features of C
  - First C Program
  - Compilation in C
  - C Character Set
  - Tokens
  - Keywords
  - Identifiers
  - Constants
  - Operators
  - Data Types
  - Variables
  - Type Casting
  - Comments
  - Input/Output
  - Escape Sequence
  - Programming Errors
➜ Control Statements
  - if Statement
  - if-else Statement
  - Nested if-else
  - else-if
  - switch Statement
  - Loop
  - while loop
  - do while loop
  - for loop
  - break
  - continue & goto
  - Nested Loop
  - Infinite Loop
➜ Functions
  - What is function
  - Call by Value & Reference
  - Recursive function
  - Storage Classes
➜ Array
  - 1-D Array
  - 2-D Array
  - Return an Array
  - Array to Function
➜ C Pointers
  - Pointers
  - Pointer to Pointer
  - Pointer Arithmetic
  - Dangling Pointers
  - sizeof() operator
  - const Pointer
  - void pointer
  - Dereference Pointer
  - Null Pointer
  - Function Pointer
  - Function pointer as argument
➜ Memory Management
  - Memory Layout of C
  - Dynamic memory
  - calloc()
  - malloc()
  - realloc()
  - free()
➜ Strings
  - gets() & puts()
  - String Functions
  - strlen()
  - strcpy()
  - strcat()
  - strcmp()
  - strrev()
  - strlwr()
  - strupr()
  - strstr()
➜ C Math
  - Math Functions
➜ Enum, Structure & Union
  - Enum
  - Structure
  - typedef
  - Array of Structures
  - Nested Structure
  - Structure Padding
  - Union
➜ File Handling
  - fprintf() fscanf()
  - fputc() fgetc()
  - fputs() fgets()
  - fseek()
  - EOF and feof()
  - fsetpos()
  - tmpfile()
  - rewind()
  - ftell()
  - lseek()
➜ Preprocessor
  - Preprocessor
  - Macros
  - #include
  - #define
  - #undef
  - #ifdef
  - #ifndef
  - #if
  - #else
  - #error
  - #pragma
➜ Command Line
  - Arguments

Operators in C Language

The C programming language comes with very rich built-in operators to write codes very efficiently and easily. An operator is a symbol that tells the compiler to perform specific mathematical operations like arithmetical, logical, relational, bitwise etc.


Types of Operators

In C language, there are various types of built-in operators to perform various operations, which are shown below :

  • Arithmetic Operator
  • Increment/Decrement Operator
  • Assignment Operator
  • Relational Operator
  • Logical Operator
  • Ternary or Conditional Operator
  • Bitwise Operator
  • Special Operator

Arithmetic Operator

Arithmetic operator performs various types of mathematical operations like addition, subtraction, division, multiplication etc.

 Operator  Description Examples
+  Addition of two operands  X + Y
-  Subtraction of second operand from the first.  X - Y
*  Multiplication of two operands.  X * Y
/  Division between two operands.  X / Y
%  Modulus operator returns the remainder of the integer division of two operands  X % Y

Increment/Decrement Operator

C has two special types of unary operators called increment (++) and decrement (--) operators. These operators increment and decrement value of a variable by 1. Increment/Decrement operators are of two types:
  1. Pre increment/decrement operator - These operators immediately increases or decreases the current value of the variable.
  2. Post increment/decrement operator - These operators causes the current value of the variable to be used in the expression, then the value is incremented or decremented.

 Operator  Description
(++op)
Pre-Increment
 Y = ++X; Here the current value of X is incremented by 1. The new value of X is then assigned to Y.
(op++)
Post-Increment
 Y = X++; Here the current value of X is assigned to Y, then value of X incremented by 1.
(--op)
Pre-Decrement
 Y = --X; Here the current value of X is decremented by 1. The new value of X is then assigned to Y.
(op--)
Post-Decrement
 Y = X--; Here the current value of X is assigned to Y, then value of X decremented by 1.

Assignment Operator

An assignment operator assigns the value of the right-hand operand to the left-hand operand variable. The right-hand operand may be a value, variable or function.
  The assignment operators in C can both transform and assign values in a single operation. C provides the following assignment operators:

 Operator  Description Example
=  Simple assignment  X = 10; assign value 10 to X
X = Y + Z; assign the value of Y + Z to X
X = &Y; assign the address of Y to X
*=  Multiplication assignment  X *= Y; can be written as X = X * Y;
/=  Division assignment  X /= Y; can be written as X = X / Y;
%=  Remainder assignment  X %= Y; can be written as X = X % Y;
+=  Addition assignment  X += Y; can be written as X = X + Y;
-=  Subtraction assignment  X -= Y; can be written as X = X - Y;
<<=  Left-shift assignment  X <<= 2; can be written as X = X << 2;
>>=  Right-shift assignment  X >>= 2; can be written as X = X >> 2;
&=  Bitwise-AND assignment  X &= 2; can be written as X = X & 2;
^=  Bitwise-exclusive-OR assignment  X ^= 2; can be written as X = X ^ 2;
|=  Bitwise-inclusive-OR assignment  C |= 2; can be written as C = C | 2;

Relational Operator

The Relational operators in C are commonly used to check the relationship between the two variables, which are mostly used either in If Conditions or Loops. If conditions are true, then it returns value 1, otherwise, it returns 0.
  The following table shows all the Relational Operators in C programming with examples :

 Operator  Description Example
>  (X > Y)   X is greater than Y  (5 > 4) returns true (1)
<  (X < Y)   X is less than Y  (5 < 4) returns false (0)
>=  (X >= Y)   X is greater than or equal to Y  (5 >= 4) returns true (1)
<=  (X <= Y)   X is less than or equal to Y  (5 <= 4) return false (0)
==  (X == Y)   X is equal to Y  (5 == 4) returns false (0)
!=  (X != Y)   X is not equal to Y  (5 != 4) returns true(1)

Logical Operator

In C programming, logical operators perform logical operations on a given expression by joining two or more various relational and conditional expressions. These logical operators check that if the logical conditions is true or not, and if the conditions are true, it returns 1, otherwise, it returns 0.
  The following table shows all three types of Logical Operators available in C programming:

 Operator  Description Example
&&  Logical AND Operator  (Condition1 && Condition2)
||  Logical OR Operator  (Condition1 || Condition2)
!  Logical NOT Operator  !(Condition1 && Condition2)

Truth table of the Logical AND (&&), OR(||), NOT(!) operator

  X     Y     X && Y     X || Y     !(X && Y)     !(X || Y)  
1 1 1 1 0 0
1 0 0 1 1 0
0 1 0 1 1 0
0 0 0 0 1 1

Ternary or Conditional Operator

In C programming, the ternary or conditional operator is used for decision making in place of longer if and else conditional statements. The ternary operator works on three operands - the first expression is a boolean condition that can be either true or false value. If condition is true then second expression will be return, ot herwise, third expression will be return by the conditional operator.
  Syntax:   (condition ? return_value_if_true : return_value_if_false)

  int x = 5, y = 8, z;
  z = (x < y) ? x : y;
  z is set equal to x, because the condition x < y was true.


Bitwise Operator

The Bitwise Operator in C performs its operation on the data at the bit-level. It is subcategorized into three subtypes based on their working principles, logical (Bitwise AND, OR, and XOR), Shift (Right Shift and left shift), and Complement (Bitwise NOT). Bitwise operators are used in numerical computations to make the calculation process faster.
Following is the list of bitwise operators provided by C programming language:

 Operator   Description Example
If A=11(binary value 1011) and B=7(binary value 0111)
& Binary AND Operator returns a bit to the result if it exists in both operands. (A & B) = 3, i.e., 0011
| Binary OR Operator returns a bit if it exists in either operand. (A | B) = 15, i.e., 1111
^ Binary XOR Operator returns the bit if it is set in one operand but not both. (A ^ B) = 12, i.e., 1100
~ Binary One's Complement Operator is unary and returns opposit bits. (~A ) = ~(11) = 4, i.e,. 0100
<< Binary Left Shift Operator. The value is moved left by the number of bits specified by the right operand. A << 2 = 12 i.e., 1100
>> Binary Right Shift Operator. The value is moved right by the number of bits specified by the right operand. A >> 2 = 2 i.e., 0010

The truth table of the bitwise operators

  A     B    A&B   A|B   A^B   ~A 
0 0 0 0 0 1
0 1 0 1 1 1
1 0 0 1 1 0
1 1 1 1 1 0

Special Operators → sizeof, &, * and comma

 Operator   Description   Example 
 sizeof()   Returns the size of a variable.  sizeof(x), where x is char, will return 1.
 &   Returns the address of a variable.  &x;
 *   Pointer to a variable.  *x;
 ,   Comma Operator combines the two expressions and evaluates them both in left-to-right order.  int i = 10, b = 20, c= 30;


ADVERTISEMENT