-2

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

Vedant
  • 145
  • 1
  • 13

1 Answers1

3

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.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • Thankyou and refering to the main () thing....i am a 12 grade student. So concepts of efficiency don't matter as i only make proof of concept softwares for project which dont have to be released in the market, so i have to ignore efficiency and the programs are not mission critical. I never knew not using int is a grave error as my compiler never pointed that out. Anyways thankyou for informing me. And this just a example code so i didnt mind following programming guidelines. The actual code is pretty big with 6-7 header files, so i didnt find it appropriate to post the code. – Vedant Oct 30 '17 at 14:25
  • You must be meaning _correctness_ when you say "efficiency". I'd also assume that if you enable a minimal level of warnings on your compiler, it will point out malformed code on that level, unless it is some particular old version that hasn't been fixed. – underscore_d Oct 30 '17 at 14:33