Float prints Int in C [duplicate] - c

This question already has answers here:
Why does division result in zero instead of a decimal?
(5 answers)
What is the behavior of integer division?
(6 answers)
Closed 2 years ago.
I am beginner at programming and I am just writing a very simple program to return the solution of 23/5. My code is below:
float res = 23/5;
printf("%.3f", res);
The expected answer is 4.6, however, the code outputs: 4.000.

You are setting its value to an int
float res = 23.0f/5.0f;
printf("%.3f", res);

Related

Stuck with the prediction of output to the below snippet. Can someone help me out as to why the output is 40 [duplicate]

This question already has answers here:
Who defines C operator precedence and associativity?
(5 answers)
Why are these constructs using pre and post-increment undefined behavior?
(14 answers)
Why is a = (a+b) - (b=a) a bad choice for swapping two integers?
(10 answers)
Closed 11 months ago.
#include<stdio.h>
int main()
{
int y=0;
int z;
z=(--y)+(y=20);
printf("%d",z);
return 0;
}
Why is the precedence considered from right to left in spite of parentheses?

why does this program output 37? [duplicate]

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 1 year ago.
#include <stdio.h>
int main()
{
float c = 5.0;
printf ("Temperature in Fahrenheit is %.2f", (9/5)*c + 32);
return 0;
}
Change your statement to (9.0/5.0)*c + 32 as 9 and 5 are integers, their division returns integer that is 1. So write them in float variable format.

What is the outcome for the arithemic in C? [duplicate]

This question already has answers here:
How does the bitwise complement operator (~ tilde) work?
(18 answers)
C Unsigned int providing a negative value?
(3 answers)
Closed 2 years ago.
There is an interview question in C as below.
int main()
{
unsigned int a = 9;
a = ~a;
printf("%d\n", a);
}
I though it was supposed to be 6 but it is -10.
~a is assinged back to an unsigned integer then printed out.
It should not be a negative value.
Isn't it? Hoe come?

how - 5%3 is equal to - 2? [duplicate]

This question already has answers here:
Modulo operation with negative numbers
(12 answers)
Closed 7 years ago.
I'm learning C basics right now. I have a question which is confusing me little bit.
My question is how the below program's output is - 2 ?
#include<stdio.h>
int main()
{
printf("%d", -5%3);
return 0 ;
}
The % operator is gives you the remainder left after of integer division.
Then -5/3 = -1 with -2 as remainder of division as 3*(-1)=-3 and -5-(-3)=-5+3=-2.

Memset issue with floating point array in C [duplicate]

This question already has answers here:
Initializing a float array with memset
(5 answers)
Closed 9 years ago.
I'm trying this simple example to populate a floating point array with 5.6, but upon printing the values out, every value is just 0.0.
#include <string.h>
float testArr[20];
memset(testArr, (float)5.6, 3*sizeof(float));
printf("Value 1: %lf\n",testArr[0]);
printf("Value 2: %lf\n",testArr[1]);
printf("Value 3: %lf\n",testArr[2]);
printf("Value 4: %lf\n",testArr[3]);
I've also tried not casting 5.6 as a float, setting testArr[20] = {} and testArr[20] = {0}, but they also result in the same 0.0.
memset fills the memory with 1 char, not float.

Resources