HP C
Run-Time Library Reference Manual for OpenVMS Systems


Previous Contents Index


strtod

Converts a given string to a double-precision number.

Format

#include <stdlib.h>

double strtod (const char *nptr, char **endptr);

Function Variants The strtod function has variants named _strtod32 and _strtod64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.10 for more information on using pointer-size-specific functions.

Arguments

nptr

A pointer to the character string to be converted to a double-precision number.

endptr

The address of an object where the function can store the address of the first unrecognized character that terminates the scan. If endptr is a NULL pointer, the address of the first unrecognized character is not retained.

Description

The strtod function recognizes an optional sequence of white-space characters (as defined by isspace ), then an optional plus or minus sign, then a sequence of digits optionally containing a radix character, then an optional letter (e or E) followed by an optionally signed integer. The first unrecognized character ends the conversion.

The string is interpreted by the same rules used to interpret floating constants.

The radix character is defined the program's current locale (category LC_NUMERIC).

This function returns the converted value. For strtod , overflows are accounted for in the following manner:

If the string starts with an unrecognized character, then the conversion is not performed, *endptr is set to nptr, a 0 value is returned, and errno is set to EINVAL.)


Return Values

x The converted string.
0 Indicates the conversion could not be performed. errno is set to one of the following:
  • EINVAL - No conversion could be performed.
  • ERANGE - The value would cause an underflow.
  • ENOMEM - Not enough memory available for internal conversion buffer.
±HUGE_VAL Overflow occurred; errno is set to ERANGE.

strtok, strtok_r

Split strings into tokens.

Format

#include <string.h>

char *strtok (char *s1, const char *s2);

char *strtok_r (char *s, const char *sep, char **lasts);

Function Variants The strtok function has variants named _strtok32 and _strtok64 for use with 32-bit and 64-bit pointer sizes, respectively. Likewise, the strtok_r function has variants named _strtok_r32 and _strtok_r64 . See Section 1.10 for more information on using pointer-size-specific functions.

Arguments

s1

On the first call, a pointer to a string containing zero or more text tokens. On all subsequent calls for that string, a NULL pointer.

s2

A pointer to a separator string consisting of one or more characters. The separator string may differ from call to call.

s

A null-terminated string that is a sequence of zero or more text tokens separated by spans of one or more characters from the separator string sep.

sep

A null-terminated string of separator characters. This separator string can be different from call to call.

lasts

A pointer that points to a user-provided pointer to stored information needed for strtok_r to continue scanning the same string.

Description

The strtok function locates text tokens in a given string. The text tokens are delimited by one or more characters from a separator string that you specify. The function keeps track of its position in the string between calls and, as successive calls are made, the function works through the string, identifying the text token following the one identified by the previous call.

A token in s1 starts at the first character that is not a character in the separator string s2 and ends either at the end of the string or at (but not including) a separator character.

The first call to the strtok function returns a pointer to the first character in the first token and writes a null character into s1 immediately following the returned token. Each subsequent call (with the value of the first argument remaining NULL) returns a pointer to a subsequent token in the string originally pointed to by s1. When no tokens remain in the string, the strtok function returns a NULL pointer. (This can occur on the first call to strtok if the string is empty or contains only separator characters.)

Since strtok inserts null characters into s1 to delimit tokens, s1 cannot be a const object.

The strtok_r function is the reentrant version of strtok . The function strtok_r considers the null-terminated string s as a sequence of zero or more text tokens separated by spans of one or more characters from the separator string sep. The lasts argument points to a user-provided pointer to stored information needed for strtok_r to continue scanning the same string.

In the first call to strtok_r , s points to a null-terminated string, sep points to a null-terminated string of separator characters, and the value pointed to by lasts is ignored. The strtok_r function returns a pointer to the first character of the first token, writes a null character into s immediately following the returned token, and updates the pointer to which lasts points.

In subsequent calls, s is a NULL pointer and lasts is unchanged from the previous call so that subsequent calls move through the string s, returning successive tokens until no tokens remain. The separator string sep can be different from call to call. When no token remains in s, a NULL pointer is returned.


Return Values

x A pointer to the first character of the parsed token in the string.
NULL Indicates that there are no tokens remaining in the string.

Examples

#1

