DEC C
Run-Time Library Reference Manual for OpenVMS Systems


Previous Contents Index

The format string consists of zero or more directives. A directive is composed of one of the following:

The strptime function uses fields in the LC_TIME category of the program's current locale to provide a value.

Table REF-9 strptime Conversion Specifications
Specification Replaced by
%a The weekday name. This is either the abbreviated or the full name.
%A Same as %a
%b The month name. This is either the abbreviated or the full name.
%B Same as %b.
%c The date and time using the locale's date format
%Ec The locale's alternative date and time representation
%C The century number (the year divided by 100 and truncated to an integer) as a decimal number (00 -- 99). Leading zeros are permitted.
%EC The name of the base year (period) in the locale's alternative representation
%d The day of the month as a decimal number (01 -- 31). Leading zeros are permitted.
%Od The day of the month using the locale's alternative numeric symbols
%D Same as %m/%d/%y
%e Same as %d
%Oe The date of the month using the locale's alternative numeric symbols
%h Same as %b
%H The hour (24-hour clock) as a decimal number (00 -- 23). Leading zeros are permitted.
%OH The hour (24-hour clock) using the locale's alternative numeric symbols
%I The hour (12-hour clock) as a decimal number (01 -- 12). Leading zeros are permitted.
%OI The hour (12-hour clock) using the locale's alternative numeric symbols
%j The day of the year as a decimal number (001 -- 366)
%m The month as a decimal number (01 -- 12). Leading zeros are permitted.
%Om The month using the locale's alternative numeric symbols
%M The minute as a decimal number (00 -- 59). Leading zeros are permitted.
%OM The minutes using the locale's alternative numeric symbols
%n Any whitespace character
%p The locale's equivalent of the AM/PM designations associated with a 12-hour clock
%r The time in AM/PM notation (%I:%M:%S %p)
%R The time in 24-hour notation (%H:%M)
%S The second as a decimal number (00 -- 61). Leading zeros are permitted.
%OS The seconds using the locale's alternative numeric symbols
%t Any whitespace character
%T The time (%H:%M:%S)
%U The week number of the year (the first Sunday as the first day of week 1) as a decimal number (00 -- 53). Leading zeros are permitted.
%OU The week number of the year (Sunday as the first day of the week) using the locale's alternative numeric symbols
%w The weekday as a decimal number (0 [Sunday] -- 6). Leading zeros are permitted.
%Ow The weekday as a number (Sunday=0) using the locale's alternative numeric symbols
%W The week number of the year (the first Monday as the first day of week 1) as a decimal number (00 -- 53). Leading zeros are permitted.
%OW The week number of the year (Monday as the first day of the week) using the locale's alternative numeric symbols
%x The locale's appropriate date representation
%Ex The locale's alternative date representation
%EX The locale's alternative time representation
%X The locale's appropriate time representation
%y The year without century as a decimal number (00 -- 99)
%Ey The offset from the base year (%EC) in the locale's alternative representation
%Oy The year without the century using the locale's alternative numeric symbols
%Y The year with century as a decimal number
%EY The locale's full alternative year representation
%% %


Return Values

x A pointer to the character following the last character parsed.
NULL Indicates that an error occurred. The contents of the tm structure are undefined.

Example


#include <string.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <time.h> 
#include <locale.h> 
#include <errno.h> 
 
#define NUM_OF_DATES  7 
#define BUF_SIZE 256 
 
/* 
 * This program takes a number of date and time strings and converts them 
 * into tm structs using strptime().  These tm structs are then passed to 
 * strftime() which will reverse the process.  The resulting strings are then 
 * compared with the originals and if a difference is found then an error 
 * is displayed. 
 */ 
 
main() 
{ 
    int         count, i; 
    char        buffer[BUF_SIZE]; 
    char        *ret_val; 
    struct tm   time_struct; 
    char        dates[NUM_OF_DATES][BUF_SIZE] = { 
                        "Thursday 01 January 1970 00:08:20", 
                        "Tuesday 29 February 1972 08:26:40", 
                        "Tuesday 31 December 1991 23:59:59", 
                        "Wednesday 01 January 1992 00:00:00", 
                        "Sunday 03 May 1992 13:33:20", 
                        "Monday 04 May 1992 17:20:00", 
                        "Friday 15 May 1992 03:20:00"}; 
 
    for (i=0; i < NUM_OF_DATES; i++) { 
        /* Convert to a tm structure */ 
        ret_val = strptime(dates[i], "%A %d %B %Y %T", &time_struct); 
 
        /* Check the return value */ 
        if (ret_val == (char *)NULL) { 
            perror("strptime"); 
            exit(EXIT_FAILURE); 
        } 
 
        /* Convert the time structure back to a formatted string */ 
        count = strftime(buffer, BUF_SIZE, "%A %d %B %Y %T", &time_struct); 
 
        /* Check the return value */ 
        if (count == 0) { 
            perror("strftime"); 
            exit(EXIT_FAILURE); 
        } 
 
        /* Check the result */ 
        if (strcmp(buffer, dates[i]) != 0) { 
            printf("Error: Converted string differs from the original\n"); 
        } 
        else { 
            printf("Successfully converted <%s>\n", dates[i]); 
        } 
    } 
} 

