用%表示的模运算符是算术运算运算符。
模除法运算符产生整数除法的余数。
语法:如果x和y是整数,则表达式:
x % y
当x除以y时,产生余数。
返回值:
- 如果y将x完全除,则表达式的结果为0。
- 如果x不能被y整除,则结果将是[1,x-1]范围内的余数。
- 如果x为0,则被零除是编译时错误。
例如:考虑以下代码:
C++
// Program to illustrate the
// working of modulo operator
#include
using namespace std;
int main(void)
{
// To store two integer values
int x, y;
// To store the result of
// the modulo expression
int result;
x = 3;
y = 4;
result = x % y;
cout << result << endl;
result = y % x;
cout << result << endl;
x = 4;
y = 2;
result = x % y;
cout<
C
// Program to illustrate the
// working of modulo operator
#include
int main(void)
{
// To store two integer values
int x, y;
// To store the result of
// the modulo expression
int result;
x = 3;
y = 4;
result = x % y;
printf("%d", result);
result = y % x;
printf("\n%d", result);
x = 4;
y = 2;
result = x % y;
printf("\n%d", result);
return 0;
}
C++
// Program to illustrate the
// working of modulo operator
#include
using namespace std;
int main()
{
// To store two integer values
float x, y;
// To store the result of
// the modulo expression
float result;
x = 2.3;
y = 1.5;
result = x % y;
cout << result;
return 0;
}
// This code is contributed by Harshit Srivastava
C
// Program to illustrate the
// working of modulo operator
#include
int main(void)
{
// To store two integer values
float x, y;
// To store the result of
// the modulo expression
float result;
x = 2.3;
y = 1.5;
result = x % y;
printf("%f", result);
return 0;
}
C++
// Program to illustrate the
// working of the modulo operator
#include
using namespace std;
int main(void)
{
// To store two integer values
int x, y;
// To store the result of
// the modulo expression
int result;
x = -3;
y = 4;
result = x % y;
cout << result << endl;
x = 4;
y = -2;
result = x % y;
cout << result << endl;
x = -3;
y = -4;
result = x % y;
cout << result;
return 0;
}
// This code is contributed by Harshit Srivastava
C
// Program to illustrate the
// working of the modulo operator
#include
int main(void)
{
// To store two integer values
int x, y;
// To store the result of
// the modulo expression
int result;
x = -3;
y = 4;
result = x % y;
printf("%d", result);
x = 4;
y = -2;
result = x % y;
printf("\n%d", result);
x = -3;
y = -4;
result = x % y;
printf("\n%d", result);
return 0;
}
输出
3
1
0
模运算符的限制:
模运算符有很多限制。
- %运算符不能应用于浮点数,即浮点数或双精度数。如果您尝试将模运算符与浮点常量或变量一起使用,则编译器将产生错误:
C++
// Program to illustrate the
// working of modulo operator
#include
using namespace std;
int main()
{
// To store two integer values
float x, y;
// To store the result of
// the modulo expression
float result;
x = 2.3;
y = 1.5;
result = x % y;
cout << result;
return 0;
}
// This code is contributed by Harshit Srivastava
C
// Program to illustrate the
// working of modulo operator
#include
int main(void)
{
// To store two integer values
float x, y;
// To store the result of
// the modulo expression
float result;
x = 2.3;
y = 1.5;
result = x % y;
printf("%f", result);
return 0;
}
编译错误:
Compilation Error in C code :- prog.c: In function 'main':
prog.c:19:16: error:
invalid operands to binary % (have 'float' and 'float')
result = x % y;
^
模运算符的结果符号与机器有关的负操作数有关,因为该操作是下溢或上溢的结果。
C++
// Program to illustrate the
// working of the modulo operator
#include
using namespace std;
int main(void)
{
// To store two integer values
int x, y;
// To store the result of
// the modulo expression
int result;
x = -3;
y = 4;
result = x % y;
cout << result << endl;
x = 4;
y = -2;
result = x % y;
cout << result << endl;
x = -3;
y = -4;
result = x % y;
cout << result;
return 0;
}
// This code is contributed by Harshit Srivastava
C
// Program to illustrate the
// working of the modulo operator
#include
int main(void)
{
// To store two integer values
int x, y;
// To store the result of
// the modulo expression
int result;
x = -3;
y = 4;
result = x % y;
printf("%d", result);
x = 4;
y = -2;
result = x % y;
printf("\n%d", result);
x = -3;
y = -4;
result = x % y;
printf("\n%d", result);
return 0;
}
输出
-3
0
-3
注意:一些编译器可能会将表达式的结果显示为1,而另一些可能会显示-1。这取决于编译器。