C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example:

int c = a+b*5
Where, =, +,* are operators, a,b,c are the variables and  5 is the constants. There are various types of the operator in C Language.

Types of Operator
  • Arithmetic Operators
  • Relational Operators
  • Logical Operators
  • Bitwise Operators
  • Assignment Operators
  • Misc Operators
  • Increment/Decrement operators
Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations like addition, subtraction, multiplication, division, and modulus.

 
Operator Example (int a=9, b=2) Result
+ A+B 11
- A-B 7
* A*B 18
/ A/B 4.5
% A%4 1

Example:
#include <stdio.h>
int main()
{
int a=9,b=2, add,sub,mul,div,mod;
add = a+b;
sub = a-b;
mul = a*b;
div = a/b;
mod = a%b;
printf("Addition of (a+b ) is : %d\n", add);
printf("Subtraction of (a-b) is : %d\n", sub);
printf("Multiplication of (a*b) is : %d\n", mul);
printf("Division of (a / b) is : %d\n", div);
printf("Modulus of (a%b) is : %d\n", mod);
}
Output:
Addition of (a+b ) is : 11
Subtraction of (a-b) is : 7
Multiplication of (a*b) is : 18
Division of (a / b) is : 4
Modulus of (a%b) is : 1
 Relational Operators Relational operators are used to compare the value of two variables.
Operator Description
> x > y (x is greater than y)
< x < y (x is less than y)
>= x >= y (x is greater than or equal to y)
<= x <= y (x is less than or equal to y)
== x == y (x is equal to y)
!= x != y (x is not equal to y)

Let us consider an example
#include <stdio.h>
int main()
{
int a=9,b=2;
if (a == b)
{
printf("a and b are equal");
}
else
{
printf("a and b are not equal");
}
}
Output
a and b are not equal
Logical Operators
Logical operators are used to perform logical operations on the given two variables. There are 3 logical operators in C LanguageAND(&&), OR ( || ) and NOT (!).
Operator Description
&& It returns true when both conditions are true.
|| It returns true while at least one condition is true.
! It reverses the state of the operand.

Let us consider an example
#include <stdio.h>
int main()
{
int a=8, b=3, c=10;
if(a>b && b<c){
printf("$$Operator : Both Condition are true \n");
}
if(a>b || b<c){
printf("||Operator : Only one condition is true \n");
}
return 0;
}
Output
$$Operator : Both Condition are true 
||Operator : Only one condition is true
 Assignment operators
Assignment operators are used to assign the values for the variable.
Operator Description
= sum = 10; 10 is assigned to variable sum
+= sum += 10; This is same as sum = sum + 10
-= sum -= 10; It is same as sum = sum – 10
*= sum *= 10; It is same as sum = sum * 10
/= sum /= 10; It  is same as sum = sum / 10
%= sum %= 10; It  is same as sum = sum % 10
&= sum&=10; It  is same as sum = sum & 10
^= sum ^= 10; It is same as sum = sum ^ 10

Let us consider an example
#include <stdio.h>
int main(){
int a=8,b=3,c;
c=a+b;
printf("a+b value is assign to c");
return 0;
}
Output
a+b value is assign to c
Operator Precedence in C The precedence of operators are used to specify the operators which will be evaluated first and next. Example:
int  result = 5+5*10-2;
The precedence and associatively of C operators is given below:
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary + - ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift <<>> Left to right
Relational <<= >>= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= ^= |= Right to left
Comma , Left to right

 Miscellaneous operators in C
There are few other important operators including sizeof and ?: supported by C Language.
Operator Description
sizeof() It returns the size of a variable.
& It returns the address of a variable.
* It assigns pointer to a variable.
? : It is a Conditional Expression.

Bitwise operators
Bitwise operator is used to perform bit by bit operations on two variables. There are following truth table for &, | and ^.
P q p & q p | q p ^ q
0 0 0 0 0
0 1 0 1 1
1 1 1 1 0
1 0 0 1 1
 
Operator Description
&   Binary AND Operator It copies a bit to the result if it exists in both operands.
|   Binary OR Operator It copies a bit if it exists in either operand.
^   Binary XOR Operator It copies the bit if it is set in one operand but not both.
~   Binary Ones Complement Operator It is unary and has the effect of 'flipping' bits.
<<  Binary Left Shift Operator The left operands value is moved left by the number of bits specified by the right operand.
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand.

Increment / decrement operator Increment Operator: The increment operator is used to increase the value of the variable by one.

Syntax:
Increment operator: ++ var_name; or var_name++;
Example:
Increment operator: ++i; i++;
Decrement Operator:
Decrement operator is used to decrease the value of the variable by one.

Syntax:
Decrement operator: -- var_name; or var_name--;
Example:
Decrement operator: --i;  i--;
Difference between Increment & Decrement operators
There are various differences between pre/Post increment & decrement operators in C.

 
Operator Operator/Description
Pre increment operator (++i) The value of i is incremented before assigning it to the variable i.
Post increment operator (i++) The value of i is incremented after assigning it to the variable i.
Pre decrement operator (- -i) The value of i is decremented before assigning it to the variable i.
Post decrement operator (i--) The value of i is decremented after assigning it to variable i.



ADVERTISEMENT
ADVERTISEMENT