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
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>
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>
If the denominator is 0, the division operation is undefined and will cause the program to crash.
Notes
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!