![]() |
conversions
hi everyone...
this is my code CODE #include <stdio.h> #include <stdlib.h> int main() { char str[] = "9960z3yu87"; printf("str: %d\n", atoi(str)); } here the output is 9960 but my problem is when i give an input like this char str[] = "9960787897z3yu87" the out put is some garbage value.... but i need my output to be as it is in " " that is "989898" output: 989898 "98765746474444" output: 98765746474444 in this way...i think you understood wat i mean.... help me out...thank you all |
Re: conversions
Code:
# include<stdio.h>The above code works perfectly. Actually, u obviously get the garbage value because the conversion value crosses the highest bound of integer. If u cnt understand the code then ask me. |
Re: conversions
thank you its working......can you explain me about it in detail...i tried google...but couldn't find it:nonod:
thank you |
Re: conversions
yah of course...look in c "int" takes 2 bytes i.e 16 bits..nd the max positive it can hold is from 0 to 65535. But float takes 4 bytes i.e 32 bits. The range of numbers increases. Thatz why i used "atof" instead of "atoi" and the format specifier used is "%f" instead of "%d". Now u know that the floating point numbers holds decimal point, then how r u getting the pure integer number in my above code? the reason is that in the format specifier "%f" u can specify the precision i.e number of places after decimal. e.g:- if u write %.3f then u get something like this 23.000(say) if u write %.2f then 23.00 if %.1f then 23.0 nd if %.0f then 23. I hope u have understood evrything
|
Re: conversions
Please use CodeBlocks when using Code in posts
|
Re: conversions
sorry! next time i wnt do this mistake...
|
Re: conversions
Code:
# include<stdio.h>This is ok...but i want to store the "str" value.....if i store it into float again am getting a garbage value.....and even tried in long...... so whats that i need to do now..... thanq all |
Re: conversions
sorry..not getting u!!.plzz cn u explain me with examples what r u trying to say??
|
Re: conversions
Code:
str[]="987649574954bvkfvbf";Code:
int x=atof(str);when i try to print these values it gives me a garbage value...... |
Re: conversions
why r u getting garbage value?? I did this just check it out...
Code:
# include<stdio.h> |
| All times are GMT +5.5. The time now is 14:48. |