Quick links
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.
- Arithmetic operators
- Assignment operators
- Relational operators
- Logical operators
- Bitwise operators
- Increment/Decrement operator
- Conditional (Ternary) operator
- Other operators
Various operators in C programming
Let us suppose a = 10
, b = 5
.
Operator | Description | Example |
---|---|---|
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. |
|
| | Bitwise OR returns 1 if any of the two binary bits are 1 otherwise 0. |
|
^ | Bitwise XOR returns 1 if both the binary bits are different else returns 0. |
|
~ | 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. |
|
<< | 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. |
|
>> | 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. |
|
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.
Operator | Name | Description |
---|---|---|
. | Member access operator | Used to access the members of structures and unions |
-> | Member access operator | Used to access the members of structures and unions |
* | Dereferencing operator | Used to dereference the value of a pointer variable |
& | Address of operator | Used to get the actual memory address of a variable |
sizeof() | Size of operator | Used 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.