String-Conversion Functions
This section presents the string-conversion functions from the general utilities library (<stdlib.h>). These functions convert strings of digits to integer and floating-point values. Following table summarizes the string-conversion functions.
Note the use of const to declare variable nPtr in the function headers (read from right to left as “nPtr is a pointer to a character constant”); const specifies that the argument value will not be modified.
Function prototype |
Function Description |
---|---|
double atof( const char *nPtr ); |
Converts the string nPtr to double |
int atoi( const char *nPtr ); |
Converts the string nPtr to int. |
long atol( const char *nPtr ); |
Converts the string nPtr to long int. |
double strtod( const char *nPtr, char **endPtr ); |
Converts the string nPtr to double. |
long strtol( const char *nPtr, char **endPtr, int base ); |
Converts the string nPtr to long. |
unsigned long strtoul( const char *nPtr, char **endPtr, int base ); |
Converts the string nPtr to unsigned long |
Function atof
Function atof converts its argument a string that represents a floating-point Number to a double value. The function returns the double value. If the converted value cannot be represented for example, if the first character of the string is a letter the behavior of function atof is undefined.
Example:
/* how to use atof */
#include <stdio.h>
#include <stdlib.h>
.
int main(void)
{
double d; /*variable to hold converted string*/
d = atof(“55.0”);
printf( "%s%.3f\n%s%.3f\n", "The string \"55.0\
converted to double is ", d, "The converted value
divided by 2 is ", d / 0 );
return 0;
}
Output:
The string "55.0" converted to double is 55.000
The converted value divided by 2 is 27.500
Function atoi
Function atoi converts its argument a string of digits that represents an integer to an int value. The function returns the int value. If the converted value cannot be represented, the behavior of function atoi is undefined.
Example
/*how to use atoi */
#include<stdio.h>
#include<stdlib.h>
int main( void )
{
int i;
i = atoi(“2593”) ;
printf( "%s%d\n%s%d\n", "the string \"2593\ converted to int is ", i, "the converted value minus 593 is ", i - 593 );
return 0;
}
Output:
The string "2593" converted to int is 2593
The converted value minus 593 is 2000
Function atol
Function atol converts its argument a string of digits representing a long integer to a long value. The function returns the long value. If the converted value cannot be represented, the behavior of function atol is undefined. If int and long are both stored in 4 bytes, function atoi and function atol work identically.
Example:
/* how to use atol*/
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
long l; /* variable to hold converted string */
l = atol( "1000000" );
printf( "%s%ld\n%s%ld\n",
"The string \"1000000\" converted to long int is ", l,
"The converted value divided by 2 is ", l / 2 );
return 0;
}
Output:
The string "1000000" converted to long int is 1000000
The converted value divided by 2 is 500000
Function strtod
Function strtod converts a sequence of characters representing a floating-point value to double. The function receives two arguments a string (char *) and a pointer to a string (char **). The string contains the character sequence to be converted to double. The pointer is assigned the location of the first character after the converted portion of the string.
d = strtod( string, &stringPtr );
indicates that d is assigned the double value converted from a string, and stringPtr is assigned the location of the first character after the converted value in the string.
Example:
/* how to use strtod */
#include<stdio.h>
#include <stdlib.h>
int main( void )
{
/* initialize string pointer */
const char *string = "65.2% are admitted"; /* initialize string */
double d; /* variable to hold converted sequence */
char *stringPtr; /* create char pointer */
d = strtod( string, &stringPtr );
printf( "The string \"%s\" is converted to the\n", string );
printf( "double value %.2f and the string \"%s\"\n", d, stringPtr );
return 0;
}
Output:
The string "65.2% are admitted" is converted to the
double value 65.20 and the string "% are admitted"
Function strtol
Function strtol converts to long a sequence of characters representing an integer. The function receives three arguments a string (char *), a pointer to a string and an integer. The string contains the character sequence to be converted. The pointer 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.
x = strtol( string, &remainderPtr, 0 );
indicates that x is assigned the long value converted from string. The second argument, remainderPtr, is assigned the remainder of the string after the conversion.
Example:
/*how to use strtol*/
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
const char *string = "-1234567xyz"; /* initialize string pointer */
char *remainderPtr; /* create char pointer */
long x; /* variable to hold converted sequence */
.
x = strtol( string, &remainderPtr, 0 );
.
printf( "%s\"%s\"\n%s%ld\n%s\"%s\"\n%s%ld\n",
"The original string is ", string,
"The converted value is ", x,
"The remainder of the original string is ", remainderPtr,
"The converted value plus 567 is ", x + 567 );
return 0;
}
Output:
The original string is "-1234567xyz"
The converted value is -1234567
The remainder of the original string is "xyz"
The converted value plus 567 is -1234000
Function strtoul
Function strtoul converts to unsigned long a sequence of characters representing an unsigned long integer. The function works identically to function strtol. The statement
x = strtoul( string, &remainderPtr, 0 );
Following example indicates that x is assigned the unsigned long value converted from string. The second argument, &remainderPtr, is assigned the remainder of string after the conversion. The third argument, 0, indicates that the value to be converted can be in octal, decimal or hexadecimal format.
Example :
/*how to use strtoul*/
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
const char *string = "1234567xyz"; /* initialize string pointer */
unsigned long x; /* variable to hold converted sequence */
char *remainderPtr; /* create char pointer */
x = strtoul( string, &remainderPtr, 0 );
printf( "%s\"%s\"\n%s%lu\n%s\"%s\"\n%s%lu\n",
"The original string is ", string,
"The converted value is ", x,
"The remainder of the original string is ",
remainderPtr,
"The converted value minus 567 is ", x - 567 );
return 0;
}
Output:
The original string is "1234567xyz"
The converted value is 1234567
The remainder of the original string is "xyz"
The converted value minus 567 is 1234000