Nelson Tatius
Nelson Tatius

Reputation: 8043

Is atoi() part of C standard?

Is atoi() part of C standard?

What should I use to convert char* to int if atoi() isn't standardised?

Upvotes: 0

Views: 4182

Answers (4)

Keith Thompson
Keith Thompson

Reputation: 263287

Yes, atoi() is part of standard C -- unfortunately.

I say "unfortunately" because it does no error checking; if it returns 0, you can't tell whether it's because you passed it "0" or because you passed it "hello, world\n" (which has may have undefined behavior, but typically returns 0).

The strtol() function is more complicated to use, but it does proper error checking. It returns a long result, which you can then convert to int -- ideally after checking that it's in the range INT_MIN to INT_MAX.

Reference: N1570 7.22.1.2.

Upvotes: 9

Reed Copsey
Reed Copsey

Reputation: 564433

It is part of the C Standard Library, and should be declared within stdlib.h

Upvotes: 1

CB Bailey
CB Bailey

Reputation: 791929

atoi is part of the current C standard but consider strtol which is also part of the standard and has a more robust interface.

Upvotes: 1

user334856
user334856

Reputation:

Yes, it is standard. From man atoi:

NAME atoi, atoi_l -- convert ASCII string to integer

LIBRARY Standard C Library (libc, -lc)

#include <stdlib.h>

However, it also says:

The atoi() function has been deprecated by strtol() and should not be used in new code.

Upvotes: 1

Related Questions