GeekInterview.com
  I am new, Sign me up!
 
Home C
 

Modulus Operator

 
Category: C
Comments (0)


What is the functionality and restrictions of Modulus Operator?


When a division is performed, the remainder of the operation is given by modulus operator. The modulus operator is denoted in c by symbol %.


For instance we have two integer values x and y, the operation x % y called as x modulus y gives the result as (x- (x / y) * y).


If x=18 and y =5 the x % y gives value as (18- (18 / 5) * 5) which gives (18-15) which results in 3 in other words the remainder of the division operation.


There is a restriction in C language while using the modulus operator. it can operator only on integers and cannot operate on floats or double. If anyone tries to use the modulus operator on floats then the compiler would display error message as ‘Illegal use of Floating Point’.


Let us see this with an example:


main ()
{
float x=7,y=5;
int z;
z = x % y;
printf( %d”, z)’;
}


This above program would give error as ‘Illegal use of Floating Point’.


This is because modulus operator is made to operate on float values namely x and y which is not possible.



Read Next: Swap two variables



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape