C Operators – Tutorial For Beginners | C Programming

C operators – with examples and sample programs. Here we cover the complete step by step information on C operators along with syntax, different operators and examples related to it. Do check it out.

• What are C Operators?

Operators are symbols used to perform a specific operation.

Generally operators work on its operands and give a value as the result. Most of the operators (binary operators) work on 2 operands and a few operators (unary operators) work on a single operand and we have one operator (ternary operator) that work on 3 operands.

Similar to mathematics, in programming languages also we use some symbols to represent an operation. Suppose we want to perform an addition, then we use the symbol +.

Similarly to perform a subtraction, we use the symbol –. These symbols are known as operators and we have different types of operators in java.

Different Types of C Operations With Examples

Arithmetic Operators :

• +             for sum after addition
• –              for difference after subtraction
• *             for product after multiplication
• /              for quotient after division
• %            for remainder after division

Arithmetic’s is the basic mathematics part and addition (+), subtraction (-), multiplication (*) and division (/) are the basic mathematical operations.

The operators related to these operations are known as arithmetic operators.

In programming the results (quotient and remainder) of a division are very much important.

At many places we use the remainder value after a division. So a separate symbol (%) is used for finding the remainder. The normal slash (/) is used to find the quotient.

Note: in C language, % symbol cannot be applied on float and double type data.

Examples:

• 13 + 10  is 23
• 13 – 10   is 3
• 13 * 10  is 130
• 13 / 10   is 1
• 13 % 10 is 3
• 13 / 10.0               is 1.3
• 13 % 10.0             is NOT VALID
• 123 / 5                   is 24
• 123 % 5                 is 3
• 123.0 / 5               is 12.3

Relational Operators –

•             less than
• <=           less than or equal
• >             greater than
• >=           greater than or equal
• ==           equality check
• !=            inequality check

These operators compare two values and give true or false as the result. In C, true is represented by 1 and false is represented by 0. So the result of a relational operator is also an integer (but either 1 or 0) only.

Examples for Relational Operators:

• 13>10    is 1
• 13<10    is 0
• 13<=10 is 0
• 13>=10 is 0
• 13==10 is 0
• 13!=10  is 1
• 13<13    is 0
• 13>13    is 0
• 13==13 is 1
• 13<=13 is 1
• 13>=13 is 1
• 13!=13  is 0
•  ‘A’==65 is 1
• ‘0’==48 is 1
• ‘2’==2   is 0

Logical Operators –

• &&         logical AND
• ||           logical OR
• !              logical NOT

Logical operators also result in true (1) or false (0) values. These operators are used to check multiple conditions as a single unit.

• When we want the unit value to be true only when both the conditions are true, then we should combine the conditions using &&
• When we want the unit value to be true when either of the conditions is true, then we should combine the conditions using ||

To invert the truth value of a condition, we should use !

• 10<20 && 10<30               is 1
• 10<20 && 10<5                  is 0
• 10<2 && 2>5                      is 0
• 10<20 || 10<30                 is 1
• 10<20 || 10<5                    is 1
• 10<2 || 2>5                        is 0
• !(10<20)                               is 0
• 10 && 20                              is 1
• ‘A’ && -4                              is 1
• 10 && 0                                is 0
• 10<20 && 20<30 && 30<40           is 1

Truth table for logical operators

A             B             A && B                 A || B                    ! A

1              1                  1                              1                          0

1              0                  0                              1                          0

0              1                  0                              1                          1

0              0                  0                              0                          1

Conditional Operator (Ternary Operator) :

?:

An operator that works on 3 operands is known as ternary operator. The conditional operator is the only ternary operator the C language has.

As this operator works on truth value of a condition (first operand), this operator is known as conditional operator. In a = 10+20, result of 10+20 is assigned to a. similarly in a = 10==20?30:40, the result of 10==20?30:40 will be assigned to a. in this expression, 10==20 is the first operand, 30 is the second operand and 40 is the third operand.

The question mark (?) is placed between first and second operands and colon (:) is placed between second and third operands. The actual result of the expression is either second operand or the third operand.

This decision is taken based on the first operand which gives true (1) or false (0) as result. If it results in true, the second operand will be the result and if it results in false, the third operand is the result.

• In our example 10==20 is false, so the third operand (40) will be the result. So the value 40 is assigned to a.

similarly, if we take an expression a = 25<40?123:456 then second operand (123) will be assigned to ‘a’ as the result of condition (first operand) is true.

Eg:

• 16==16 ? 13 : 10                result will be 13
• 16!=16 ? 13 : 10                 result will be 10
• 10<13 ? 10 : 13                   result will be 10
• In this operation, both the second operand and third operand should be of same type.

Grouping Operators –

• ( )            unit operator
• [ ]            array operator
• .               referencing operator

( ) can be used to group a set of elements as a single unit. When we have an expression like 2+3*4, as per normal priorities 3*4 is performed first and the result (12) is added to 2. If we group 2+3 as a single unit and write the same expression like (2+3)*4 then that single unit (result 5) is multiplied with 4 and results in 20.

Eg:

[ ] are used to work with arrays. While creating an array or to access element of an array this square brackets set is used.

Eg:

• . (dot) operator is used to access elements/members of a structure.

Eg:

Bitwise Operators –

• &             bitwise AND
• |              bitwise OR
• ^             bitwise XOR
•             bitwise NOT (negation)
• <<           left shift
• >>           right shift

Truth table for bitwise operators

A             B                A & B                            A | B                      A ^ B                      ~ A

1              1                 1                              1                             0                              0

1              0                 0                              1                              1                             1

0              1                 0                              1                              1                             0

0              0                 0                              0                              0                             1

These operators work on individual bits of the numbers and generally used for masking purposes.

