C - Operators and Types of Operators

C language has different character sets and symbols. There are many special characters with special meaning. Operators are special symbols which cause to perform mathematical or logical operations. Following are the symbols used for arithmetic operators in C.

C language has different character sets and symbols. There are many special characters with special meaning. Operators are special symbols which cause to perform mathematical or logical operations. Following are the symbols used for arithmetic operators in C.

Operators
Meaning
+
Addition
-
Subtraction
*
Multiplication
/
Division
%
Remainder
% operator is also referred as modulus operator. It generates remainder after the integer division. The second data supplied for the / and % operator should be non zero. Operators separate identifiers in a statement/expression.
a + b = a a and b operands + operator
Operators act upon data items (variable, constants). The data items upon which operation is performed are called operands. Some operators required a single operand, while others may required two or even three operands. Operators which take only one operand are called unary (nomadic) operators and operators which take tow operands are called binary (dyadic) operators. Examples of operations are
  1. a + b  + takes two operands 'a" and 'b'. So it is binary operator 
  2. -b       i takes one operand. So it is unary operator
Different types of operators available in C are

Arithmetic Operators

It is the basic and common operation in a computer programming language and used to perform mathematical operations. It is known as binary operator since they take two operands to be evaluated. 
Operators
Meaning
Example
+
Addition
X+y
-
Subtraction
x-y
*
Multiplication
X*y
/
Division
x/y
%
Remainder
X%y (returns reminder)

Assignment Operators

The assignment operator is used to assing a value to a variable. The form is as follows
variable = expression;
When executed, expression is evaluated, and the resulting value is assigned to variable, Some assignment operators are
Operators
Example
+=
x+=y, x=x+y, x+y is equal to x=x+y;
-=
x-=y
*=
X*=y
/=
x/=y
%=
X%=y
In a C assignment statement, the right side can be any expression, and the left side must be variable name.

Unary Operators

The unary mathematical operators are so named because they take a single operand. Unary operator acts upon single operand to be increased or decreased by one. 
Operators
Symbol
Action
Example
Increment
++
Increment the operand by one
++x, x++
Decrement
--
Decrements the operand by one
--x, x--
The statements
++x;
--y;
are the equivalent of these statements:
x=x+1;
y=y-1;
They differ in terms of when the increment or decrements is performed;
  1. When used in prefix mode, the increment and decrements operators modify their operand before it's used.
  2. When used in post-fix mode, the increment and decrements operators modify their operand after it's used. 
an example of prefix mode
x=10;
y=x++;
after these statements are executed, x has the value 11, and y has the value 10. The value of x was assigned to y, and then x was incremented. In contrast, the following statements result in both y and x having the value 11. x is incremented, and then its value is assigned to y. an example of postfix mode;
x=10;
y= ++x;

Relational Operators

Relational operators are used to compare expression , asking questions such as "is x greater than 50?" or "is y is equal to 0?" an expression containing a relational operator evaluates to either true (1) or false (0).
Operators
Symbol
Example
Equal
==
x ==y
Greater than
> 
x>y
Less than
< 
x<y
Greater than or equal to
>=
x>=y
Less than or equal to
<=
x<=y
Not equal
!=
x!=y

Logical Operators 

Logical operators let you combine two or more relational expression into a single expression that evaluates to either true or false. 
Operators
Symbol
Example
AND
&&
Expression1 && Expression2
OR
||
Expression1 || Expression2
NOT
!
!expression1

Ternary Operator

The ternary operator is C's only ternary operator, meaning that it takes three operands, 
expression1? expression2 : expression3;
if expression1 evaluates to true (that is, nonzero), the entire expression evaluates to the value of expression2. If expression evaluates to false (tat is zero), the entire expression evaluates as the value of expression3. For example, the following statement assigns the value 1 to x if y is true and assigns 100 to x if y is false.
x=y?1:100;
Likewise, to make z equal to the larger of x and y, you could write
z=(x>y)?x:y;
perhaps you have noticed that the conditional operator functions somewhat like an if statement. The preceding statement could also be written like this.
if(x>y)
z=x;
else 
z=y;
The ternary operator can't be used in all situations in place of an if...else construction, but the conditional operators is more concise. The ternary operator can also be used in places you can't use an if statement,such as inside a single printf() statement.
printf("The larger value is %d",((x>y)?x:y));

Comma Operator

It permits two different expression to appear in situation where only one expression would ordinarily be used. 
{
         int a,b,c;
         c=(a=10, b=20, a+b);
         printf("C=%d",c);
}

Sizeof Operator

This is a unary operator, which finds out the number of bytes that any object occupies in computers' memory. It has the same precedence and associativity as the other unary operators. The syntax for sizeof operator is, 
sizeof(object);
where object refers to variable or data type. for example, the size of data types can be displayed by the following statements.
printf("size of char = %u bytes", sizeof(char));
printf("size of int = %u bytes",sizeof(int));
printf("size of float = %u bytes",sizeof(float));
printf("size of double = %u bytes",sizeof(double));
Similarly, the type of a constant or a variable can be displayed as,
printf("size of char = %u bytes",sizeof(a));
printf("size of int = %u bytes",sizeof(5));

Conditional Operator

The conditional operator in C is also known as ternary operator ?:.  It is called ternary operator because it takes three arguments. The conditional operator has the following construct.
expr1 ? expr2 : expr3
The expression expr1 is normally a true-false expression. The first expression expr1 is evaluated first if it is nonzero (true) then expr2 is evaluated which is the value of the whole conditional expression. If expr1 is zero (false) then expr3 is evaluated which is the value of the whole conditional expression. Thus, a conditional expression can be used to do the work of the if-else statement. For example the following assignment statement.
small = x<y ? x : y;
small is assigned the value of x if x is smaller than y otherwise y is assigned to small. This expression is equivalent to the following if-else statement.
if(x<y)
          small = x;
else
          small = y;

C Code

#include<stdio.h>
#include<conio.h>
void main()
{
    int a = 5, b = 8;
    int c;
    c = (a < b)? a : b;
    printf(“%d”, c);
}

You May Also Like...