#include <stdio.h> 
#include <string.h> 
 
main() 
{ 
    static char str[] = "...ab..cd,,ef.hi"; 
    
    printf("|%s|\n", strtok(str, ".")); 
    printf("|%s|\n", strtok(NULL, ",")); 
    printf("|%s|\n", strtok(NULL, ",.")); 
    printf("|%s|\n", strtok(NULL, ",.")); 
} 
 
      

Running this example program produces the following results:


$ RUN STRTOK_EXAMPLE1
|ab| 
|.cd| 
|ef| 
|hi| 
$ 

#2

#include <stdio.h> 
#include <string.h> 
 
main() 
{ 
   char *ptr, 
        string[30]; 
 
   /* The first character not in the string "-" is "A".  The   */ 
   /* token ends at "C.                                        */ 
 
    strcpy(string, "ABC"); 
    ptr = strtok(string, "-"); 
    printf("|%s|\n", ptr); 
 
    /* Returns NULL because no characters not in separator      */ 
    /* string "-" were found (i.e.  only separator characters   */ 
    /* were found)                                              */ 
 
    strcpy(string, "-"); 
    ptr = strtok(string, "-"); 
    if (ptr == NULL) 
        printf("ptr is NULL\n"); 
 
}  
 
      

Running this example program produces the following results:


$ RUN STRTOK_EXAMPLE2
|abc| 
ptr is NULL 
$ 


strtol

Converts strings of ASCII characters to the appropriate numeric values.

Format

#include <stdlib.h>

long int strtol (const char *nptr, char **endptr, int base);

Function Variants The strtol function has variants named _strtol32 and _strtol64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.10 for more information on using pointer-size-specific functions.

Arguments

nptr

A pointer to the character string to be converted to a long .

endptr

The address of an object where the function can store a pointer to the first unrecognized character encountered in the conversion process (that is, the character that follows the last character in the string being converted). If endptr is a NULL pointer, the address of the first unrecognized character is not retained.

base

The value, 2 through 36, to use as the base for the conversion.

Description

The strtol function recognizes strings in various formats, depending on the value of the base. This function ignores any leading white-space characters (as defined by isspace in <ctype.h> ) in the given string. It recognizes an optional plus or minus sign, then a sequence of digits or letters that may represent an integer constant according to the value of the base. The first unrecognized character ends the conversion.

Leading zeros after the optional sign are ignored, and 0x or 0X is ignored if the base is 16.

If base is 0, the sequence of characters is interpreted by the same rules used to interpret an integer constant: after the optional sign, a leading 0 indicates octal conversion, a leading 0x or 0X indicates hexadecimal conversion, and any other combination of leading characters indicates decimal conversion.

Truncation from long to int can take place after assignment or by an explicit cast (arithmetic exceptions not withstanding). The function call atol (str) is equivalent to strtol (str, (char**)NULL, 10) .


Return Values

x The converted value.
LONG_MAX or LONG_MIN Indicates that the converted value would cause an overflow.
0 Indicates that the string starts with an unrecognized character or that the value for base is invalid. If the string starts with an unrecognized character, * endptr is set to nptr.

strtoq, strtoll (ALPHA, I64)

Convert strings of ASCII characters to the appropriate numeric values. strtoll is a synonym for strtoq .

Format

#include <stdlib.h>

__int64 strtoq (const char *nptr, char **endptr, int base);

__int64 strtoll (const char *nptr, char **endptr, int base);

Function Variants These functions have variants named _strtoq32 , _strtoll32 and _strtoq64 , _strtoll64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.10 for more information on using pointer-size-specific functions.

Arguments

nptr

A pointer to the character string to be converted to an __int64 .

endptr

The address of an object where the function can store a pointer to the first unrecognized character encountered in the conversion process (that is, the character that follows the last character in the string being converted). If endptr is a NULL pointer, the address of the first unrecognized character is not retained.

base

The value, 2 through 36, to use as the base for the conversion.

Description

The strtoq and strtoll functions recognize strings in various formats, depending on the value of the base. Any leading white-space characters (as defined by isspace in <ctype.h> ) in the given string are ignored. The functions recognize an optional plus or minus sign, then a sequence of digits or letters that may represent an integer constant according to the value of the base. The first unrecognized character ends the conversion.

