2

I am getting compilation errors when i tried to compile the code as shown below

#include <stdlib.h>

main()
{
    int val = 10;
    char buff[10];
    char *ptr;
    ptr = ltoa(val,buff,10);
    printf("The val is %s\n",buff);
}

I get the compilation errors shown below:

[mcanj@varaprada ~]$ cc -g samp2.c
samp2.c: In function `main':
samp2.c:8: warning: assignment makes pointer from integer without a cast
samp2.c:11:2: warning: no newline at end of file
/tmp/ccifnKFx.o(.text+0x23): In function `main':
/home/mcanj/samp2.c:8: undefined reference to `ltoa'
collect2: ld returned 1 exit status.

Please let me know how to resolve this issue. Thanks and regards.

Dan Fego
  • 13,644
  • 6
  • 48
  • 59
Maddy
  • 503
  • 4
  • 12
  • 21

4 Answers4

4

It is itoa() and not ltoa() but even itoa() is not a Standard Library function.
If you want your program to be portable use sprintf() or snprintf() in C99.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
3

It's itoa, not ltoa. just change the call and it will be fine.

asaelr
  • 5,438
  • 1
  • 16
  • 22
  • 1
    Correct. The compiler warning you're getting is misleading; it doesn't know what `ltoa` is, so assumes that it returns an integer. This might lead you to assume that `ltoa` returns an `int`. The real error is in the linker, where it explains that it couldn't find the function `ltoa` in any available code. – Anthony Jan 23 '12 at 13:41
  • 1
    On linux, the chances that `itoa` is in `stdlib.h` are slim. – Daniel Fischer Jan 23 '12 at 14:07
1

see http://www.cplusplus.com/reference/clibrary/cstdlib/itoa/

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

http://www.strudel.org.uk/itoa/

Arrgghh C/C++! 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).

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • 1
    What's frustrating is all the bad Windows-oriented books and tutorials and examples that teach you bogus non-portable stuff like `itoa` when there are cleaner, universally-portable alternatives. – R.. GitHub STOP HELPING ICE Jan 23 '12 at 17:47
1

C does not have itoa or ltoa functions, C has atoi function that converts a string pointed to to an int representation.

You have to implement the function if you want to use it.

ouah
  • 142,963
  • 15
  • 272
  • 331