Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   conversions (http://www.go4expert.com/forums/conversions-t20467/)

transfernly 22Dec2009 16:26

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

techgeek.in 22Dec2009 17:36

Re: conversions
 
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.

transfernly 22Dec2009 18:15

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

techgeek.in 22Dec2009 18:54

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

shabbir 22Dec2009 19:12

Re: conversions
 
Please use CodeBlocks when using Code in posts

techgeek.in 22Dec2009 19:31

Re: conversions
 
sorry! next time i wnt do this mistake...

transfernly 23Dec2009 10:20

Re: conversions
 
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

techgeek.in 23Dec2009 12:28

Re: conversions
 
sorry..not getting u!!.plzz cn u explain me with examples what r u trying to say??

transfernly 23Dec2009 13:01

Re: conversions
 
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......

techgeek.in 23Dec2009 13:45

Re: conversions
 
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();
}



All times are GMT +5.5. The time now is 15:03.