conversions

Discussion in 'C' started by transfernly, Dec 22, 2009.

  1. transfernly

    transfernly New Member

    Joined:
    Dec 15, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  2. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    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 a moderator: Dec 22, 2009
  3. transfernly

    transfernly New Member

    Joined:
    Dec 15, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    thank you its working......can you explain me about it in detail...i tried google...but couldn't find it:nonod:


    thank you
     
  4. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    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
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Please use CodeBlocks when using Code in posts
     
  6. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    sorry! next time i wnt do this mistake...
     
  7. transfernly

    transfernly New Member

    Joined:
    Dec 15, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    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
     
  8. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    sorry..not getting u!!.plzz cn u explain me with examples what r u trying to say??
     
  9. transfernly

    transfernly New Member

    Joined:
    Dec 15, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0
    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......
     
  10. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    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();
    }
    
     
  11. transfernly

    transfernly New Member

    Joined:
    Dec 15, 2009
    Messages:
    19
    Likes Received:
    0
    Trophy Points:
    0




    check out the out put is
    9960787968
     
  12. techgeek.in

    techgeek.in New Member

    Joined:
    Dec 20, 2009
    Messages:
    572
    Likes Received:
    19
    Trophy Points:
    0
    Occupation:
    EOC (exploitation of computers)..i m a Terminator.
    Location:
    Not an alien!! for sure
    Home Page:
    http://www.techgeek.in
    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
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice