如何检查/运算符在C中是否没有余数?

use*_*316 4 c operator-keyword

我要检查if/经营者有没有剩余或不:

int x = 0;    
if (x = 16 / 4), if there is no remainder: 
   then  x = x - 1;
if (x = 16 / 5), if remainder is not zero:
   then  x = x + 1;
Run Code Online (Sandbox Code Playgroud)

如何检查是否有剩余C?以及
如何实施它?

Gri*_*han 7

首先,你需要%余数运算符:

if (x = 16 % 4){
     printf("remainder in X");
}
Run Code Online (Sandbox Code Playgroud)

注意:它不适用于float/double,在这种情况下你需要使用fmod (double numer, double denom);.

第二,按照您的意愿实施:

  1. if (x = 16 / 4),如果没有剩余,x = x - 1;
  2. If (x = 16 / 5)然后x = x + 1;

使用,逗号运算符,您可以按照以下步骤执行此操作(读取注释):

int main(){
  int x = 0,   // Quotient.
      n = 16,  // Numerator
      d = 4;   // Denominator
               // Remainder is not saved
  if(x = n / d, n % d) // == x = n / d; if(n % d)
    printf("Remainder not zero, x + 1 = %d", (x + 1));
  else
    printf("Remainder is zero,  x - 1 = %d", (x - 1));
  return 1;
} 
Run Code Online (Sandbox Code Playgroud)

检查工作代码@codepade:第一,第二,第三.
请注意if-condition我正在使用逗号运算符:,,通过示例了解,运算符read:逗号运算符.