Thread: How to get integer from string???

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    18

    How to get integer from string???

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int main(void)
    {
       unsigned number1, number2;
       char aString = "5.55";
    
       /* ?????????????????????????????? */
    
       printf("%d.%d\n", number1, number2);
    
       return EXIT_SUCCESS;
    }

    How can i assign 5 to number1, 55 to number2.
    So that the printf will print out 5.55?

    Please help. Thanks!

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well assuming that you really need to extract the decimal part and the integer part you could use strtok() to break up the string based on the location of the '.', then use strtol() to transform the parts in numbers.

    Alternatively, you could just convert it directly to a floating point number using strtof().

    In any case, you need to Google these functions and read the examples.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    13
    There are a couple of ways to do that
    .
    Code:
    #include<stdio.h> int main(void) { char aString[]="15.55"; char *res,*tok="."; long int ar[5]; unsigned long int number1,number2; int i=0; res=strtok(aString,tok); while(res!=NULL) { ar[i++]=atol(res); res=strtok(NULL,tok); } number1=ar[0]; number2=ar[1]; printf("%ld %ld",number1,number2); return 0; }

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Please don't provide entire solutions to people asking for help, it is completely counterproductive to their learning.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    13
    sry...i din know that

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    18
    anyway, thanks for helping!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to take integer from a string
    By suryak in forum C Programming
    Replies: 21
    Last Post: 11-08-2011, 06:45 AM
  2. Convert Integer to String and String to Integer
    By hqt in forum C++ Programming
    Replies: 26
    Last Post: 09-15-2011, 11:39 AM
  3. string to integer
    By slash_axl in forum C Programming
    Replies: 2
    Last Post: 10-09-2010, 06:15 AM
  4. Replies: 4
    Last Post: 12-04-2009, 10:22 AM
  5. Integer to String
    By SoFarAway in forum C Programming
    Replies: 7
    Last Post: 03-26-2005, 11:17 PM