Memset issue with floating point array in C [duplicate] - c

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.

Related

wrong decimal values in c? [duplicate]

This question already has answers here:
Why are floating point numbers inaccurate?
(5 answers)
Is floating point math broken?
(31 answers)
Closed 4 months ago.
i want to extract the decimal part of a float variable by substracting the whole part, the thing is i get a wrong value
#include<stdio.h>
int main(){
int k ;
float a=12.36,i;
k = (int)a;
i = a - k ;
i*=10;
printf("%f",i);
return 0;
}
well, the output is 3.599997 not 3.6 , is there a way to solve this ?
edit : i know it's because of the binary conversion, i m asking if there is a solution to get the right result, not the cause of it. ty for the replies anw.
edit 2 : sadly it's not a matter of display, i want the stored value to be 3.6 ( in this case) because i need it in other calculations.

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 wrong with this expression on the code? [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 2 years ago.
I have written this piece of code in my computer and the result is 7 instead of 8 (the correct result ... I think).
I don't know why... Can somebody help me?
#include <stdio.h>
int main() {
int num;
num = (68/10.0 - 68/10)*10;
printf("the result %d", num);
return 0;
}
double typically represents exactly about 264 different numbers. 68/10.0 is not one of them,
As a binary64, 68/10.0 is about
6.7999999999999998223643161..., the closest value to 6.8 that is a multiple of a dyadic rational. # AntonH
68/10 is an integer division with a quotient of 6.
(68/10.0 - 68/10)*10 is thus about 7.9999999999999982236431606...
Assigning that to an int is 7 not 8 as the fraction is discarded even though it is very close to 8.
When converting a floating point value consider round to the the closest, rather than truncating.
num = lround((68/10.0 - 68/10)*10);

Pointer arithmetic output issues [duplicate]

This question already has answers here:
C/C++: Pointer Arithmetic
(7 answers)
Closed 4 years ago.
Here are we use typecasting from pointer to integer, but output of arithmetic operation are different from expected answer, Why?
Source Code :
int main(){
int *p,*q;
p = 1000;
q = 2000;
printf("%d",q-p);
return 0;
}
Output: 250
The same reason due to which p++ will give you 1004. Since sizeof(int) on your machine is 4, hence each operation is shown wrt sizeof(int), even the difference i.e. 1000/4.

Value difference between two pointers is not making sense [duplicate]

This question already has answers here:
Pointer Arithmetic In C
(2 answers)
Closed 8 years ago.
I'm not getting the output. Why it is happening?
#include <stdio.h>
int main(void){
int a[3][3];
int *p, *q;
p=a[0];
q=a[1];
printf("%d\n",sizeof(int));
printf("%d\n",q-p);
printf("%d %d\n",q,p);
return 0;
}
Output
4
3
2686728 2686716
I thought (q-p) should be 12! Is my math degrading?!
I thought (q-p) should be 12
No. (q-p)==3 shall hold true since they have type int*. Meanwhile it's true that q == p + 3.
Also this is true: (char*)q - (char*)p == 12
You are getting output value as 3, because both pointers are pointing to integer data type and the difference between them is three objects.

Resources