In C language, 1/2 evaluates to 0 instead of 0.5 because integer division discards the decimal part. Integer division discards the decimal part and only retains the integer part. Floating point division returns a decimal result.
What is 1/2 equal to in C language?
In C language, 1/2 evaluates to 0, not 0.5. This is because integer division returns an integer result after discarding the fractional part.
Integer division
<code class="c">int a = 5; int b = 2; int result = a / b; // result = 2</code>
Floating point division
<code class="c">float a = 5.0; float b = 2.0; float result = a / b; // result = 2.5</code>
Therefore, in C language, 1/2 will return 0 because it is an integer division operation and discards the decimal part.
The above is the detailed content of What is 1/2 equal to in C language?. For more information, please follow other related articles on the PHP Chinese website!