int to string (char) conversion in C
Bob Keane

Hello all. I am new to C programming and was wondering if it is possible to convert an integer value to char or string. I checked a book, but only found documentation to convert from string to integer. Thanks in advance.

X-G

A char is an integer type already.
sprintf

ReyBrujo

int to char is done by simply casting:

int i = 10;
char c;
c = i;

Remember that chars can hold 256 numbers only. As for string, just use sprintf, like sprintf(buffer, "%d", i);, where buffer is a char array and i an integer. Remember to make the buffer as large as necessary.

LennyLen

If you want to convert from an integer to a string with a specified radix, use itoa().

itoa()

Bob Keane

Thanks for the help. If I were adding the numeric value to the end of an existing string, would the format be:

char buffer[16} = "<some characters>";
int place;
char c;
c = sprintf(buffer, "%d", place);?

[/edit] I tried itoa, got an undefined reference to itoa.
[edit/]

LennyLen

Nah, just:

char buffer[16} = "<some characters>";
int place = 10;

sprintf(buffer, "%d", place);

Bob Keane

That worked. Thanks LennyLen and X-G.

[\edit] Oops, closed the thread too soon. It appears to be overwriting what is in buffer, as opposed to adding place to the end. Any ideas?
[\edit]

LennyLen
Quote:

I tried itoa, got an undefined reference to itoa

You need to add the following to the top of your code:

#include <stdlib.h>

Bob Keane

<stdlib.h> was included. Is it recent? Maybe my compiler does not support it yet.

LennyLen

It's not new, but it's not an ANSI C funtion either, though most compilers support it. Which are you using?

The following compiled fine for me using GCC.

1#include <stdio.h>
2#include <stdlib.h>
3 
4char buffer[20];
5int n;
6 
7int main() {
8
9 n = 25;
10
11 itoa(n, buffer, 2);
12 printf("%d in binary is %s\n", n, buffer);
13
14 itoa(n, buffer, 16);
15 printf("%d in hexadecimal is %s\n", n, buffer);
16
17 return 0;
18
19}

Bob Keane

I am using gcc 4.0.2. I copied the test program you submitted and compiled, still having problems with the itoa function. Are you coding in C or C++? I coded in C.

LennyLen

I compiled as C. I'm currently using GCC 3.4.2, so perhaps it's been removed in later versions.

CGamesPlay

New rule: no creating new programming forum threads until you read the thread list!

Is this week the printf assignment at the college that you three attend?

http://www.allegro.cc/forums/thread/588773
http://www.allegro.cc/forums/thread/588701

Not that I'm being hostile or anything.

Bob Keane

CGamesPlay: My origional question was how to convert an int to char or string. To get back on topic, I tried sprintf, the new characters overwrote what was in the target string. Is there any way to add the characters to the end of the string, or did I do something wrong?

LennyLen
#include <stdio.h>
#include <string.h>

char buffer[20], buffer2[20];

int main() {
  
     n = 25;
     strcpy(buffer, "stuff");
     sprintf(buffer2, "%d", n);
     strcat(buffer, buffer2);
  
     return 0;

}

Bob Keane

That was it. Thanks.

Thread #588784. Printed from Allegro.cc