2

Perhaps not a really important question, but just starting out in c. Why will this not compile correctly?

#include <stdio.h>
#include <stdlib.h>

void main()
{
int i = 15;
char word[100];

itoa (i,word,10);

printf("After conversion, int i = %d, char word = %s", i, word);
}

I keep getting the error message

Undefined symbols:
"_itoa", referenced from:
_main in ccubsVbJ.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
kalaracey
  • 815
  • 1
  • 12
  • 28
  • 1
    It's a non-standard function, and may not be included with your stdlib.h. see wikipedia, http://en.wikipedia.org/wiki/Itoa – thelaws Apr 07 '11 at 01:57
  • 1
    Define your main as `int main()`. More here : http://stackoverflow.com/questions/636829/difference-between-void-main-and-int-main –  Apr 07 '11 at 01:58
  • all questions are important - for somebody :-) – AndersK Apr 07 '11 at 02:13

2 Answers2

6

Use sprintf instead, itoa is not part of the C standard and it's probably not included in your stdlib implementation.

sprintf(word,"%d",i);

UPDATE:

As noted in the comments snprintf is safer to use to avoid buffer overflows. In this case it's not necessary because your buffer is longer than the largest integer that can be stored in a 32-bit (or 64-bit integer). But it's good to keep buffer overflows in mind.

GWW
  • 43,129
  • 11
  • 115
  • 108
2

itoa is a non-standard function, it may not be included in your implementation. Use something like sprintf instead.

jonsca
  • 10,218
  • 26
  • 54
  • 62