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
Code: # include<stdio.h> # include<conio.h> # include<stdlib.h> void main() { char str[]="9960787897z3yu87"; clrscr(); printf("int: %.0f\n",atof(str)); getch(); } 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.
thank you its working......can you explain me about it in detail...i tried google...but couldn't find it:nonod: thank you
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
Code: # include<stdio.h> # include<conio.h> # include<stdlib.h> void main() { char str[]="9960787897z3yu87"; clrscr(); printf("int: %.0f\n",atof(str)); getch(); } 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
Code: str[]="987649574954bvkfvbf"; printf("%.0f",atof(str)); this was ur code....now i tried storing Code: int x=atof(str); long x=atof(str); float x=atof(str); when i try to print these values it gives me a garbage value......
why r u getting garbage value?? I did this just check it out... Code: # include<stdio.h> # include<conio.h> # include<stdlib.h> void main() { char str[]="9960787897z3yu87"; float x; clrscr(); x=atof(str); printf("int: %.0f\n",x); getch(); }
Code: # include<stdio.h> # include<conio.h> # include<stdlib.h> void main() { char str[]="9960787897z3yu87"; double x; clrscr(); x=(double)atof(str); printf("int: %.0lf\n",x); getch(); } sorry..now i have modified it and itz working output is:-9960787897