We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


IntToString(char *s, int i) ???

victorlong asked
Medium Priority
Last Modified: 2008-02-01
What function for putting a number into a string?
Watch Question

itoa(int,char*,int); //last arg is 10 for base 10


Itoa() is not a standard C++ function.  sprintf() is.

You would do something like.

char Str[80];
int i = 45;


of course ther are millions of possibilities for controling the base, the leading sign, the pading characters etc with sprintf().


I want a C++ answer since a am in C++ space.

I remember function printf() is a C style function which needs a C style head file (forgot the name). Are you sure sprintf() is a C++ function? What head file it needs?

All the c functions are available in C++.  It is found in stdio.h.

I guess if you want a more C++ (not C) approach, you could use the << operator to a strstream, like

#include <strstream>
strstream S;
int i = 123;
char Str[50];

S << i;

But I don't think I would go to that trouble.

Actually that's not 100% correct.  the string returned by the strstream's str() function is not NUL terminated.  You have to get the stringbuffer and get the length from there.  I can elaborate if you really want to go that route.



1. So you agree that C approach is better than C++ in my case.
2. Why you said "Itoa() is not a standard C++ function.  sprintf() is."? It seems to me that Both of them are C functions and can be used in C++.

1.  the sprintf() (C) approach will probably slightly faster and more direct than the stringstream (C++) approach.  Realistically, you aren't likely to notice the difference.

2.  sprintf() is part of the standard C library.  All C and C++ compilers that are shipped with a copy of the standard library (wich is probably all compilers) will support sprintf().  Thus code that uses sprintf() is portable.  itoa() is not part of the standard C or C++ libraries.  Thus code that uses it may not be portable to other compilers.  If you don't plan on porting and if your compiler supports it, then it doesn't matter and you can use it.  (Or you can write your own itoa() for compilers that don't support it and use the existing itoa() on compilers that do.).


Experts, Thank you.

Lase quesion:

lucidity's answer isn't a wrong answer but nietod's comments let me know much more....How to do with the points?

That's up to you.  If it were up to me, I think I would base it on the solution I chose to use.  That is, if I choose to use itoa(), I would give lucidity the points, if not, I would give them to the other expert.    But the choice is yours.  I offered the advice freely knowing well that most likely I would not get the points.



Would you say something?


>All the c functions are available in C++.  It is found in stdio.h.

If you want to pick nits, you're supposed to #include <cstdio>, not <stdio.h>, if you want the standard C stdio library in a C++ project. The only time you're supposed to use the old-style headers is in a mixed-language project. Of course nearly every compiler comes with a cstdio that just #includes stdio.h anyway, so it doesn't really make any difference...

And just to clarify one more tiny irrelevancy, the fact that sprintf is in <cstdio> means it's part of the standard C++ library as well as the standard C library...

And why 80 characters to represent an integer? In case someone's using a 256-bit platform?

I believe that according to the standard (now that there is one).  stdio.h is the typical C standard stdio.h include file.  The cstdio include file is a short include file that includes the stdio.h include file, but includes it inside the "std" namespace.  Thus you can include either, depending on how you feel about namespaces.  And I believe this is part of the standard.

Why 80 characters?  I ment 800 to be safe.  : - ) No fixed length string is every long enough.  


To nietod:

I think you should get the points and I assume lucidity agree also....
Can you tell me how to pass the points to you?

Well, I'm not sure lucidity would agree.  But the choice is yours (and the client is always right).  

You can reject his answer and I'll submit a "dummy" answer.  I appreciate the fact that you are taking the time seen to it that the correct (in your opinion) expert gets the points.  Too many times clients just grade the proposed answer regardless of who gave the answer they prefer.
Unlock this solution and get a sample of our free trial.
(No credit card required)
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.