Leading zeros after the optional sign are ignored, and 0x or 0X is ignored if the base is 16.

If base is 0, the sequence of characters is interpreted by the same rules used to interpret an integer constant: after the optional sign, a leading 0 indicates octal conversion, a leading 0x or 0X indicates hexadecimal conversion, and any other combination of leading characters indicates decimal conversion.

The function call atoq (str) is equivalent to strtoq (str, (char**)NULL, 10) .


Return Values

x The converted value.
__INT64_MAX or __INT64_MIN Indicates that the converted value would cause an overflow.
0 Indicates that the string starts with an unrecognized character or that the value for base is invalid. If the string starts with an unrecognized character, * endptr is set to nptr.

strtoul

Converts the initial portion of the string pointed to by nptr to an unsigned long integer.

Format

#include <stdlib.h>

unsigned long int strtoul (const char *nptr, char **endptr, int base);

Function Variants The strtoul function has variants named _strtoul32 and _strtoul64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.10 for more information on using pointer-size-specific functions.

Arguments

nptr

A pointer to the character string to be converted to an unsigned long .

endptr

The address of an object where the function can store a pointer to a pointer to the first unrecognized character encountered in the conversion process (that is, the character that follows the last character in the string being converted). If endptr is a NULL pointer, the address of the first unrecognized character is not retained.

base

The value, 2 through 36, to use as the base for the conversion. Leading zeros after the optional sign are ignored, and 0x or 0X is ignored if the base is 16.

If the base is 0, the sequence of characters is interpreted by the same rules used to interpret an integer constant: after the optional sign, a leading 0 indicates octal conversion, a leading 0x or 0X indicates hexadecimal conversion, and any other combination of leading characters indicates decimal conversion.


Return Values

x The converted value.
0 Indicates that the string starts with an unrecognized character or that the value for base is invalid. If the string starts with an unrecognized character, * endptr is set to nptr.
ULONG_MAX Indicates that the converted value would cause an overflow.

strtouq, strtoull (ALPHA, I64)

Convert the initial portion of the string pointed to by nptr to an unsigned __int64 integer. strtoull is a synonym for strtouq .

Format

#include <stdlib.h>

unsigned __int64 strtouq (const char *nptr, char **endptr, int base);

unsigned __int64 strtoull (const char *nptr, char **endptr, int base);

Function Variants These functions have variants named _strtouq32 , _strtoull32 and _strtouq64 , _strtoull64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.10 for more information on using pointer-size-specific functions.

Arguments

nptr

A pointer to the character string to be converted to an unsigned __int64 .

endptr

The address of an object where the function can store a pointer to a pointer to the first unrecognized character encountered in the conversion process (that is, the character that follows the last character in the string being converted). If endptr is a NULL pointer, the address of the first unrecognized character is not retained.

base

The value, 2 through 36, to use as the base for the conversion. Leading zeros after the optional sign are ignored, and 0x or 0X is ignored if the base is 16.

If the base is 0, the sequence of characters is interpreted by the same rules used to interpret an integer constant: after the optional sign, a leading 0 indicates octal conversion, a leading 0x or 0X indicates hexadecimal conversion, and any other combination of leading characters indicates decimal conversion.


Return Values

x The converted value.
0 Indicates that the string starts with an unrecognized character or that the value for base is invalid. If the string starts with an unrecognized character, * endptr is set to nptr.
__UINT64_MAX Indicates that the converted value would cause an overflow.

strxfrm

Changes a string such that the changed string can be passed to the strcmp function, and produce the same result as passing the unchanged string to the strcoll function.

Format

#include <string.h>

size_t strxfrm (char *s1, const char *s2, size_t maxchar);


Arguments

s1, s2

Pointers to character strings.

maxchar

The maximum number of bytes (including the null terminator) to be stored in s1.

Description

The strxfrm function transforms the string pointed to by s2, and stores the resulting string in the array pointed to by s1. No more than maxchar bytes, including the null terminator, are placed into the array pointed to by s1.

If the value of maxchar is less than the required size to store the transformed string (including the terminating null), the contents of the array pointed to by s1 is indeterminate. In such a case, the function returns the size of the transformed string.

If maxchar is 0, then s1 is allowed to be a NULL pointer, and the function returns the required size of the s1 array before making the transformation.

