© Mikael Olsson 2019
Mikael OlssonModern C Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-4288-9_22

22. Strings and Numbers

Mikael Olsson1 
(1)
Hammarland, Länsi-Suomi, Finland
 

The standard C library contains several functions for making conversions between strings and numbers.

String Conversion

One built-in conversion function is atoi , which converts a string into an integer type. The following example illustrates the use of this function by converting all command-line string arguments into numbers and adding them together.
#include <stdio.h> /* printf */
#include <stdlib.h> /* atoi */
int main(int argc, char* argv[]) {
  int i, sum=0;
  for(i=0; i < argc; i++) {
    sum += atoi(argv[i]);
  }
  printf("Sum is: %d\n", sum);
}
Passing a non-numeric argument to atoi will typically evaluate as zero; however, the C standard does not define this behavior so it is not guaranteed across all implementations. Also, the function does not distinguish between a failed conversion and the number zero. Here is an example output from running the program with both correct and incorrect arguments.
gcc myapp.c -o myapp.exe
./myapp.exe 2 3
Sum is 5
./myapp.exe Hello 0
Sum is 0
Since no error-handling capabilities are provided by atoi, use of this function is discouraged. The preferred alternative is to instead convert strings with the safer strtol function . As seen in the following code, this function takes as many characters as it can to form a valid integer value. If a valid integer cannot be formed, then zero is guaranteed to be returned.
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
int main(void) {
  char str[] = "-123test";
  char *end;
  long r;
  r = strtol(str, &end, 10);
  printf("Number part %ld\n", r); /* -123 */
  printf("String part %s\n", end); /* "test" */
}
In addition to the string to be converted, the strtol function takes two more arguments, a char pointer and an integer. The char pointer will point to the last parsed character in the string, so for a successful conversion of the entire string this will be the null character found at the end of the string. The integer argument is the expected base value (between 2 and 36) of the integer, so for a hexadecimal number this would be set to 16 instead of 10, as seen in the next example. Note that the function skips any whitespace characters at the beginning of the string and stops if it encounters a non-numeric character.
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
int main(void) {
  char str[] = "5 F";
  char *end;
  long r1, r2;
  r1 = strtol(str,&end,10); /* 5 */
  r2 = strtol(end,&end,16); /* 15 */
  if (*end == '\0') { /* entire string parsed */
    printf("%ld+%ld is %ld",r1,r2,r1+r2); /* 5+15 is 20 */
  }
}
Recall that the errno variable is changed by some C library functions to indicate certain errors. In the case of the strtol function , the errno variable is used to signal overflow, which is caused when the number is too large or small to fit in the long data type. When this occurs errno is set to ERANGE, which is a macro defined in errno.h to represent a range error. In the next example, overflow occurs when trying to convert a number larger than what the long type can hold. The long type will then be assigned the largest value it can hold instead, a shortcut for which is provided by the LONG_MAX macro.
#include <stdio.h> /* printf */
#include <stdlib.h> /* strtol */
#include <errno.h> /* errno */
int main(void) {
  const char* str = "9223372036854775808"; /* LONG_MAX+1 */
  char *end;
  long res;
  errno = 0;
  res = strtol(str,&end,10); /* LONG_MAX */
  if (errno == ERANGE) {
    printf("Overflow"); /* "Overflow" */
  }
}

Number Conversion

When printf is called with a number as an argument, there is a string conversion happening. The standard library provides a variant of printf called snprintf that can be used to store the resulting string instead of printing it, as seen in the following code.
int main(void) {
  int num = 123;
  char buffer[4];
  snprintf(buffer, sizeof(buffer), "%d", num);
}

In addition to the typical arguments supplied to printf, the snprintf function takes two more arguments at the start: the output buffer and the maximum number of characters (including the null character) that it will store in the buffer.