# C Operator and Precedence Table

In general, we know that Operator is a person who operates equipment or a machine. for a programming language, Operator is a symbol that tails compiler or an interpreter to perform specific mathematical or logical / relational manipulation. Unlike others programming language C or C++ has a wide range of operator to perform the various operation. To facilitate better understanding about operator we classified the operator as:

• Arithmetic Operators
• Increment and Decrement Operators
• Assignment Operators
• Relational Operators
• Logical Operators
• Conditional (ternary)  Operators
• Bitwise Operators
• Special Operators

## C Arithmetic Operator:

• Addition or Unary Plus (+),
• Subtraction or Unary Minus (-),
• Multiplication (*),
• Division (/),
• Modulus or reminder or a division (%)

Above all are categorized under C arithmetic Operators. It works with two Operand.  The operand is a variable or a constant for example x=a+b here a and b are the operand of the operator (+).

## 1# Example Program  : Using Arithmetic Operators

``` #include <stdio.h>
int main()
{
int a = 10,b = 2, c;
clrscr();
c = a+b;
printf("a+b = %d \n",c);

c = a-b;
printf("a-b = %d \n",c);

c = a*b;
printf("a*b = %d \n",c);

c=a/b;
printf("a/b = %d \n",c);

c=a%b;
printf("Remainder when a divided by b = %d \n",c);

getch();

return 0;
}```

Output

```a+b = 12
a-b = 8
a*b = 20
a/b = 5
Remainder when a divided by b=0

```

## Increment and decrement operators

• Increment operator (++)
• Decrement operator (- -)

In general C programming has increment ++ and decrement — to step up  and step down the value of an operand (constant or variable) by 1.

Assume that you have a variable a holds value 5, then a++ step up the value by 1 after execution it will be 6. similarly, a– step down the value of a by 1 after execution it will be 4.  Here another thing I want to highlight that the operator has two modes one is pre-increment and another is post-increment.

## 2# Example Program: Let’s focus on pre-increment bellow code block:

``` #include<stdio.h>
void main()
{
int a=5;b,c;   //here value of a =5

b=++a;         //here pre-increment the value of a first then
//asign to variable b now value of a is 6 and
// value of b is 6

c=a+b;         // here c= 12

printf("The Value of a=%d, b=%d, C=%d",a,b,c);

getch();

}```

Output:

a=6, b=6, c=12

## 3# Example Program: Now focus on post- increment bellow code block:

```  #include<stdio.h>
void main()
{
int a=5;b,c;   //here value of a =5

b=a++;         //here post-increment occur the value of a
// first assign to b then increase by 1
// now value of b is 5 and value of a is 6

c=a+b;         // here c= 11

printf("The Value of a=%d, b=%d, C=%d",a,b,c);

getch();

}```

Output:

a=6, b=5, c=11

## C Assignment Operators

An assignment operator is used for assigning a value to a variable. The most common assignment operator is =

Pictorial Representation : ## Shorthand technique of assignment operator:

A shorthand operator is a shorter way to express an assignment expression.

Operator Example Same as
= a = b a = b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b

## 4# Example Program: Using assignment operator

```#include <stdio.h>
int main()
{
int a = 5, c;

c = a;
printf("c = %d \n", c);

c += a; // c = c+a
printf("c = %d \n", c);

c -= a; // c = c-a
printf("c = %d \n", c);

c *= a; // c = c*a
printf("c = %d \n", c);

c /= a; // c = c/a
printf("c = %d \n", c);

c %= a; // c = c%a
printf("c = %d \n", c);

return 0;
}```

Output

```c = 5
c = 10
c = 5
c = 25
c = 5
c = 0```

## C Relational Operators

A relational operator find the relation between tow operand (Variable or Constant). If the relation is true, it returns 1; if the relation is false, it returns value 0. The Relational operators are mostly used in decision making and loops.