The string comparison functions, strcoll and strcmp , can produce different results given the same two strings to compare. The reason for this is that strcmp does a straightforward comparison of the code point values of the characters in the strings, whereas strcoll uses the locale information to do the comparison. Depending on the locale, the strcoll comparison can be a multipass operation, which is slower than strcmp .

The purpose of the strxfrm function is to transform strings in such a way that if you pass two transformed strings to the strcmp function, the result is the same as passing the two original strings to the strcoll function. The strxfrm function is useful in applications that need to do a large number of comparisons on the same strings using strcoll . In this case, it might be more efficient (depending on the locale) to transform the strings once using strxfrm , and then do comparisons using strcmp .


Return Value

x Length of the resulting string pointed to by s1, not including the terminating null character.

No return value is reserved for error indication. However, the function can set errno to EINVAL -- The string pointed to by s2 contains characters outside the domain of the collating sequence.


Example


/* This program verifies that two transformed strings when      */ 
/* passed through strxfrm and then compared, provide the same   */ 
/* result as if passed through strcoll without any              */ 
/* transformation.  
 
#include <string.h>                                        
#include <stdio.h> 
#include <stdlib.h> 
#include <locale.h> 
 
#define  BUFF_SIZE  256 
 
main() 
{ 
   char string1[BUFF_SIZE]; 
   char string2[BUFF_SIZE]; 
   int errno; 
   int coll_result; 
   int strcmp_result; 
   size_t strxfrm_result1; 
   size_t strxfrm_result2; 
 
   /* setlocale to French locale */ 
 
   if (setlocale(LC_ALL, "fr_FR.ISO8859-1") == NULL) { 
       perror("setlocale"); 
       exit(EXIT_FAILURE); 
   } 
 
   /* collate string 1 and string 2 and store the result */ 
 
   errno = 0; 
   coll_result = strcoll("<a`>bcd", "abcz"); 
   if (errno) { 
       perror("strcoll"); 
       exit(EXIT_FAILURE); 
   } 
 
   else { 
       /* Transform the strings (using strxfrm) into string1   */ 
       /* and string2                                          */ 
 
       strxfrm_result1 = strxfrm(string1, "<a`>bcd", BUFF_SIZE); 
 
       if (strxfrm_result1 == ((size_t) - 1)) { 
           perror("strxfrm"); 
           exit(EXIT_FAILURE); 
       } 
 
       else if (strxfrm_result1 > BUFF_SIZE) { 
           perror("\n** String is too long **\n"); 
           exit(EXIT_FAILURE); 
       } 
 
       else { 
           strxfrm_result2 = strxfrm(string2, "abcz", BUFF_SIZE); 
           if (strxfrm_result2 == ((size_t) - 1)) { 
               perror("strxfrm"); 
               exit(EXIT_FAILURE); 
           } 
 
           else if (strxfrm_result2 > BUFF_SIZE) { 
               perror("\n** String is too long **\n"); 
               exit(EXIT_FAILURE); 
           } 
 
           /* Compare the two transformed strings and verify   */ 
           /* that the result is the same as the result from   */ 
           /* strcoll on the original strings                  */ 
           else { 
               strcmp_result = strcmp(string1, string2); 
               if (strcmp_result == 0 && (coll_result == 0)) { 
                   printf("\nReturn value from strcoll() and " 
                     "return value from strcmp() are both zero."); 
                  printf("\nThe program was successful\n\n"); 
               } 
 
               else if ((strcmp_result < 0) && (coll_result < 0)) { 
                   printf("\nReturn value from strcoll() and " 
                "return value from strcmp() are less than zero."); 
                   printf("\nThe program successful\n\n"); 
               } 
 
               else if ((strcmp_result > 0) && (coll_result > 0)) { 
                   printf("\nReturn value from strcoll() and " 
             "return value from strcmp() are greater than zero."); 
                   printf("\nThe program was successful\n\n"); 
               } 
 
               else { 
               printf("** Error **\n"); 
               printf("\nReturn values are not of the same type"); 
               } 
            } 
        } 
   } 
} 

Running the example program produces the following result:


Return value from strcoll() and return value 
               from strcmp() are less than zero. 
The program was successful 


Previous Next Contents Index