**C** integer to string function ?

Started by
11 comments, last by Little Coding Fox 17 years ago
How can i convert an integer to string in c? I know how to do it with C++(streams)..im just wondering how can be done in C.
Advertisement
Well, C doesn't have "strings" like C++ does (as far as being a data type), but you can use sprintf to convert integers (and other data types) to send to char* buffer.
itoa : integer -> char
ftoa : float -> char
atof : char -> float
atoi : char -> integer
Moved to For Beginners. 3Dgonewild, please make an effort to post in the appropriate forum.
Quote:Original post by nuno_silva_pt
itoa : integer -> char
ftoa : float -> char
atof : char -> float
atoi : char -> integer


Those are of course not standard C functions (some other poster also said this, but that reply seems to have been deleted). AFAIK the best option in C is sprintf like NickGravelyn says, but it's nowhere near optimal (both in performance and usage). The itoa function was actually introduced by the C language's designers in the C programming language book so there is definitely a need for it and if I had to program in C I'd implement it myself.
Quote:Original post by CTar
Quote:Original post by nuno_silva_pt
itoa : integer -> char
ftoa : float -> char
atof : char -> float
atoi : char -> integer


Those are of course not standard C functions (some other poster also said this, but that reply seems to have been deleted).


That was me. After posting, I was going to go back and edit pointing out the sprintf() alternative and include the code for an implementation of itoa() which I wrote but found that sprintf() had already been covered and so it seemed redundant - so I deleted my post.
[TheUnbeliever]
Sorry for posting in wrong forum (..).

Anyway , i got my answer , but i would like to ask one more question.

In C , how can i combine 2 char variables?

this wont work:
char a[]="....";
char b[]="??";
char c=a+b;
Use strcat or strncat:
char buffer[100] = "I like ";char a[] = "pie";strcat(buffer, a);    // buffer now holds "I like pie\0"// Or the safe way, to avoid buffer overflows:strncat(buffer, a, 100 - strlen(buffer) - 1);
(I think this is the correct usage; someone please correct me if I'm wrong; I don't use this stuff very often. [grin])

Note that the first argument (the destination buffer) must be large enough to hold the result after concatenation. It's safest to use strncat here, since it can be prevented from overflowing the destination buffer.


EDIT: also, I just noticed this:
char c=a+b;

Aside from the fact that there's no string concatenation operator, this would not work because the result you want from string concatenation should be a C-style string (char*), not a char.
good reference for you:
http://www.cplusplus.com/reference/clibrary/cstring/

Be very very very warned however. by using c-strings you must be extremely careful about memory usage. your buffers must always be big enough, your strings must always end with a '\0' (null) character.

It's probably a really good idea to fully understand pointers and memory allocation before you use c-strings.

-me
Nice stuff...thanks!

This topic is closed to new replies.

Advertisement