Operator Meaning of Operator Example
== Equal to 7 == 2 returns 0
> Greater than 7 > 2 returns 1
< Less than 7 < 2 returns 0
!= Not equal to 7 != 2 returns 1
>= Greater than or equal to 7 >= 2 returns 1
<= Less than or equal to 7 <= 2 return 0

## 5# Example Program:Using Relational Operator

``` #include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10;

printf("%d == %d = %d \n", a, b, a == b); // the expression returns true
printf("%d == %d = %d \n", a, c, a == c); // the expression returns false

printf("%d > %d = %d \n", a, b, a > b); //the expression returns false
printf("%d > %d = %d \n", a, c, a > c); //the expression returns false

printf("%d < %d = %d \n", a, b, a < b); //the expression returns false
printf("%d < %d = %d \n", a, c, a < c); //the expression returns true

printf("%d != %d = %d \n", a, b, a != b); //the expression returns false
printf("%d != %d = %d \n", a, c, a != c); //true

printf("%d >= %d = %d \n", a, b, a >= b); //the expression returns true
printf("%d >= %d = %d \n", a, c, a >= c); //false

printf("%d <= %d = %d \n", a, b, a <= b); //the expression returns true
printf("%d <= %d = %d \n", a, c, a <= c); //the expression returns true

return 0;

}```

Output

```5 == 5 = 1
5 == 10 = 0
5 > 5 = 0
5 > 10 = 0
5 < 5 = 0
5 < 10 = 1
5 != 5 = 0
5 != 10 = 1
5 >= 5 = 1
5 >= 10 = 0
5 <= 5 = 1
5 <= 10 = 1 ```

## C Logical Operators

These operators are used to perform logical operations on the given expressions. There are 3 logical operators in C language. They are, logical AND (&&), logical OR (||) and logical NOT (!).

Operator Meaning of Operator Example
&& Logial AND. True only if all operands are true If c = 5 and d = 2 then, expression `((c == 5) && (d > 5))` equals to 0.
|| Logical OR. True only if either one operand is true If c = 5 and d = 2 then, expression `((c == 5) || (d > 5))` equals to 1.
! Logical NOT. True only if the operand is 0 If c = 5 then, expression `! (c == 5)` equals to 0.

### Circuit Diagram of And (&&) Operator ### Circuit Diagram of OR (||) Operator ### Circuit Diagram of OR (!) Operator ## 6# Example Program:Using Logical Operator

```#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;

clrscr();

result = (a = b) && (c > b);     // The expression returns True
printf("(a = b) && (c > b) equals to %d \n", result);

result = (a = b) && (c < b);     // The expression returns Fasle
printf("(a = b) && (c < b) equals to %d \n", result);

result = (a = b) || (c < b);     // The expression returns True
printf("(a = b) || (c < b) equals to %d \n", result);

result = (a != b) || (c < b);    // The expression returns False
printf("(a != b) || (c < b) equals to %d \n", result);

result = !(a != b);              // The expression returns True
printf("!(a == b) equals to %d \n", result);

result = !(a == b);            // The expression returns False
printf("!(a == b) equals to %d \n", result);

return 0;
getch();
}```

Output

```(a = b) && (c > b) equals to 1
(a = b) && (c < b) equals to 0
(a = b) || (c < b) equals to 1
(a != b) || (c < b) equals to 0
!(a != b) equals to 1
!(a == b) equals to 0 ```

## Conditional (ternary)  Operators

The conditional (ternary) operator  is a shortcut of If statement in c. It operates three operands thus it called ternary operator. The basic syntax of a Conditional operator is   conditionexpression 1expression 2 ## 7# Example Program:Using Conditional (Ternary ) Operator

```#include<stdio.h>
void main();
{
age, flag;
clrscr();
printf("Enter Your Age :);
scanf("%d",&age);
age>=18? flag=1 : flag=0;
if(flag==1)
printf("You are Young and Adult);
else
printf("You are  junior ");
getch();

}```

Output:

Enter Your Age: 12

You are junior

## Bitwise Operators

As we discuss earlier post that, C is a middle-level language which has both high and low-level features. Here you can perform the bit-level operation using Bitwise operator.

