C Operators

Operators and Operands

Operands  

  • Operands are numerical, text and Boolean values that a program can manipulate and use as logical guidelines for making decisions.
  • Operators are used to manipulate and check operand values.
  • If the operator manipulates one operand it is known as unary operator; if the operator manipulates two operands, it is known as binary operator and if the operator manipulates three operands, it is known as ternary operator.
  • C has following operators 
  1.  Arithmetic operators  all are binary operators 
  2. Logical operators  all are binary operators 
  3. Relational operators  all are binary operators 
  4. Bitwise operators  all are binary operators except ! and ~ 
  5. Increment, decrement operators  unary operators 
  6. Assignment operators  binary operators 
  7. Conditional operator  ternary operator 
  8. Type cast operator unary operator 
  9. sizeof operator  unary operator 
  10. Comma operator  binary operator
Arithmetic Operators

  1. A+B - adds A with B; 
  2. A-B - subtracts B from A; 
  3. A*B - multiplies A by B; 
  4. A/B - divides A by B; 
  5. I%J - gives the remainder of I divided by J. Here I and J are expressions of any integer data type;
Ex:
#include 
int main (void) 
int a = 100; 
int b = 2; 
int c = 25; 
int d = 4; 
int result; result = a - b; 
printf ("a - b = %i\n", result); 
result = b * c; 
printf ("b * c = %i\n", result); 
result = a / c; printf ("a / c = %i\n", result); 
result = a + b * c; 
printf ("a + b * c = %i\n", result); 
printf ("a * b + c * d = %i\n", a * b + c * d); 
return 0; 
}

Logical Operators 
  1. A && B - has the value 1 if both A and B are nonzero, and 0 otherwise (and B is evaluated only if A is nonzero);
  2. A || B - has the value 1 if either A or B is nonzero, and 0 otherwise (and B is evaluated only if A is zero);
  3. !A - has the value 1 if a is zero, and 0 otherwise.
Ex:-
#include 
int main() 
int a = 5; int b = 20; int c = 1; 
printf( "( a && b ) = %d \n", a && b ); 
printf( "( a || b ) = %d \n", a || b ); 
printf( " !a = %d \n", !a ); 
return 0; 
}

Relational Operators

  1. A < B has the value 1 if A is less than B, and 0 otherwise; 
  2. A <= B - has the value 1 if A is less than or equal to B, and 0 otherwise;
  3. A > B has the value 1 if A is greater than B, and 0 otherwise; 
  4. A >= B - has the value 1 if A is greater than or equal to B, and 0 otherwise;
  5. A == B has the value 1 if A is equal to B, and 0 otherwise; 
  6. A != B has the value 1 if A is not equal to B, and 0 otherwise;
Ex:-

#include 
int main() 
int a = 5; 
int b = 20; 
int c = 5; 
printf( "( a < b ) = %d \n", a < b ); 
printf( "( a <= b ) = %d \n", a <= b ); 
printf( "( a > b ) = %d \n", a > b ); 
printf( "( a >= b ) = %d \n", a >= b ); 
printf( "( c == a ) = %d \n", c == a ); 
printf( "( a != b ) = %d \n", a != b ); 
return 0; 
}
 
Bitwise Operators

  1. A & B - performs a bitwise AND of A and B; 
  2. A | B - performs a bitwise OR of A and B; 
  3. A ^ B - performs a bitwise XOR of A and B; 
  4. ~A - takes the bitwise complement of A; 
  5. A << n - shifts A to the left n bits; 
  6. A >> n - shifts A to the right n bits;

  • Left shift 
  1.  212 = 11010100 (In binary) 
  2. 212<<1 = 110101000 (In binary) [Left shift by one bit] 
  3. 212<<0 =11010100 (Shift by 0) 
  4. 212<<4 = 110101000000 (In binary) =3392(In decimal) 
  • Right shift  
  1. 212 = 11010100 (In binary) 
  2. 212>>2 = 00110101 (In binary) [Right shift by two bits] 
  3. 212>>7 = 00000001 (In binary) 
  4.  212>>8 = 00000000 
  5. 212>>0 = 11010100 (No Shift)

• Assume A = 60 and B = 13 in binary format, they will be as follows: 

A = 0011 1100 
B = 00001101 

A&B = 0000 1100 
A|B = 0011 1101 
A^B = 0011 0001 
~A = 1100 0011 
A << 2 = 240 i.e., 1111 0000 
A >> 2 = 15 i.e., 0000 1111

Ex:-
#include 
int main() 
{
 int a = 60; int b = 13; 
printf( "( a & b ) = %d \n", a & b ); 
printf( "( a | b ) = %d \n", a | b ); 
printf( "( a ^ b ) = %d \n", a ^ b ); 
printf( "( ~ a ) = %d \n", ~a ); 
printf( "( a << 2 ) = %d \n", a << 2); 
printf( "( a >> 2 ) = %d \n", a >> 2); 
return 0; 
}


Increment and Decrement Operators 

  1. ++A  - increments A and then uses its value as the value of the expression;
  2. A++  - uses A as the value of the expression and then increments A;
  3. --A  - decrements A and then uses its value as the value of the expression;
  4. A-- - uses A as the value of the expression and then decrements A;