& is used to perform bitwise AND operation on the given two values.

• If we want to perform 25&78, then AND operation is performed on corresponding bits of the numbers and the result is given.

Ex:

• 25 ->      00000000 00011001
• 78 ->      00000000 01001110
• 00000000 00001000 -> 8

So 25 & 78 is 8

Similarly, 25 | 78 is

• 25 ->      00000000 00011001
• 78 ->      00000000 01001110
• 00000000 01011111 -> 95

25^78 is 87

EX:

• 25 ->      00000000 00011001
• 78 ->      00000000 01001110

00000000 01010111 -> 87

Left Shift Operator :

(<<) is used to shift each bit towards left to the specified number of locations.

Taking an example 25, its bit pattern (taking 2 bytes) will be 00000000 00011001. If we shift 4 locations towards left, then the bits will become 00000001 10010000.

When we perform a left shift for n locations, n bits at left end will be removed and n 0s will be added at right end.

• 00000000 00011001 is 25
• 00000001 10010000 is 400
• So we can say 25<<4 is 400
• Similarly, 6 << 8 will be 1536
• 00000000 00000110 is 6
• 00000110 00000000 is 1536

Instead of doing all these calculations by writing bits we can find the value by using the formula :

x<<y      can be understood as    x*2y

Ex:

• 25<<4    as            25*24       is             400
• 6<<8      as            6*28           is             1536

Right Shift Operator :

Shifts bits towards right for the specified number of locations.

If we shift for n locations, the right most n bits will be removed and n 0s will be added for positive numbers and 1s will be added for negative numbers at left end. Even for this we can apply a formula:

x>>y      as

Example:

• 25>>2    as            25/22     is             6
• 6>>8      as            6/28        is             0
• 500>>1 as            500/21   is             250
• -500>>3 as          -500/23 is             -63 (yes, it is -63 but not -64 and not -62)
• -15>>2  as            -15/22    is             -4
• -5>>1    as            -5/21      is             -3
• 5>>1      as            5/21        is             2 (yes, 5 is positive, so result is 2)

Negation Operator :

(~) is used to invert all bits reversed it means all 1s will become 0s and all 0s will become 1s.

suppose take 25 and its bits are 00000000 00011001 and when all bits are reversed the bit sequence will be come 11111111 11100110 whose value is -26. Similarly when we negate 87 the result would be -88. Similarly -92 will become 91 when reversed.

 A ~A 0 -1 1 -2 2 -3 … … 32767 -32768

Note: They cannot be applied on float and double type of data.

Increment and Decrement Operators :

• ++           increment
• —             decrement

These are used to increment/decrement a value by 1. Generally to increment a value in the variable x by 1 we use the statement like x=x+1;

The same statement can be written like x+=1; also using combined assignment operator. Alternatively we can use x++ or ++x to perform the same thing. Similarly, x=x-1; can be written as –x or x– also.

These operators cannot be applied on constants.

Example:

• b++        is valid
• 8++        is not valid
• c++++   is not valid
• x++ + y++            is valid

Generally unary operators are used in prefix notation only but the increment/decrement operators can be used both in prefix and postfix notations.

Example:

• -a            +a           ~a           !a            are valid
• a-                  a+           a~           a!            are not valid
• a++        a–          ++a        –a          all are valid

There is a small difference between behaviour of prefix and postfix notations.

When ++ is used in prefix notation, the increment takes place first on the variable and then the resulting value will be used in the expression.

Example:

• int a=5;
• int b=++a; // a is incremented first (so that it becomes 6) and then that 6 is assigned to b.

Example -2:

int x=5;

Int y=++x + ++x; // with the first ++x, a incremented to 6 and the 6 is used in place of that ++x. with the second ++x, x is incremented to 7 and that 7 is used in place of that.

So by the time both the ++ operations are completed the expression will become y= 6+7; so y becomes 13.

When ++ is used in the postfix notation, the original value (before increment) is used in the expression first then increment will take place.

Eg1:        int a=5;

int b=a++; here original value of a (5) is used as value of the expression a++ and then a is incremented. So the value 5 is assigned to b and value of a will be 6.

Eg2:        int x=5;

Int y=x++ + x++; here, with the first x++, value of the expression becomes 5 and x is incremented to 6. With the second x++, value of expression will be 6 (by that time x is 6) and x is incremented to 7. So the expression will become y=5+6; so 11 is assigned to y.

Based on the number of operands associated with an operator, operators are divided into 3 types

• Unary operators
• Binary operators
• Ternary operators

The operators that work on single operand are known as unary operators.

Eg:          ++a        –a          +a           -a            ~a           !a            etc

The operators that work on two operands are known as binary operators.

Eg:          a+b        a-b         a*b        a&b        a^b        a<<b      a=b        a==b      a<=b      a&&b    etc

The operator that works on three operands is known as ternary operator. We have only one ternary operator in Java. As it works on a condition, it is also known as conditional operator.

Eg:          a?b:c

Assignment Operators:

=             +=           -=            *=           /=           %=          <<=        >>=        &=          |=           ^=

We assign result of an expression to a variable and for that purpose we use a normal assignment (=) operator.

Eg:          a = 10;

a = 10+20;

a = 2+3*4-5;

If we want to perform an operation on a variable and assign the result to that variable itself, then we can use a shortcut operator which is known as combined assignment operator. When we want to write an expression like a=a+b;  we can write it as a+=b;

Eg:

• a=a+b;                                  //  a+=b;
• len=len*3;          //  len*=3;
• cv=cv+exp1*exp2   //   cv+=exp1*exp2;
• n=n/10;                //   n/=10;
• val=val>>n;         //   val>>=n;
• a=a&b;                 //   a&=b;