+ 3
how is the answer of this code 1.000. Can anyone explain?
#include <stdio.h> int main() { float a = 5%2*3/2; printf("%f", a); return 0; }
16 Réponses
+ 7
Shubham Rampurkar all three operators in the calculation for y have the same mathematical precedence, so the order of operations is left-to-right. Adding parentheses to clarify, it is equivalent to this:
int y = ((5%2)*3)/2;
5%2 = 1 [5/2 has remainder 1]
(1)*3 = 3
(3)/2 = (int) 1 [integer division]
Integer division occurs when the divisor and dividend are both integer.
So, y = 1, and the printf shows it.
Note: In this case, even if you were to divide by 2.0 in order to force floating point division, the result would be converted to 1 because y is int.
+ 3
5%2 = 1
3/2 = 1
+ 3
Shubham Rampurkar yes, the integer division rule still applies. Both sides of the division are int (3 and 2). Try replacing dividend 2 with 2.0.
+ 3
Shubham Rampurkar you're welcome. In C you do have to be precise and intentional about mixing types. By design the compiler makes few assumptions about what you are trying to accomplish. It just does what the code says. That makes it flexible, but less user-friendly. You must know all the rules well so you can carefully craft your intended results.
+ 1
ok. Thanks Brian for your help. That's a bit confusing for me. but now I understand.
+ 1
for sure.
+ 1
Ankit Kumar In printf, the syntax is like this:
printf("%[format specifier of datatype]",[variable]);
so the actual code is like Rain's post above.
0
AJ 3/2 = 1.5
0
Thanks Brian. but I've done this problem like below and it still gives answer 1.000
#include <stdio.h>
int main() {
float a = 5%2*3/2;
printf("%f", a);
return 0;
}
0
I haven't used C for a while, but doesn't
printf("%f", a)
mean to print a in float format?
0
Rain yeah. That's true. But i think the problem lies in the code operation processing (a=5%2*3/2). As far as i know float data type rarely use modulo operation. It is more common in int data type
0
Bro to na variable kaha let kiya
?????
0
!-- created by : SwapniL -->
<!DOCTYPE html>
<html>
<head>
<title>Loading Animation</title>
<style>
*, *::after, *::before{
padding: 0;
margin: 0;
box-sizing: border-box;
}
body{
height: 100vh;
display: flex;
align-items: center;
justify-content: center;
}
.base{
height: 200px;
width: 200px;
display: flex;
align-items: center;
justify-content: center;
position: relative;
}
.dot{
height: 20px;
width: 20px;
background-color: black;
border-radius: 50%;
opacity : 0;
position: absolute;
left: calc(50% - var(--x));
animation: fade 1s var(--delay) linear alternate-reverse infinite;
}
@keyframes fade {
from{
opacity : 1;
}
to{
opacity: 0;
transform: translateY(20px);
}
}
.one{
--x : -50px;
--delay :
0
Ayan Hayat - hello, and welcome to Sololearn!
It seems you pasted in the wrong place. We're happy to have you join us, though the irrelevant html code clutters the conversation. Kindly delete your reply.
0
In this expression `5%2*3/2` is evaluated. Here's the step-by-step explanation:
1. `5 % 2` calculates the remainder when 5 is divided by 2, which is 1.
2. `1 * 3` is then multiplied by 3, resulting in 3.
3. Finally, `3 / 2` is divided by 2 using integer division, resulting in 1 (since the fractional part is discarded in integer division).
So, the value assigned to variable `a` is 1.0
and when you print it using `printf("%f", a);
it will display as "1.000000".
0
Ok 👌