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
3
  • 1
    It's a non-standard function, and may not be included with your stdlib.h. see wikipedia, en.wikipedia.org/wiki/Itoa Commented Apr 7, 2011 at 1:57
  • 1
    Define your main as int main(). More here : stackoverflow.com/questions/636829/… Commented Apr 7, 2011 at 1:58
  • all questions are important - for somebody :-) Commented Apr 7, 2011 at 2:13

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

If available you should be using snprintf() to avoid buffer overflows.
+1, although snprintf would be better due to potential buffer overflows with sprintf (won't happen here though).
I agree that snprintf is better if you can buffer overflow.
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.