Kyle L
Kyle L

Reputation: 820

Problem with itoa converting integer to string

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

Upvotes: 2

Views: 7546

Answers (2)

GWW
GWW

Reputation: 44131

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.

Upvotes: 6

jonsca
jonsca

Reputation: 10381

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

Upvotes: 2

Related Questions