Embedded Artistry libc
C Standard Library Support for Bare-metal Systems
atol.c File Reference
#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
Include dependency graph for atol.c:

Go to the source code of this file.

Functions

long atol (const char *str)
 Interprets a Long value in a byte string pointed to by str. More...
 

Function Documentation

◆ atol()

long atol ( const char *  str)

Interprets a Long value in a byte string pointed to by str.

Interprets a Long value in a byte string pointed to by str. Discards any whitespace characters until the first non-whitespace character is found, then takes as many characters as possible to form a valid long number representation and converts them to an long value. The valid Long value consists of the following parts: a) (optional) plus or minus sign b) numeric digits

Parameters
strpointer to the null-terminated byte string to be interpreted
Returns
long value corresponding to the contents of str on success. If the converted value falls out of range of corresponding return type, the return value is undefined. If no conversion can be performed, ​0​ is returned.

Definition at line 5 of file atol.c.

6 {
7  long val = 0;
8  bool neg = false;
9 
10  while(isspace(*str))
11  {
12  str++;
13  }
14 
15  switch(*str)
16  {
17  case '-':
18  neg = true;
19  // intentional fallthrough
20  case '+':
21  str++;
22  }
23 
24  while(isdigit(*str))
25  {
26  val = (10 * val) + (*str++ - '0');
27  }
28 
29  return neg ? -val : val;
30 }
int isspace(int ch)
Checks if the given character is a whitespace character.
Definition: isspace.c:5
int isdigit(int ch)
Checks if the given character is a numeric character.
Definition: isdigit.c:5

References isdigit(), and isspace().