C float decimal not being memorized [duplicate] - c

This question already has answers here:
What is the behavior of integer division?
(6 answers)
Closed 8 years ago.
I have a piece of code that looks like this:
float nb = 100 / 42;
printf("%.2f", nb);
which I expect to print out 2.38, but instead it prints out 2.00.
The 42 is just an example. In the original code it's a variable.

You need to specify the numbers as floating point themselves. Try this:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
float nb = 100.0/42.0;
printf("%.2f\n", nb);
return 0;
}

Related

Why am I getting 0 as the output? Shouldn't it be 40? [duplicate]

This question already has answers here:
Why printf() isn't outputting this integer as float number? [duplicate]
(8 answers)
Closed 4 years ago.
Why does the following code output 0 instead of 40?
#include <stdio.h>
int main()
{
int volume;
int length = 5;
int width = 8;
volume = length * width;
printf("%f", volume);
return 0;
}
The variable "volume" is Integer , you need in the printf function to change from %f that is for float variable, to %d that is for print Integer variable.
Declaration of a variable is int volume; and you are printing it by format specifier %f which belongs to float type of variable. Type casting requires (float)volume. In C programming it happens a lot because compiler dependency comes in picture.

What happens when we convert string literal to integer in C? [duplicate]

This question already has answers here:
Multiple characters in a character constant
(3 answers)
Closed 6 years ago.
Consider a C program:
#include <stdio.h>
int main (void)
{
int x = 'a';
printf("%d", x);
}
Here the output is 97 as per the ASCII value table.
But in the example below:
#include <stdio.h>
int main(void)
{
int x ='aa';
printf("%d", x);
}
The output is 24929.
Can anyone please explain how the literal has been converted to this integer value?
int x ='aa';
This is valid but value of x is implementation defined. And btw, this is not a string literal. String literal would be this "aa".
You assigned a value to the int using octets : 'a' is 0x61.
So writing int x = 'aa' is like writing int x = 0x6161.
Edit: but do not write that. Just write int x = 0x6161 or int x = 24929.

Reason why this C program stopped working [duplicate]

This question already has answers here:
Declaration of variable causes segmentation fault
(2 answers)
Closed 6 years ago.
I wonder why this program stopped working when I increase the value of array a[]
If it has, plaese tell me how to increase this value without crashing
Thanks
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int i, j, save;
char a[2082001];
memset(a,'1',2082000);
for (i=2;i<=2082000;i++)
{
if (a[i]=='1')
{
save=i;
for (j=i*2;j<=2082000;j+=i)
a[j]='0';
}
}
printf("save = %d",save);
return 0;
}
Basic signed integer type. Capable of containing at least the [−32767, +32767] range; thus, it is at least 16 bits in size,
that takes j out of bounds.

being "ia" an integer array, printf("3[ia]:%d\n",3[ia]); WHY it works? [duplicate]

This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 7 years ago.
#include <stdio.h>
int main(int argc, char *argv[]){
int *ia[5]={0,1,2,3,4};
iap=ia;
printf("ia[3]:%d\n",3[ia]);
return 0;
}
Why is that line working in C?
The amazing world of C pointer arithmetic:
ia[3] is computed as *(ia + 3), which is the same as *(3 + ia) or 3[ia].
You can also write 1[ia+2], or 3[ia-+!ia], or even 2[1+ia, ia+1]...
None of these should appear in regular code unless you are trying to obfuscate and confuse the casual reader/maintainer/code reviewer.

printing float value 0.9 in C [duplicate]

This question already has answers here:
Why dividing two integers doesn't get a float? [duplicate]
(7 answers)
Closed 5 years ago.
Please do flog me for this really basic question, but I still can not get why this happen.
I read that for printing several value behind coma, I should use %.f in C.
So I have this problem, counting 90/100. I expect to print 0.9
#include <stdio.h>
#include <math.h>
int main()
{
double c=0;
c = 90/100;
printf("%.1f\n", c);
}
And it shows me 0.0 ..(err..) . tried to change it into (printf("%f\n",c)) return me with 0.00000.. (err..)
Can anyone help me with this? (sorry, really new in programming..)
Thank you
The problem is that you are doing integer division. 90/100 = 0 in integer terms.
If you want to get 0.9, do : 90.0/100.0
The problem is at
c = 90/100;
Although, it will be assigned to a double data type, but the computation itself is all integer and that's why the value is 0.0.
Try,
c = 90.0/100;
it is integer division, do:
c = 90.0/100;
c = (float)90/100;
you need to make atleast one operant a double to evaluate the whole equation as double
You are dividing two integers, thus the result is an integer too. Try the following
#include <stdio.h>
#include <math.h>
int main()
{
double c=0;
c = 90.0/100;
printf("%.1f\n", c);
}

Resources