convert int to char* in standard C (without itoa)
First of all, itoa
is not a C++ thing.
You can simply use sprintf
:
sprintf(str, "%d", a)
In a real application you'll want to use snprintf
though to remove the risk of a buffer overflow:
str = malloc(16);
snprintf(str, 16, "%d", a);
And 15 characters are way enough to store an integer.
Alexandru N. Onea
I have always loved technology and the way it helps creating a better world, and I have always focused my passion on the study of Artificial Intelligence and electronics (mostly because I like the idea of these two going together well). However, my true passions is not related to any particular field, but rather to an idea, a dream of mine, of a world where each and every person contributes with their skills and knowledge to the advancement of the human kind, a world where each and every city is a truly democratic city and their citizens are part of the decision making processes, of an educational system where students are taught not to remember, but to discover, to explore and to create, where students are invited to collaborate and innovate. My passion is to make every small step, to meet every brilliant mind, and together to try and fail until all this puzzle is finally completed.
Updated on July 09, 2022Comments
-
Alexandru N. Onea almost 2 years
I have declared and initialized two variables as shown below:
int a=5; char* str; str = (char*)calloc(255, sizeof(char));
I want to convert the int to char* in standard C. I cannot use any conversion function from C++ such itoa.
I am using Ubuntu 11.10
-
Fred Foo over 12 years
sizeof(char)
is per definition equal to 1, andcalloc
is overkill. Also, casting the result fromcalloc
is useless. Juststr = malloc(BUFSIZE)
would suffice. -
ThiefMaster over 12 yearsYeah, just copied it from his question. However, there are many people who always use calloc instead of malloc even if one of the arguments is
1
since it nulls the memory and saves the hassle of doing a memset after the allocation. -
Fred Foo over 12 yearsfair enough, but given that you were making corrections already, I though you might want to add these in as well :)
-
Daniel Fischer over 12 years15 characters is not enough for long on most 64-bit platforms, it might be good to drop the habit of thinking long == int.
-
ThiefMaster over 12 yearsIf it's
int
, 15 chars should be sufficient. But you are right if it can be along
orlong long
. Then he needs a 21+ char buffer. -
ugoren over 12 yearsThe answer is correct, but I wouldn't mention the existence of
sprintf
. It's good that you suggestsnprintf
for a real application, but why not use it for "unreal" ones as well? -
user411313 over 12 yearssnprintf is only C99, not ANSI C,
str = malloc(BUFSIZE)
is wrong, right isstr = malloc(BUFSIZ)
-
ThiefMaster over 12 years@user411313: 1) The OP did not ask for ANSI C. 2)
BUFSIZE
is perfectly fine when defined by yourself - and I did defineBUFSIZE
in my answer. 3) Please get a nickname, the default user12345 nicks are annoying. -
R.. GitHub STOP HELPING ICE over 12 yearsANSI C is^H^Hwas C99. Now it's C11.