convert from an integer to a string

i want to convert from an integer to a string..in unix...i am writing a C program with embedded SQL...
I remeber using itoa...but for some reason it doesnt work......i cant find it in the manual.....
Maybe that is the wrong command.....
but i have checked Dev Studio.....and it doest exist in the help...all there is is _itoa...but that is to difficult a call to use...
Any help with me grateful.....

thanks...a struggling student...:slight_smile:

Mojo

sprintf is the do-all command for conversion to strings.
It's just like printf, only the results are stored in
a string.

char buffer[200];
int i = 35, j;

/* Format and print integer: */

j = sprintf( buffer + j, "\tInteger: %d\n", i );

printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );

Can you explain why you call sprintf like this? I would just do:
j = sprintf(buffer, "\tInteger: %d\n",i);

You have not initialized j, so what is the purpose of "buffer+j"?

I was just copying example code, deleting stuff that
wasn't necessary -- EXCEPT for the initialization of j,
which could easily be catastophic.

The code was from VC++:

/* SPRINTF.C: This program uses sprintf to format various

  • data and place them in the string named buffer.
    */

#include <stdio.h>

void main( void )
{
char buffer[200], s[] = "computer", c = 'l';
int i = 35, j;
float fp = 1.7320534f;

/* Format and print various data: */
j = sprintf( buffer, "\tString: %s\n", s );
j += sprintf( buffer + j, "\tCharacter: %c\n", c );
j += sprintf( buffer + j, "\tInteger: %d\n", i );
j += sprintf( buffer + j, "\tReal: %f\n", fp );

printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );
}

Ok, makes more sense now. Of course all this could be simplified to:

j = sprintf(buffer,"\tString: %s\n\tCharacter %c\n\tInteger: %d\n\tReal: %f\n",s, c, i, fp);
printf( "Output:\n%s\ncharacter count = %d\n", buffer, j );

thanks ppl.....
I had noticed that this morning about j....when i got a complier error..but soon...got it....

But the promblem is that..that only alters the way it goes to standard out....i need to take it back into a variable and the strcat this to another varaible..and then use embbedded SQL statements to get it back into the Ingres database.....doh!!!!

any other ways.....

Why not itoa........?????????

please...

:((

Mojo

int j=42;
char buffer[20];

sprintf(buffer, "%d", j);
strcat(buf,buffer);

etc.