Ex:-

#include 
int main() 
int a = 21; 
int c =10;; c = a++; 
printf( "Line 1 - Value of a and c are : %d - %d\n", a, c); c = ++a; 
printf( "Line 2 - Value of a and c are : %d - %d\n", a, c); c = a--; 
printf( "Line 3 - Value of a and c are : %d - %d\n", a, c); c = --a; 
printf( "Line 4 - Value of a and c are : %d - %d\n", a, c); 
return 0; 
}

Assignment Operators 
• A = B  stores the value of B into A; 
• A op= B applies op to A and B, storing the result in A.

  1. = - C = A + B will assign the value of A + B to C
  2. += - C += A is equivalent to C = C + A
  3. *= - C *= A is equivalent to C = C * A
  4. /= - C /= A is equivalent to C = C / A
  5. %= - C %= A is equivalent to C = C % A
  6. <<= - C <<= 2 is same as C = C << 2
  7. >>= - C >>= 2 is same as C = C >> 2
  8. &= - C &= 2 is same as C = C & 2
  9. ^= - C ^= 2 is same as C = C ^ 2
  10. |=  - C |= 2 is same as C = C | 2
Ex:-
#include 
int main() 
int a = 21; 
int c ; c = a; 
printf("Line 1 - = Operator Example, Value of c = %d\n", c ); c += a; 
printf("Line 2 - += Operator Example, Value of c = %d\n", c ); c -= a; 
printf("Line 3 - -= Operator Example, Value of c = %d\n", c ); c *= a; 
printf("Line 4 - *= Operator Example, Value of c = %d\n", c ); c /= a; 
printf("Line 5 - /= Operator Example, Value of c = %d\n", c ); c = 200; c %= a; 
printf("Line 6 - %= Operator Example, Value of c = %d\n", c ); c <<= 2; 
printf("Line 7 - <<= Operator Example, Value of c = %d\n", c ); c >>= 2; 
printf("Line 8 - >>= Operator Example, Value of c = %d\n", c ); c &= 2; 
printf("Line 9 - &= Operator Example, Value of c = %d\n", c );  c ^= 2; 
printf("Line 10 - ^= Operator Example, Value of c = %d\n", c ); c |= 2; 
printf("Line 11 - |= Operator Example, Value of c = %d\n", c ); 
return 0; 
}

Conditional Operator 
• Given that A, B, C are expressions; then the expression 
A ? B : C 
has as its value B if A is nonzero, and C otherwise; only expression B or C is evaluated. 
In order to generate 1 or 0 for A, it should be a relational or logical expression.

• Expressions B and C must be of the same data type.


Ex:-
#include 
int main() 
int num; 
int flag; printf("Enter the Number : "); 
scanf("%d",&num); 
flag = ((num%2==0)?1:0); 
printf ("Flag value : %d \n", flag); 
return 0; 
}

Type Cast Operator 
• You can convert the values from one type to another explicitly using the cast operator as follows: (type_name) expression

sizeof Operator 

• Given that type is as the name of a basic data type, an enumerated data type (preceded by the keyword enum), a typedef-defined type, or is a derived data type; A is an expression; then the expression 

sizeof (type) has as its value the number of bytes needed to contain a value of the specified type; 

sizeof A  has as its value the number of bytes required to hold the result of the evaluation of A.

Ex:-
#include 
int main() 
int ivar = 100; 
char cvar = 'a'; 
float fvar = 10.10; 
double dvar = 18977670.10; 
printf("%d\n", sizeof(ivar)); 
printf("%d\n", sizeof(cvar)); 
printf("%d\n", sizeof(fvar)); 
printf("%d\n", sizeof(dvar)); 
return 0; 
}

Comma Operator
• The comma operator can be used to link the related expressions together. 
• A comma linked expression is evaluated from left to right and the value of the right most expression is the value of the combined expression.
x = (a = 2, b = 4, a+b)
• Comma operator returns the value of the rightmost operand when multiple comma operators are used inside an expression.
• Comma operators are commonly used in for loops, while loops, while exchanging values.
• Comma Operator Can acts as : 
  1. Operator : In the Expression 
  2. Separator : Declaring Variable , In Function Call Parameter List

Ex:-
#include 
int main() 
int num1 = 1, num2 = 2; 
int res, x, a, b; 
x = (a = 2, b = 4, a+b); 
res = (num1, num2); 
printf("%d\n", res); 
x = (a = 2, b = 4, a+b); 
printf("%d\n", x); 
return 0; 
}

C Arithmetic Expression 
• Arithmetic expression in C is a combination of variables, constants and operators written in a proper syntax. 

Operator Precedence 
When an expression contains multiple operators, the precedence of the operators controls the order in which the individual operators are evaluated.For example, the expression x + y * z is evaluated as x + (y * z) because the * operator has higher precedence than the binary + operator. The precedence of an operator is established by the definition of its associated grammar production.

Operator Associativity 
A higher precedence operator is evaluated before a lower precedence operator. If the precedence levels of operators are the same, then the order of evaluation depends on their associativity (or, grouping).























Comments