Home > Back-end >  Is there any c integer constant x such that x% 1 not equal to 0
Is there any c integer constant x such that x% 1 not equal to 0

Time:03-20

Is there any c integer constant that is relatively prime with 1? There isnt one in the set of mathematical integers, but I was wondering if there was such a number for the c language.

NaN or INT_MAX or some other #define ed constants maybe?

CodePudding user response:

No. You did not use the tag "language-lawyer", so I won't refer to the standard but will instead just look at what typical compilers do. Using Compiler Explorer, I see that GCC 11.2 compiles the following C function to assembly that always just returns 0:

int foo(int x) {
    return x % 1;
}

Also Clang 13.0.1 does that too.

So the compiler developers have thought about this and determined that there are no integers that satisfy the condition you are looking for.

CodePudding user response:

% is defined in expr.mul#4:

The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined. For integral operands the / operator yields the algebraic quotient with any fractional part discarded; if the quotient a/b is representable in the type of the result, (a/b)*b a%b is equal to a; otherwise, the behavior of both a/b and a%b is undefined.

Your question can be phrased as: Are there integers a and k!=0 such that (a/1)*1 k == a. However, only 0 can be added to (a/1)*1 == a to get a. In different words, there is no integer a such that (a/1)*1 is not a.

No, a%1 == 0 for any integer a.

  •  Tags:  
  • c
  • Related