Converting an int to a char in C?

24,404

Solution 1

Given the revised requirement to get digit '0' into string[i] if number == 0, and similarly for values of number between 1 and 9, then you need to add '0' to the number:

assert(number >= 0 && number <= 9);
string[i] = number + '0';

The converse transform is used to convert a digit character back to the corresponding number:

assert(isdigit(c));

int value = c - '0';

Solution 2

If you want to convert a single digit to a number character you can use

string[i] = (char) (number+'0');

Of course you should check if the int value is between 0 and 9. If you have arbitrary numbers and you want to convert them to a string, you should use snprintf, but of course, then you can't squeeze it in a char aynmore, because each char represents a single digit.

If you create the digit representation by doing it manually, you should not forget that a C string requires a \0 byte at the end.

Solution 3

You'll want to use sprintf().

sprintf(string,'%d',number);

I believe.

EDIT: to answer the second part of your question, you're casting an integer to a character, which only holds one digit, as it were. You'd want to put it in a char* or an array of chars.

Share:
24,404
Rohan
Author by

Rohan

Developer at Microsoft Azure. Previously a student at The University of Texas at Austin.

Updated on January 18, 2020

Comments

  • Rohan
    Rohan over 4 years

    I'm trying to add an int to a char array. My (broken) code is as follows,

    string[i] = (char) number;
    

    with i being some int index of the array and number is some integer number. Actually, while typing this out I noticed another problem that would occur if the number is more than one digit, so if you have some answer to that problem as well that would be fantastic!

  • WhozCraig
    WhozCraig over 10 years
    Apart from being a non-standard GNU extension, the idea is correct.
  • Keith Thompson
    Keith Thompson over 10 years
    it's worth noting that this works because C requires the representations for '0' .. '9' to be contiguous. This method doesn't necessarily work for letters, which are not required to be contiguous; for example, 'a' + 25 is not necessarily 'z' (though it is in ASCII).
  • Rohan
    Rohan over 10 years
    @KeithThompson why is it that way? It seems to me that when I do char letter = 'a' and then letter++;, it continuously increments the ASCII value so that a + 25 would be 'z'.
  • Keith Thompson
    Keith Thompson over 10 years
    @Rohan: Yes, 'a' + 25 == 'z' -- for your implementation. My point is that the C standard doesn't guarantee that. In particular, it's not true for systems using ECBDIC rather than ASCII. C does guarantee that '0' + 9 == '9' are contiguous (but not that '0' == 48).
  • Jonathan Leffler
    Jonathan Leffler over 10 years
    For most systems, including systems using Unicode and the ISO 8859 code sets, then for the unaccented Latin alphabet, you can assert('z' - 'a' == 25);, but in EBCDIC, which is found primarily on IBM mainframes or in systems connecting to those, there are gaps in the alphabet (so 'a'..'i' are contiguous (0x81..0x89), and 'j'..'r' are contiguous (0x91..0x99), and 's'..'z' are contiguous (0xA2..0xA9)). There are valid non-alphabetic characters in the gaps between these ranges. See EBCDIC at Wikipedia for more information.