C provides six operators for bit manipulation these are:

 Symbol Operator & bitwise AND | bitwise inclusive OR ^ bitwise XOR (eXclusive OR) << left shift >> right shift ~ bitwise NOT (one’s complement) (unary)

## 8# Example Program:Using bitwise shift operator

``` #include <stdio.h>
#include <conio.h>

void showbits(unsigned int x)
{
int i;
for(i=(sizeof(int)*8)-1; i>=0; i--)
(x&(1u<<i))?putchar('1'):putchar('0');

printf("\n");
}

int main()
{
int j = 5225, m, n;
printf("The decimal %d is equal to binary - ", j);
/* assume we have a function that prints a binary string when given
a decimal integer
*/
showbits(j);

/* the loop for right shift operation */
for ( m = 0; m <= 5; m++ ) {
n = j >> m;
printf("%d right shift %d gives ", j, m);
showbits(n);
}
return 0;
}```

The output of the above program will be

```  The decimal 5225 is equal to binary - 0001010001101001
5225 right shift 0 gives 0001010001101001
5225 right shift 1 gives 0000101000110100
5225 right shift 2 gives 0000010100011010
5225 right shift 3 gives 0000001010001101
5225 right shift 4 gives 0000000101000110
5225 right shift 5 gives 0000000010100011```

To better understand bitwise operator you have to learn boolean algebra and truth table of the gate.

### Bitwise AND

This makes more sense if we apply this to a specific operator. In C/C++/Java, the & operator is bitwise AND. The following is a chart that defines &1, defining AND on individual bits.

 xi yi xi &1 yi 0 0 0 0 1 0 1 0 0 1 1 1

For instance, working with a byte (the char type):

```     01001000 &
10111000 =
--------
00001000```

We can do an example of bitwise &. It’s easiest to do this on 4-bit numbers, however.

 Variable b3 b2 b1 b0 x 1 1 0 0 y 1 0 1 0 z = x & y 1 0 0 0

### Bitwise OR

The | operator is bitwise OR (it’s a single vertical bar). The following is a chart that defines |1, defining OR on individual bits.

 xi yi xi |1 yi 0 0 0 0 1 1 1 0 1 1 1 1

Similar to bitwise AND, bitwise OR only operates at the bit level. Its result is a 1 if one of the either bits is 1 and zero only when both bits are 0. Its symbol is ‘|’ which can be called a pipe.

```     11001110
| 10011000
= 11011110```

We can do an example of bitwise |. It’s easiest to do this on 4-bit numbers, however.

 Variable b3 b2 b1 b0 x 1 1 0 0 y 1 0 1 0 z = x | y 1 1 1 0

### Bitwise XOR

The ^ operator is bitwise XOR. The usual bitwise OR operator is inclusive OR. XOR is true only if exactly one of the two bits is true. The XOR operation is quite interesting, but we defer talking about the interesting things you can do with XOR until the next set of notes.

The following is a chart that defines ^1, defining XOR on individual bits.

 xi yi xi ^1 yi 0 0 0 0 1 1 1 0 1 1 1 0

We can do an example of bitwise ^. It’s easiest to do this on 4-bit numbers, however.

 Variable b3 b2 b1 b0 x 1 1 0 0 y 1 0 1 0 z = x ^ y 0 1 1 0

### Bitwise NOT

There’s only one unary bitwise operator, and that’s bitwise NOT. Bitwise NOT flips all of the bits. There’s not that much to say about it, other than it’s not the same operation as the unary minus.The following is a chart that defines ~1, defining NOT on an individual bit.

 xi ~1 xi 0 1 1 0

We can do an example of bitwise ~. It’s easiest to do this on 4 bit numbers (although only 2 bits are necessary to show the concept).

 Variable b3 b2 b1 b0 x 1 1 0 0 z = ~x 0 0 1 1

### Right shift >>