Running the example program produces the following result:


Successfully converted <Thursday 01 January 1970 00:08:20> 
Successfully converted <Tuesday 29 February 1972 08:26:40> 
Successfully converted <Tuesday 31 December 1991 23:59:59> 
Successfully converted <Wednesday 01 January 1992 00:00:00> 
Successfully converted <Sunday 03 May 1992 13:33:20> 
Successfully converted <Monday 04 May 1992 17:20:00> 
Successfully converted <Friday 15 May 1992 03:20:00> 


strrchr

Returns the address of the last occurrence of a given character in a null-terminated string. The terminating null character is considered to be part of the string.

Format

#include <string.h>

char *strrchr (const char *str, int character);

Function Variants This function also has variants named _strrchr32 and _strrchr64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.

ARGUMENTS

str

A pointer to a null-terminated character string.

character

An object of type int .

DESCRIPTION

See also strchr in this section.

Return Values

x The address of the last occurrence of the specified character.
NULL Indicates that the character does not occur in the string.

strsep

Separates strings.

Format

#include <string.h>

char *strsep (char **stringp, char *delim);

Function Variants This function also has variants named _strsep32 and _strsep64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.

ARGUMENTS

stringp

A pointer to a pointer to a character string.

delim

A pointer to a string containing characters to be used as delimeters.

DESCRIPTION

This function locates in stringp, the first occurrence of any character in delim (or the terminating '\0' character) and replaces it with a '\0'. The location of the next character after the delimiter character (or NULL, if the end of the string is reached) is stored in the stringp argument. The original value of the stringp argument is returned.

You can detect an "empty" field; one caused by two adjacent delimiter characters, by comparing the location referenced by the pointer returned in the stringp argument to '\0'.

The stringp argument is initially NULL, strsep returns NULL.


Return Values

NULL Indicates success.

Example

The following example uses strsep to parse a string, containing token delimited by white space, into an argument vector:


    char **ap, **argv[10], *inputstring; 
 
    for (ap = argv; (*ap = strsep(&inputstring, " \t")) != NULL;) 
 if (**ap != '\0') 
     ++ap; 


strspn

Returns the length of the prefix of a string that consists entirely of characters from a set of characters.

Format

#include <string.h>

size_t strspn (const char *str, const char *charset);


ARGUMENTS

str

A pointer to a character string. If this string is a null string, 0 is returned.

charset

A pointer to a character string containing the characters for which the function will search.

DESCRIPTION

This function scans the characters in the string, stops when it encounters a character not found in charset, and returns the length of the string's initial segment formed by characters found in charset.

Return Value

x The length of the segment.

strstr

Locates the first occurrence in the string pointed to by s1 of the sequence of characters in the string pointed to by s2.

Format

#include <string.h>

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

Function Variants This function also has variants named _strstr32 and _strstr64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.

ARGUMENTS

s1, s2

Pointers to character strings.

Return Values

Pointer A pointer to the located string.
NULL Indicates that the string was not found.

Example


#include <stdio.h> 
#include <string.h> 
 
main() 
    { 
    static char lookin[]="that this is a test was at the end"; 
    
    putchar('\n'); 
    printf("String: %s\n", &lookin[0] ); 
    putchar('\n'); 
    printf("Addr: %s\n", &lookin[0] ); 
    printf("this: %s\n", strstr( &lookin[0] ,"this") ); 
    printf("that: %s\n", strstr( &lookin[0] , "that" ) ); 
    printf("NULL: %s\n", strstr( &lookin[0], "" ) ); 
    printf("was: %s\n", strstr( &lookin[0], "was" ) ); 
    printf("at: %s\n", strstr( &lookin[0], "at" ) ); 
    printf("the end: %s\n", strstr( &lookin[0], "the end") ); 
    putchar('\n'); 
    
    exit(); 
    }; 
 

This example produces the following results:


$ RUN STRSTR_EXAMPLE
String: that this is a test was at the end
Addr: that this is a test was at the end
this: this is a test was at the end
that: that this is a test was at the end
NULL: that this is a test was at the end
was: was at the end
at: at this is a test was at the end
the end: the end
$ 


strtod

Converts a given string to a double-precision number.

Format

#include <stdlib.h>

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

Function Variants This function also has variants named _strtod32 and _strtod64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 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

This 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 Indicates overflow. errno is set to ERANGE.

strtok

Locates text tokens in a given string. The text tokens are delimited by one or more characters from a separator string that you specify. This 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.

Format

#include <string.h>

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

Function Variants This function also has variants named _strtok32 and _strtok64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 for more information on using pointer-size-specific functions.

ARGUMENTS

s1

On the first call, a pointer to a string containing 0 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.

DESCRIPTION

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.


Return Values

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

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 This function also has variants named _strtol32 and _strtol64 for use with 32-bit and 64-bit pointer sizes, respectively. See Section 1.8 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

This 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.


Previous Next Contents Index