Chapter Contents |
Previous |
Next |
atoi |
Portability: | ISO/ANSI C conforming, UNIX compatible |
SYNOPSIS | |
DESCRIPTION | |
RETURN VALUE | |
CAUTION | |
DIAGNOSTICS | |
IMPLEMENTATION | |
EXAMPLE | |
RELATED FUNCTIONS | |
SEE ALSO |
SYNOPSIS |
#include <stdlib.h> int atoi(const char *str);
DESCRIPTION |
atoi
converts the character string
str
to an
integer. The string should consist of an optional plus or minus sign, followed
by one or more decimal digits. Initial white-space characters are ignored.
RETURN VALUE |
atoi
returns the integer value represented by the character string up to the first
unrecognized character. If no initial segment of the string is a valid integer,
the return value is 0.
CAUTION |
No indication of overflow or other error
is returned, so you should validate the string before calling
atoi
.
DIAGNOSTICS |
No indication is returned to the program to specify whether the string contains a valid integer.
If the correct value is too large to be stored in a
370
long
, either
LONG_MAX
(231-1) or
LONG_MIN
(-231) is returned, depending
on the sign of the value.
IMPLEMENTATION |
atoi(x)
is implemented as
(int)atol(x)
.
EXAMPLE |
#include <stdlib.h> #include <stdio.h> #include <string.h> #define MAXLINE 25 main() { int value; char *input; char line[MAXLINE]; puts("Enter a number: "); input = gets(line); /* If string contains only digits, white space, or +/-, */ /* convert. Note that the input may still not represent */ /* a valid integer. Consider +-14+5. **/ if (strspn(input, " +-0123456789") == strlen(input)){ value = atoi(input); printf(" The integer equivalent of given string is %d\n", value); } else printf("Invalid count value: %s\n", input); }
RELATED FUNCTIONS |
SEE ALSO |
Chapter Contents |
Previous |
Next |
Top of Page |
Copyright © 2001 by SAS Institute Inc., Cary, NC, USA. All rights reserved.