Sign in to follow this  
3Dgonewild

**C** integer to string function ?

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this