What is the difference between _itoa and itoa?
Asked Answered
F

5

10

Visual Studio is yelling at me about using itoa() saying to use _itoa() instead?

It looks to me like they are the same function. What gives?

Finicky answered 19/10, 2009 at 2:47 Comment(0)
G
23

A C run time library implementation is not supposed to introduce names that aren't in the standard unless they follow a certain naming convention (like starting with an underscore). The earlier versions of Microsoft's compiler didn't follow this rule particularly closely, but over time, Microsoft has been moving more toward making their implementation more standards compliant. So functions they used to supply that would intrude on the user's namespace they have been implementing using names that are reserved for compiler implementations and have been deprecating the old names.

If _CRT_NONSTDC_NO_WARNINGS is defined, the MS compiler won't complain about the itoa() function being deprecated. But it will still complain about it being unsafe (you have to define _CRT_SECURE_NO_WARNINGS to quiet that warning). Or use the safer version of the function (_itoa_s()) that provides the function with the destination buffer size

Both _itoa() and itoa() resolve to the exact same function in the library down to the same address - there is no difference except in the name.

Grazia answered 19/10, 2009 at 5:57 Comment(0)
W
8

The MSDN documentation for itoa() says:

This POSIX function is deprecated beginning in Visual C++ 2005. Use the ISO C++ conformant _itoa or security-enhanced _itoa_s instead.

Wheelhouse answered 19/10, 2009 at 2:52 Comment(2)
Is it just the names that are different then??Finicky
this answer does not address whether or not _itoa and itoa are the same function...Klystron
V
5

itoa is not standard C.

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

So MSVS is telling you to use the _itoa to tell you that it is not standard C++ and that you should mark it as such. I believe that it is there for backwards compatibility and that this notation is for readability and distinction.

Ventriloquize answered 19/10, 2009 at 2:53 Comment(0)
T
4

itoa is not standard, so you should use stringstream instead.

you'll need #include <sstream>

an example of it's use would be:

int i = 5;
std::stringstream ss;

ss << i;

std:: cout << ss.str();
Tatro answered 19/10, 2009 at 3:3 Comment(3)
I dont find why we have to use a stream having _itoa() or _itoa_s(). I think that two alternatives are better choice that creating an stream to only convert a number to string.Balling
itoa isn't standard, you can still use it. Just make sure the compiler you're using supports it. a stringstream is just the proper way of doing it.Tatro
No, it's the C++ way of doing it, and being C++ does not mean it's right.Klystron
O
1

In reply to the answer by Bruce:

itoa is not standard, so you should use stringstream instead.

you'll need #include <sstream>

an example of it's use would be:

int i = 5; std::stringstream ss;

ss << i;

std:: cout << ss.str();

You can also code your own itoa() function instead

eg:

const char* itoa (int num)
{
    if (num == 0)
    {
        return "0";
    }
    bool neg = false;
    if (num < 0)
    {
        neg = true;
        num = -num;
    }

    int digits = 0;
    int tmp = num;

    while (tmp > 0)
    {
        digits++;
        tmp /= 10;
    }

    int digs[digits];

    for (tmp = digits; num > 0; tmp--)
    {
        digs[tmp] = num % 10;
        num /= 10;
    }

    string s = neg == true ? "-" : "";
    for (tmp = 1; tmp <= digits; tmp++)
    {
        s += (char)(digs[tmp] + 48);
    }
    return s.c_str();
}
Overgrow answered 27/10, 2009 at 10:0 Comment(1)
This function is inefficient, non portable and wasteful: the caller cannot properly discard the memory allocated for the string because the pointer returned could also point to a string literal. C++ is no Java.Seadon

© 2022 - 2024 — McMap. All rights reserved.