converting int to string

M

Michael

Hi,

I had a look in FAQ but couldn't find any (what seemed to be) similar
questions to this. Doubtless there is one, but I can't see it. Anyway......

If I have an int, say 1234, and I need to conert it to an equivalent string
"1234" , is there a standard function that does this?

Thanks for your help

Michael
 
S

seni.yin

Michael said:
Hi,

I had a look in FAQ but couldn't find any (what seemed to be) similar
questions to this. Doubtless there is one, but I can't see it. Anyway......

If I have an int, say 1234, and I need to conert it to an equivalent string
"1234" , is there a standard function that does this?

Thanks for your help

Michael

fuction "sprintf"
 
M

Michael Mair

Michael said:
I had a look in FAQ but couldn't find any (what seemed to be) similar
questions to this. Doubtless there is one, but I can't see it. Anyway......

If I have an int, say 1234, and I need to conert it to an equivalent string
"1234" , is there a standard function that does this?

ret = snprintf(buffer, n, "%d", 1234);
if (ret < 0 || ret >= n) {
/* something went wrong || the buffer size (n) was not
** large enough */
}
or, if your library does not have the C99 function snprintf(),
use its unsafe cousin, sprintf(), with a "large enough" buffer.

For arbitrary base, have a look at itoa() which is not a part of
the standard library. Recently, I posted an extended safer version
of itoa() to show how one could write his or her own, see
<[email protected]>.


Cheers
Michael
 
J

Jack Klein

Hi,

I had a look in FAQ but couldn't find any (what seemed to be) similar
questions to this. Doubtless there is one, but I can't see it. Anyway......

How did you miss:

"13.1 How can I convert numbers to strings (the opposite of atoi)? Is
there an itoa function?"
If I have an int, say 1234, and I need to conert it to an equivalent string
"1234" , is there a standard function that does this?

Thanks for your help

The question, and its answer, are at http://c-faq.com/lib/itoa.html
 
R

richard_l

Hi,

As already indicated, you can use sprintf to convert a string eg.

int number=1234;
char buffer[5];

sprintf (buffer, "%d", number);

sprintf also null terminates the string!

However, I also think there is a function called itoa() which can also
be used. However, I prefer to use sprintf.

Regards,

Richard
 
R

Richard Bos

(e-mail address removed) wrote:

[ Don't top-post, please. And do snip. Corrected. ]
As already indicated, you can use sprintf to convert a string eg.
sprintf also null terminates the string!

It had better, or it isn't a string.
However, I also think there is a function called itoa() which can also
be used.

And if you'd read that FAQ Jack linked to, you'd have known that there
isn't, in ISO C.

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,529
Latest member
JaclynShum

Latest Threads

Top