LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Integer To a String in C (https://www.linuxquestions.org/questions/programming-9/integer-to-a-string-in-c-186404/)

Bean101 05-27-2004 03:28 AM

Integer To a String in C
 
h is an int; number in the 5000's.

char *ret;

got a for loop running

this is where h is being set. i need to add -h to the end of ret.
so ret will look something like this when the loop is done
5000-5001-5002 but a string. :scratch:

duncanbojangles 05-27-2004 03:45 AM

You're looking for itoa();
This is a good little tutorial on itoa() and some others you might be interested in: http://www.iota-six.co.uk/c/g3_atoi_...ntf_sscanf.asp
Hope this helps.

jim mcnamara 05-27-2004 04:46 AM

sprintf() will also do what you need.
Code:

void foo(char *ret){
        int h=0;
        char tmp[6]={0x00};
        *ret=0x00;
        for(h=5000;h<5004;h++){
              sprintf(tmp,"%d-",h);
              strcat(ret,tmp);
        }
}



All times are GMT -5. The time now is 05:32 PM.