conversions

transfernly's Avatar, Join Date: Dec 2009
Go4Expert Member
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
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
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.

Last edited by shabbir; 22Dec2009 at 19:12.. Reason: Code Block
0
transfernly's Avatar, Join Date: Dec 2009
Go4Expert Member
thank you its working......can you explain me about it in detail...i tried google...but couldn't find it


thank you
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
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
0
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Please use CodeBlocks when using Code in posts
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
sorry! next time i wnt do this mistake...
0
transfernly's Avatar, Join Date: Dec 2009
Go4Expert Member
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
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
sorry..not getting u!!.plzz cn u explain me with examples what r u trying to say??
0
transfernly's Avatar, Join Date: Dec 2009
Go4Expert Member
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......
0
techgeek.in's Avatar, Join Date: Dec 2009
Skilled contributor
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();
}