Operators and separators in C programming

Every C program is developed using five fundamental units keywords, identifiers, operators, separators and literals. In previous post, we already learned about keywords and identifiers. Here in this post we will focus on operators and separators.

Operators in C

Operator is a symbol given to an operation that operates on some value. It tells the computer to perform some mathematical or logical manipulations. Such as + is an arithmetic operator used to add two integers or real types.

C language provides a rich set of operators. Operators are classified into following categories based on their usage.

  1. Arithmetic operators
  2. Assignment operators
  3. Relational operators
  4. Logical operators
  5. Bitwise operators
  6. Increment/Decrement operator
  7. Conditional (Ternary) operator
  8. Other operators

Various operators in C programming

Let us suppose a = 10, b = 5.

OperatorDescriptionExample
Arithmetic operator
Arithmetic operator are used to perform basic arithmetic operations.
+Add two integer or real type.a + b gives 15
*Multiply two integer or real types.a * b gives 50
/Divide two integer or real types.a / b gives 2
%Modulus operator divide first operand from second and returns remainder.a % b gives 0 (As 10/5 will have 0 remainder)
Assignment operator
Assignment operator is used to assign value to a variable. The value is assigned from right to left.
=Assign value from right operand to left operand.a = 10 will assign 10 in a
Relational operators
Relational operators are used to check relation between any two operands.
>If value of left operand is greater than right, returns true else returns false(a > b) returns true
<If value of right operand is greater than left, returns true else returns false(a < b) returns false
==If both operands are equal returns true else false(a == b) returns false
!=If both operands are not equal returns true else false.(a != b) returns true
>=If value of left operand is greater or equal to right operand, returns true else false(a >= b) returns true
<=If value of right operand is greater or equal to left operand, returns true else false(a <= b) will return false
Logical operators
Logical operators are used to combine two boolean expression together and results a single boolean value according to the operand and operator used.
&&Used to combine two expressions. If both operands are true or Non-Zero, returns true else false((a>=1) && (a<=10)) returns true since (a>=1) is true and also (a<=10) is true.
||If any of the operand is true or Non-zero, returns true else false((a>1) || (a<5)) will return true. As (a>1) is true. Since first operand is true hence there is no need to check for second operand.
!Logical NOT operator is a unary operator. Returns the complement of the boolean value.!(a>1) will return false. Since (a>1) is true hence its complement is false.
Bitwise operators
Bitwise operator performs operations on bit(Binary level). Lets suppose a = 10, b = 5
a = 0000 1010 (8-bit binary representation of 10)
b = 0000 0101 (8-bit binary representation of 5)
&Bitwise AND performs anding operation on two binary bits value. If both are 1 then will result is 1 otherwise 0.
  <span class="token number">0000</span> <span class="token number">1010</span>
<span class="token operator">&</span> <span class="token number">0000</span> <span class="token number">0101</span>
____________
  <span class="token number">0000</span> <span class="token number">0000</span>
|Bitwise OR returns 1 if any of the two binary bits are 1 otherwise 0.
  <span class="token number">0000</span> <span class="token number">1010</span>
<span class="token operator">|</span> <span class="token number">0000</span> <span class="token number">0101</span>
___________
  <span class="token number">0000</span> <span class="token number">1111</span>
^Bitwise XOR returns 1 if both the binary bits are different else returns 0.
  <span class="token number">0000</span> <span class="token number">1010</span>
<span class="token operator">^</span> <span class="token number">0000</span> <span class="token number">0101</span>
___________
  <span class="token number">0000</span> <span class="token number">1111</span>
~Bitwise COMPLEMENT is a unary operator.It returns the complement of the binary value i.e. if the binary bit is 0 returns 1 else returns 0.
<span class="token operator">~</span> <span class="token number">0000</span> <span class="token number">1010</span>
___________
  <span class="token number">1111</span> <span class="token number">0101</span>
<<Bitwise LEFT SHIFT operator is unary operator. It shift the binary bits to the left. It inserts a 0 bit value to the extreme right of the binary value.
  <span class="token number">0000</span> <span class="token number">1010</span> <span class="token operator"><<</span> <span class="token number">2</span> 
<span class="token operator">=</span> <span class="token number">0010</span> <span class="token number">1000</span>
>>Bitwise RIGHT SHIFT operator is unary operator. It shifts the binary bits to the right. It inserts a 0 bit value to the extreme left of the binary value.
  <span class="token number">0000</span> <span class="token number">1010</span> <span class="token operator"><<</span> <span class="token number">2</span>
<span class="token operator">=</span> <span class="token number">0000</span> <span class="token number">0010</span>
Increment/Decrement operator
Increment/Decrement operator is a unary operator used to increase an integer value by 1 or decrease it by 1. Increment/decrement operator are of two types Postfix and Prefix.
++Increment operator will add 1 to an integer value.a++ will result to 11
++a will result to 11
--Decrement operator will subtract 1 from an integer value.a-- will result to 9
--a will result to 9
Conditional/Ternary operator
Ternary operator as a conditional operator and is similar to simple if-else. It takes three operand.
?:It is used as conditional operator. Syntax of using ternary operator:
(condition) ? (true part) : (false part)
b = (a>1) ? a : b;
will store the value 10 in b as (a>1) is true hence true part will execute, assigning the value of a in b.

Other operators

In addition to above mentioned operator, C supports many more operators.

OperatorNameDescription
.Member access operatorUsed to access the members of structures and unions
->Member access operatorUsed to access the members of structures and unions
*Dereferencing operatorUsed to dereference the value of a pointer variable
&Address of operatorUsed to get the actual memory address of a variable
sizeof()Size of operatorUsed to get the size of a data type

At this point discussing these operators is not possible. I will introduce these operators later in this C programming tutorial series.

Read more – Operator precedence and associativity in C

Separators in C

Separators are used to separate one programming element from other. Such as separating keyword from keyword, keyword from identifier, identifier from other identifier etc. They are similar to punctuation marks in English paragraphs.

In C programming every expression is separated using white space character/s, statements are separated from other using semicolon ;.

We can use any number of white space characters to separate two expressions. However we must use at least single white space character to separate one programming element from other. We can also use number of semicolon to separate one statement from other.

Note: We can write entire C program in two lines if proper separators used. Take an example of below two programs.

#include <stdio.h> 
int main(){int a=10;int b=20;int c=a+b;printf("Sum=%d",c);return 0;}

The above program displays sum of two numbers 10 and 20 using minimum number of separators. However, it is less readable and considered as poor programming practice. We must use proper separators (spaces and indention) to make the program readable.

Consider the same program written with proper separators and is much more readable than previous program.

#include <stdio.h>

int main()
{
	int a = 10;
	int b = 20;
	int c = a + b;

	printf("Sum=%d", c);

	return 0;
}

Hence, I always recommend you to maintain a good indention in your program.