Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I've a simple code - this works on other platforms but does not work in ubuntu.

#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=99;
char str[100];
itoa(99, str, 10);
return 0;
}

Trying to compile in using terminal in gcc with :

gcc test.c

But I get the error:

/tmp/ccJN77g6.o: In function `main':
test.c:(.text+0x35): undefined reference to `itoa'
collect2: ld returned 1 exit status

Why so? prototype for itoa is included in stdlib.h

share|improve this question
    
This question turns out to be about how the C library on Ubuntu doesn't contain the itoa function, which is contained by C library implementations on some other operating systems. So should this really be considered off-topic for our site? Is the community at Stack Overflow really better suited to this question? In any case, if the answer is yes, I suggest this be migrated. –  Eliah Kagan Oct 6 '12 at 1:01
    
@EliahKagan - add this as an answer and lets get a couple of upvotes for you to close. –  fossfreedom Oct 18 '12 at 19:57
    
It's been in stackoverflow awhile: stackoverflow.com/questions/6462938/… –  ubfan1 Oct 18 '12 at 20:19
add comment

3 Answers

itoa doesn't seem to be ANSI C++ and thus is very likely to be not supported by gcc.

According to this source, a the following workaround is suggested:

This function is not defined in ANSI-C and is not part of C++, but is supported by some compilers.

A standard-compliant alternative for some cases may be sprintf:

    sprintf(str,"%d",value) converts to decimal base.
    sprintf(str,"%x",value) converts to hexadecimal base.
    sprintf(str,"%o",value) converts to octal base.

A sprintf reference can be found here.

share|improve this answer
add comment

itoa function is not portable, non standard and most Linux compilers doesn't support it.

Instead you should use snprintf() function

Check the snprintf reference here

#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=99;
char str[100];
// itoa(99, str, 10);
snprintf(str,10,"%d", x);
return 0;
}
share|improve this answer
add comment

I used the code you entered and I get the same error. It would appear that itoa() isn't ANSI C standard and doesn't work with GCC on Linux (at least the version I'm using). Things like this are frustrating especially if you want your code to work on different platforms (Windows/Linux/Solaris/whatever).

It is not a standard C function. Here are some links that might get you started to find a way around the function:

link1 Stack overflow question

Hope it helps.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.