Home > Backend Development > C++ > Usage of % and / in C language

Usage of % and / in C language

下次还敢
Release: 2024-05-02 17:24:55
Original
382 people have browsed it

In C language, % is used for modulo operation, returning the remainder obtained by dividing one number by another number, while / is used for division operation, returning the result of dividing two numbers, suitable for integers and Floating point number.

Usage of % and / in C language

Usage of % and / in C language

In C language, % and / symbols are used respectively Modulo and division operations.

% Modulo operation

The modulo operation (%) returns the remainder obtained by dividing one number by another number. The operand on the left side of the operator is the divisor, and the operand on the right side is the dividend. The sign of the result is the same as the dividend.

For example:

<code class="c">int a = 13 % 5; // a = 3
int b = -13 % 5; // b = -3</code>
Copy after login

If the divisor is 0, the modulo operation is undefined and will cause the program to crash.

Division operation

The division operation (/) returns the result of dividing two numbers. The operand on the left side of the operator is the numerator, and the operand on the right side is the denominator. The result is a floating point number.

For example:

<code class="c">float c = 13 / 5; // c = 2.6
float d = -13 / 5; // d = -2.6</code>
Copy after login

If the denominator is 0, the division operation is undefined and will cause the program to crash.

Notes

  • The modulo operation only applies to integers, while the division operation applies to integers and floating-point numbers.
  • The sign of the result of the modulo operation is the same as the dividend, while the sign of the result of the division operation depends on the signs of the numerator and denominator.
  • When the denominator is 0, the modulo operation and division operation are undefined.

The above is the detailed content of Usage of % and / in C language. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template