10

I'm a C noob and I just found out that atoi is deprecated in favor of strtol etc.

Where can I find a list of deprecated C functions ?

Neeladri Vishweswaran
  • 1,695
  • 5
  • 22
  • 40

1 Answers1

3

There's a difference between unsafe and deprecated. atoi() is unsafe, however gcc is not going to tell you to stop using it because it's dangerous. Using gets() produces a different result :) YCMV (your compiler may vary).

In general, if a function can fail and no error checking is possible, don't use it. If a function lets you write to a region of memory with out being able to pass a size limit, don't use it.

The latter is easier to determine just by the function prototype. However, if you are somewhat conscious of what you are doing, you'll quickly realize that you have no way of knowing if what you got from atoi() was really the string representation of the result that a user just entered on the command line.

This rationale is not at all exclusive to the standard C library. You will encounter lots and lots of library code, some of it good. No list can replace learned, defensive coding habits.

Community
  • 1
  • 1
Tim Post
  • 33,371
  • 15
  • 110
  • 174
  • gcc is probably assuming you're dealing with a forgiving implementation of the C library. Conceptually, `atoi` and `gets` are equally bad - both result in **undefined behavior** unless you have strict control over the inputs they will receive. – R.. GitHub STOP HELPING ICE Sep 20 '10 at 01:50
  • `gets` was the first and only function to be deprecated (in C99 TC3). It was later removed entirely in C11. So at this point, there are *no* deprecated functions in the language. (There's also a reference to the use of ungetc at the beginning of a binary file being deprecated in C99 (It's undefined behavior now). I couldn't find a version of C90 to compare with, though, so I don't know what it used to do.) – Ray Aug 31 '16 at 23:08