In Section 22.8, we discussed several of C++’s most popular C string-manipulation functions. In the next several sections, we cover the remaining functions, including functions for converting strings to numeric values, functions for searching strings and functions for manipulating, comparing and searching blocks of memory.
This section presents the C string-conversion functions
from the general-utilities library <cstdlib>
. These functions convert C strings to integer and floating-point values. In new code, C++ programmers typically use the string stream processing capabilities (Chapter 21) to perform such conversions. Figure 22.27 summarizes the C string-conversion functions. When using functions from the general-utilities library, include the <cstdlib>
header.
Prototype | Description |
---|---|
double atof(const char* nPtr) |
Converts the string nPtr to double . If the string cannot be converted, 0 is returned. |
int atoi(const char* nPtr) |
Converts the string nPtr to int . If the string cannot be converted, 0 is returned. |
long atol(const char* nPtr) |
Converts the string nPtr to long int . If the string cannot be converted, 0 is returned. |
double strtod(const char* nPtr, char** endPtr) |
|
Converts the string nPtr to double . endPtr is the address of a pointer to the rest of the string after the double . If the string cannot be converted, 0 is returned. |
|
long strtol(const char* nPtr, char** endPtr, int base) |
|
Converts the string nPtr to long . endPtr is the address of a pointer to the rest of the string after the long . If the string cannot be converted, 0 is returned. The base parameter indicates the base of the number to convert (e.g., 8 for octal, 10 for decimal or 16 for hexadecimal). The default is decimal. |
|
unsigned long strtoul(const char* nPtr, char** endPtr, int base) |
|
Converts the string nPtr to unsigned long . endPtr is the address of a pointer to the rest of the string after the unsigned long . If the string cannot be converted, 0 is returned. The base parameter indicates the base of the number to convert (e.g., 8 for octal, 10 for decimal or 16 for hexadecimal). The default is decimal. |
Function atof
(Fig. 22.28, line 8) converts its argument—a string that represents a floating-point number—to a double
value. The function returns the double
value. If the string cannot be converted—for example, if the first character of the string is not a digit— function atof
returns zero.
Function atoi
(Fig. 22.29, line 8) converts its argument—a string of digits that represents an integer—to an int
value. The function returns the int
value. If the string cannot be converted, function atoi
returns zero.
Function atol
(Fig. 22.30, line 8) converts its argument—a string of digits representing a long integer—to a long
value. The function returns the long
value. If the string cannot be converted, function atol
returns zero. If int
and long
are both stored in four bytes, function atoi
and function atol
work identically.
Function strtod
(Fig. 22.31) converts a sequence of characters representing a floating-point value to double
. Function strtod
receives two arguments—a string (char*
) and the address of a char*
pointer (i.e., a char**
). The string contains the character sequence to be converted to double
. The second argument enables strtod
to modify a char*
pointer in the calling function, such that the pointer points to the location of the first character after the converted portion of the string. Line 11 indicates that d
is assigned the double
value converted from string
and that stringPtr
is assigned the location of the first character after the converted value (51.2
) in string
.
Function strtol
(Fig. 22.32) converts to long
a sequence of characters representing an integer. The function receives a string (char*
), the address of a char*
pointer and an integer. The string contains the character sequence to convert. The second argument is assigned the location of the first character after the converted portion of the string. The integer specifies the base of the value being converted. Line 11 indicates that x
is assigned the long
value converted from string
and that remainderPtr
is assigned the location of the first character after the converted value (-1234567
) in string1
. Using a null pointer for the second argument causes the remainder of the string to be ignored. The third argument, 0
, indicates that the value to be converted can be in octal (base 8), decimal (base 10) or hexadecimal (base 16). This is determined by the initial characters in the string—0
indicates an octal number, 0x
indicates hexadecimal and a number from 1
to 9
indicates decimal.
In a call to function strtol
, the base can be specified as zero or as any value between 2 and 36. (See Appendix D for a detailed explanation of the octal, decimal, hexadecimal and binary number systems.) Numeric representations of integers from base 11 to base 36 use the characters A–Z to represent the values 10 to 35. For example, hexadecimal values can consist of the digits 0–9 and the characters A–F. A base-11 integer can consist of the digits 0–9 and the character A. A base-24 integer can consist of the digits 0–9 and the characters A–N. A base-36 integer can consist of the digits 0–9 and the characters A–Z. [Note: The case of the letter used is ignored.]
Function strtoul
(Fig. 22.33) converts to unsigned long
a sequence of characters representing an unsigned long
integer. The function works identically to strtol
. Line 12 indicates that x
is assigned the unsigned long
value converted from string
and that remainderPtr
is assigned the location of the first character after the converted value (1234567
) in string1
. The third argument, 0
, indicates that the value to be converted can be in octal, decimal or hexadecimal format, depending on the initial characters.