The symbol of right shift operator is >>. For its operation, it requires two operands. It shifts each bit in its left operand to the right. The number following the operator decides the number of places the bits are shifted (i.e. the right operand). Thus by doing ch >> 3 all the bits will be shifted to the right by three places and so on.

Example:

If the variable ch contains the bit pattern 11100101, then ch >> 1 will produce the result 01110010, and ch >> 2 will produce 00111001.
Here blank spaces are generated simultaneously on the left when the bits are shifted to the right. When performed on an unsigned type, the operation performed is a logical shift, causing the blanks to be filled by 0s (zeros). When performed on a signed type, the result is technically undefined and compiler dependent, however most compilers will perform an arithmetic shift, causing the blank to be filled with the sign bit of the left operand.

Right shift can be used to divide a bit pattern by 2 as shown:

i = 14; // Bit pattern 1110
j = i >> 1; // here we have the bit pattern shifted by 1 thus we get 111 = 7 which is 14/2

### Left shift <<

The symbol of left shift operator is <<. It shifts each bit in its left-hand operand to the left by the number of positions indicated by the right-hand operand. It works opposite to that of right shift operator. Thus by doing ch << 1 in the above example we have 11001010. Blank spaces generated are filled up by zeroes as above.

Left shift can be used to multiply an integer in multiples of 2 as in

int i = 4; /* bit pattern equivalent is 100 */
int j = i << 2; /* makes it 10000, which multiplies the original number by 4 i.e. 16 */

### Bitwise assignment operators

C provides a compound assignment operator for each binary arithmetic and bitwise operation (i.e. each operation which accepts two operands). Each of the compound bitwise assignment operators performs the appropriate binary operation and store the result in the left operand.

The bitwise assignment operators are as follows:

Symbol Operator
`&=` bitwise AND assignment
`|=` bitwise inclusive OR assignment
`^=` bitwise exclusive OR assignment
`<<=` left shift assignment
`>>=` right shift assignment

### Uses of Bitwise Operations

Occasionally, you may want to implement a large number of Boolean variables, without using a lot of space.

A 32-bit int can be used to stored 32 Boolean variables. Normally, the minimum size for one Boolean variable is one byte. All types in C must have sizes that are multiples of bytes. However, only one bit is necessary to represent a Boolean value.

You can also use bits to represent elements of a (small) set. If a bit is 1, then element i is in the set, otherwise it’s not.You can use bitwise AND to implement set intersection, bitwise OR to implement set union.

### Facts About Bitwise Operators

Consider the expression x + y. Do either x or y get modified? The answer is no.

Most built-in binary operators do not modify the values of the arguments. This applies to logical operators too. They don’t modify their arguments.

There are operators that do assignment such as +=, -=, *=, and so on. They apply to logical operators too. For example, |=, &=, ^=. Nearly all binary operators have a version with = after it.

### Summary

Bitwise operators only work on limited types: int and char (and variations of int). You can, with some casting, make it work on other types. These operators, in conjunction with bitshift operators, allow you to access and modify bits.

## Special Operators

Pointer(*), sizeof, comma(,) following you can categorise under special operator.

Sizeof Operator

The `sizeof` is an unary operator which returns the size of data (constant, variables, array, structure etc). it is widely used when you have to dynamic allocation of memory in your program.

``` #include <stdio.h>
int main()
{
int a, e;
float b;
double c;
char d;
printf("Size of int=%lu bytes\n",sizeof(a));
printf("Size of float=%lu bytes\n",sizeof(b));
printf("Size of double=%lu bytes\n",sizeof(c));
printf("Size of char=%lu byte\n",sizeof(d));
printf("Size of integer type array having 10 elements = %lu bytes\n", sizeof(e));
return 0;
}```

Output

```Size of int = 4 bytes
Size of float = 4 bytes
Size of double = 8 bytes
Size of char = 1 byte
Size of integer type array having 10 elements = 40 bytes```
```#include <stdio.h>
#include <stdlib.h>
int main(){
int n,i,*ptr,sum=0;
printf("Enter number of elements: ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //memory allocated using malloc
if(ptr==NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
printf("Enter elements of array: ");
for(i=0;i<n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}```

