views:

156

answers:

3

Is it possible to use toString operator, or how to convert numbers to char arrays.

+4  A: 
int myNumber = 27; /* or what have you */
char myBuffer[100];
snprintf(myBuffer, 100, "%d", myNumber);

There are several considerations for you to think about here. Who provides the memory to hold the string? How long do you need it for, etc? (Above it's a stack buffer of 100 bytes, which is way bigger than necessary for any integer value being printed.)

Best answer: start using Java. Or Javascript, or C#, or for the love of God almost anything but C. Only tigers lie this way.

quixoto
Comparing C with Javascript or C# isn't very useful, IMO. Each language has its use. I would rather program in C than in Javascript or C# :-)
Alok
Life is too short to write pure C.
quixoto
You should be using `snprintf()` instead of `sprintf()` if it's available.
Chinmay Kanchi
OK. Life is still too short. :)
quixoto
I'd still use C over JavaScript or C# for most tasks. Maybe Python. But still, C has its uses, and they won't go away soon!
chpwn
Surely you mean `myNumber` in the last line, or name your variable `myInt`.
Edd
No autocomplete either on SO! Life still too short. I'm outta here.
quixoto
Best luck to write a kernel or driver in Java. Or Javascript.
KennyTM
+2  A: 

Use the sprintf() function.

Marcel Gheorghita
You should be using `snprintf()` instead of `sprintf()` if it's available.
Chinmay Kanchi
+2  A: 

sprintf() is considered unsafe because it can lead to a buffer overflow. If it's available (and on many platforms it is), you should use snprintf() instead. Consider the following code:

#include <stdio.h>
int main()
{
    int i = 12345;
    char buf[4];
    sprintf(buf, "%d", i);
}

This leads to a buffer overflow. So, you have to over-allocate a buffer to the maximum size (as a string) of an int, even if you require fewer characters, since you have the possibility of an overflow. Instead, if you used snprintf(), you could specify the number of characters to write, and any more than that number would simply be truncated.

#include <stdio.h>
int main()
{
    int i = 12345;
    char buf[4];
    snprintf(buf, 4, "%d", i);
    //truncates the string to 123
}

Note that in either case, you should take care to allocate enough buffer space for any valid output. It's just that snprintf() provides you with a safety net in case you haven't considered that one edge case where your buffer would otherwise overflow.

Chinmay Kanchi
On some platforms, it's `_snprintf`.
R Samuel Klatchko
Why is it safer to truncate than to over-allocate a buffer to the maximum possible size to represent an int? There are many real world cases where truncating would have dire consequences. Printing out a medical prescription, for example.
Dipstick
Agreed. The example was for illustrative purposes only. However, silent truncation should be preferred to overflowing a buffer as the latter is undefined behaviour and could cause worse than an incorrect result. Care should be taken to allocate enough space in either case.
Chinmay Kanchi
Edited to clarify.
Chinmay Kanchi
If you're truly worried about truncation, check the return value of `snprintf`. On most platforms it returns the number of bytes that would have been used if the buffer was large enough. If dynamically allocating memory, you can pass in a zero length and use the return value to `malloc` enough space to hold the formatted string.
tomlogic
@tomlogic - Why is it safer (or in any way better) to use malloc() rather than to over-allocate a buffer to the maximum possible size needed to represent an int? If the int is volatile the size needed may have changed since the call to snprintf().
Dipstick
@chrisharris: in the simple case of formatting a single integer, it's easy to allocate a buffer large enough to hold the result. If you're dealing with other strings of unknown length (for example, a filename), then it's safer to use snprintf to find out how large of a buffer is required. (Also, good point on the volatile).
tomlogic