Basic usage of "/" and "%" in C language, C Language
Introduction:
In the past, when I was learning the "%" operation, I was always confused about how it was used, later, I gradually felt the benefits of "%" in the course of question brushing. Therefore, I wrote some simple summaries based on the summary written by everyone on the Internet. If there is any error, thank you for your advice and discussion!
Summary of "/" "%"
Division operator "/". Binary operators with left-bound attributes. When all the values involved in the calculation are integer, the result is integer and the decimal point is removed. If one of the computations is real, the result is double-precision real. The remainder operator "%", a binary operator, with a left combination. The amount involved in the calculation is an integer.
The result of the remainder operation is equal to the remainder after the division of two numbers.
The modulo operator "%" has some small applications, such:
① When you want to obtain a random number through rand (), rand () % 100; generates a random number ranging from 0 to 99.
If you want to generate a number between 16 and 59, you can write: rand () % 44 + 16 (here 44 is obtained from 59-16 + 1 ).
Rand () % 44 can obtain a random number ranging from 0 to 43, plus 16 to get a random number ranging from 16 to 59;
② In addition to the first point, the "%" operation is usually used for the conversion of the N-base.
For example:
If it is binary conversion, you can use/and % together to get the converted binary number (short division)
When the original number is removed to the remaining 0, its last modulus is the highest bitwise number.
30 (10)-> 11110 (2)/and % can usually be used to obtain the lowest digit or a required digit.
For example:
"/" Can usually remove the ending number, while "%" is usually used to obtain the ending number.
The code is demonstrated as follows:
# Include
Int main () {
int x;
double y;
int result;
int dx;
double dy;
double dresult;
/* round the integer through + 0.5, otherwise, the number of digits in the C language is directly removed from the integer.
For example, if it is 5/2. 0 = 2.5. If % d is output directly, 2 is returned. If + 0.5/10*10, 3 is returned. The rounding operation */
x = 28 is performed;
y = 10.0;
printf ("x = % d, y = % lf \ n", x, y);
result = (double (x/y) + 0.5) /10*10;
printf ("result = % d \ n", result);
/* for real numbers passed .? The output operations of can realize rounding them. For example, in the example, 30/7 is originally equal to 4. 258 ....
however, the output time limit is in the format. 2lf, so the result 4.26 is displayed, Which is rounded to */
dx = 30;
dy = 7;
printf ("dx = % d, dy = %. 1lf \ n ", dx, dy);
dresult = dx/dy;
printf (" dresult = %. 2lf \ n ", dresult);
// hexadecimal conversion
int tx = 30;
int mod = 0,
temp = 0;
int binary [10] = {0 };
printf ("% d (10)->", tx);
while (tx) {
binary [temp ++] = tx % 2;
tx/= 2;
}
temp --;
for (int I = temp; I> = 0; I --)
printf ("% d", binary [I]);
printf ("(2 )");
return 0 ;
}
The output result is as follows:
X = 28, y = 10.000000.
Result = 3
Dx = 30, dy = 7.0
Dresult = 4.29
30 (10)-> 11110 (2)