In general, we use static allocation of memory in the declaration section of C program, but in a large project, you need to allocate memory at any moment and any point of your project. The above program we use malloc function that dynamically allocates memory and the sizeof operator tells malloc function that how much memory should allocate.

Pointer

A pointer is a variable which contains the address in memory of another variable. We can have a pointer to any variable type.

The unary or monadic operator & gives the “address of a variable”.

The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.

Declaration of  a pointer variable:

int *pointer;

## Unary, Binary, Ternary Operators in C

 Operator Name Type ! Logical NOT Unary & Address-of Unary ( ) Cast Operator Unary * Pointer dereference Unary + Unary Plus Unary ++ Increment Unary – Unary negation Unary –– Decrement 1 Unary ~ complement Unary , Comma Binary != Inequality Binary % Modulus Binary %= Modulus assignment Binary & Bitwise AND Binary && Logical AND Binary &= Bitwise AND assignment Binary * Multiplication Binary *= Multiplication assignment Binary + Addition Binary += Addition assignment Binary – Subtraction Binary –= Subtraction assignment Binary –> Member selection Binary –>* Pointer-to-member selection Binary / Division Binary /= Division assignment Binary < Less than Binary << Left shift Binary <<= Left shift assignment Binary <= Less than or equal to Binary = Assignment Binary == Equality Binary > Greater than Binary >= Greater than or equal to Binary >> Right shift Binary >>= Right shift assignment Binary ^ Exclusive OR Binary ^= Exclusive OR assignment Binary | Bitwise inclusive OR Binary |= Bitwise inclusive OR assignment Binary || Logical OR Binary ? : Conditional Operators Ternary

# C Operator Precedence Table

Here is the lists of C operators in order to precedence (highest to lowest). Their associativity indicates in what order operators of equal precedence in an expression are applied.

Operator

Description

Associativity

( )
[ ]
.
->
++ —
Parentheses (function call) (see Note 1)
Brackets (array subscript)
Member selection via object name
Member selection via pointer
Postfix increment/decrement (see Note 2)

left-to-right

++ —
+ –
! ~
(type)
*
&
sizeof
Prefix increment/decrement
Unary plus/minus
Logical negation/bitwise complement
Cast (convert value to temporary value of type)
Dereference
Determine size in bytes on this implementation
right-to-left
*  /  % Multiplication/division/modulus left-to-right
+  – Addition/subtraction left-to-right
<<  >> Bitwise shift left, Bitwise shift right left-to-right
<  <=
>  >=
Relational less than/less than or equal to
Relational greater than/greater than or equal to
left-to-right
==  != Relational is equal to/is not equal to left-to-right
& Bitwise AND left-to-right
^ Bitwise exclusive OR left-to-right
| Bitwise inclusive OR left-to-right
&& Logical AND left-to-right
| | Logical OR left-to-right
? : Ternary conditional right-to-left
=
+=  -=
*=  /=
%=  &=
^=  |=
<<=  >>=
Assignment
Multiplication/division assignment
Modulus/bitwise AND assignment
Bitwise exclusive/inclusive OR assignment
Bitwise shift left/right assignment
right-to-left

,

Comma (separate expressions) left-to-right
Note 1:
Parentheses are also used to group sub-expressions to force a different precedence; such parenthetical expressions can be nested and are evaluated from inner to outer.
Note 2:
Postfix increment/decrement have high precedence, but the actual increment or decrement of the operand is delayed (to be accomplished sometime before the statement completes execution). So in the statement y = x * z++; the current value of z is used to evaluate the expression (i.e., z++ evaluates to z) and z only incremented after all else is done.
The following two tabs change content below.

#### Subroto Mondal

Chief Coordinator HR&CR
I like Programming and New Technologies. And work with Linux.

#### Latest posts by Subroto Mondal (see all)

This site uses Akismet to reduce spam. Learn how your comment data is processed.