Search code examples
c++char32bit-64bititoa

itoa () does not work on 32bit tdm-gcc 5.1 even on including stdlib.h


I have a made a c++ program which uses itoa (). I compiled it on 64Bit compiler(TDM-GCC-5.1), it compiled and worked. But when i compiled it using 32 bit TDM-GCC-5.1 compiler i get the error itoa () was not declared in this scope. I tried compiling this on two different 32 bit machines still i got the same error and it by including cstdlib and stdlib.h still the same error in both the cases. It compiles and runs fine on a 64 bit machine. But why doesn't it do so on a 32 bit compiler??

Can you please suggest some alternative for 32 bit ?

code:

#include <iostream>
#include <stdlib.h>
using namespace std;

main()
{
int test;
char t[2];

itoa(test,t,10);
}

Compiler output:

C:\Users\hello\Desktop\Untitled1df.cpp  In function 'int main()':
C:\Users\hello\Desktop\Untitled1df.cpp  [Error] 'itoa' was not declared in this scope

Screenshot: the IDE screenshot


Solution

  • itoa is not a standard function. It is provided by some implementations and not by others. Avoid it in portable software.

    The 64-bit version of TDM GCC in its default mode happens to provide itoa, while the 32-bit version doesn't. In order to keep the behaviour consistent between versions, try e.g. -std=c++11 -DNO_OLDNAMES -D_NO_OLDNAMES.

    A standards-conforming alternative would be for example

    char buffer[20];
    snprintf (buffer, sizeof(buffer), "%d", number);
    

    Speaking of portability, main() without int is a grave error which is erroneously left without a diagnostic in some versions of GCC for Windows. It's a bug in these version of GCC. In addition, accessing an uninitialised variable test triggers undefined behaviour.