Previous | Table of Contents | Next

Page 1030

DESCRIPTION

The strcmp() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

The strncmp() function is similar, except it only compares the first n characters of s1.

RETURN VALUE

The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.

CONFORMS TO

SVID 3, POSIX, BSD 4.3, ISO 9899

SEE ALSO

bcmp(3), memcmp(3), strcasecmp(3), strncasecmp(3), strcoll(3)

11 April 1993

strcoll

strcoll—Compares two strings using the current locale

SYNOPSIS

#include <string.h>
int strcoll(const char *s1, const char *s2);

DESCRIPTION

The strcoll() function compares the two strings s1 and s2. It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2. The comparison is based on strings interpreted as appropriate for the program's current locale for category LC_COLLATE. (See setlocale(3)).

RETURN VALUE

The strcoll() function returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2, when both are interpreted as appropriate for the current locale.

CONFORMS TO

SVID 3, BSD 4.3, ISO 9899

NOTES

The Linux C Library currently hasn't implemented the complete POSIX-collating.


In the POSIX or C locales, strcoll() is equivalent to strcmp().

SEE ALSO

bcmp(3), memcmp(3), strcasecmp(3), strcmp(3), strxfrm(3), setlocale(3)

GNU, 12 April 1993

strcpy, strncpy

strcpy, strncpy—Copy a string

Page 1031

SYNOPSIS

#include <string.h>
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);

DESCRIPTION

The strcpy() function copies the string pointed to be src (including the terminating \0 character) to the array pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy.

The strncpy() function is similar, except that not more than n bytes of src are copied. Thus, if there is no null byte among the first n bytes of src, the result will not be null-terminated.

In the case where the length of src is less than that of n, the remainder of dest will be padded with nulls.

RETURN VALUE

The strcpy() and strncpy() functions return a pointer to the destination string dest.

CONFORMS TO

SVID 3, POSIX, BSD 4.3, ISO 9899

SEE ALSO

bcopy(3), memccpy(3), memcpy(3), memmove(3)

GNU, 11 April 1993

strdup

strdup—Duplicates a string

SYNOPSIS

#include <string.h>
char *strdup(const char *s);

DESCRIPTION

The strdup() function returns a pointer to a new string that is a duplicate of the string s. Memory for the new string is obtained with malloc(3), and can be freed with free(3).

RETURN VALUE

The strdup() function returns a pointer to the duplicated string, or NULL if insufficient memory was available.

ERRORS

ENOMEM Insufficient memory available to allocate duplicate string

CONFORMS TO

SVID 3, BSD 4.3

SEE ALSO

calloc(3), malloc(3), realloc(3), free(3)

GNU, 12 April 1993

Page 1032

strerror

strerror—Returns string describing error code

SYNOPSIS

#include <string.h>
char *strerror(int errnum);

DESCRIPTION

The strerror() function returns a string describing the error code passed in the argument errnum. The string can only be used until the next call to strerror().

RETURN VALUE

The strerror() function returns the appropriate description string, or an unknown error message if the error code is unknown.

CONFORMS TO

SVID 3, POSIX, BSD 4.3, ISO 9899

SEE ALSO

errno(3), perror(3), strsignal(3)

GNU, 13 April 1993

strfry

strfry—Randomizes a string

SYNOPSIS

#include <string.h>
char *strfry(char *string);

DESCRIPTION

The strfry() function randomizes the contents of string by using rand(3) to randomly swap characters in the string. The result is an anagram of string.

RETURN VALUE

The strfry() function returns a pointer to the randomized string.

CONFORMS TO

The strfry() function is unique to the Linux C Library and GNU C Library.

SEE ALSO

memfrob(3)

GNU, 12 April 1993

strftime

strftime—Formats date and time

Page 1033

SYNOPSIS

#include <time.h>
size t strftime(char *s, size_t max, const char *format,
const struct tm *tm);

DESCRIPTION

The strftime() function formats the broken-down time tm according to the format specification format and places the result in the character array s of size max.

Ordinary characters placed in the format string are copied to s without conversion. Conversion specifiers are introduced by a % character, and are replaced in s as follows:

%a The abbreviated weekday name according to the current locale
%A The full weekday name according to the current locale
%b The abbreviated month name according to the current locale
%B The full month name according to the current locale
%c The preferred date and time representation for the current locale
%d The day of the month as a decimal number (range 01 to 31)
%H The hour as a decimal number using a 24-hour clock (range 00 to 23)
%I The hour as a decimal number using a 12-hour clock (range 01 to 12)
%j The day of the year as a decimal number (range 001 to 366)
%m The month as a decimal number (range 01 to 12)
%M The minute as a decimal number
%p Either a.m. or p.m. according to the given time value, or the corresponding strings for the current locale
%S The second as a decimal number
%U The week number of the current year as a decimal number, starting with the first Sunday as the first day of the first week
%W The week number of the current year as a decimal number, starting with the first Monday as the first day of the first week
%w The day of the week as a decimal, Sunday being 0
%x The preferred date representation for the current locale without the time
%X The preferred time representation for the current locale without the date
%y The year as a decimal number without a century (range 00 to 99)
%Y The year as a decimal number including the century
%Z The time zone or name or abbreviation
%% A literal % character

The broken-down time structure tm is defined in <time.h> as follows:

struct tm
{
int tm sec; /* seconds */
int tm min; /* minutes */
int tm hour; /* hours */
int tm mday; /* day of the month */
int tm mon; /* month */
int tm year; /* year */
int tm wday; /* day of the week */
int tm yday; /* day in the year */
int tm isdst; /* daylight saving time */
};

Page 1034

The members of the tm structure are

tm_sec The number of seconds after the minute, normally in the range 0 to 59, but can be up to 61 to allow for leap seconds.
tm_min The number of minutes after the hour, in the range 0 to 59.
tm_hour The number of hours past midnight, in the range 0 to 23.
tm_mday The day of the month, in the range 1 to 31.
tm_mon The number of months since January, in the range 0 to 11.
tm_year The number of years since 1900.
tm_wday The number of days since Sunday, in the range 0 to 6.
tm_yday The number of days since January 1, in the range 0 to 365.
tm_isdst A flag that indicates whether daylight saving time is in effect at the time described. The value is positive if daylight saving time is in effect, zero if it is not, and negative if the information is not available.

RETURN VALUE

The strftime() function returns the number of characters placed in the array s, not including the terminating NULL character. If the value equals max, it means that the array was too small.

CONFORMS TO

SVID 3, POSIX, BSD 4.3, ISO 9899

SEE ALSO

date(1), time(2), ctime(3), setlocale(3), sprintf(3)

NOTES

The function supports only those locales specified in locale(7)

GNU, 2 July 1993

strcasecmp, strcat, strchr, strcmp, strcoll, strcpy, strcspn,
strdup, strfry, strlen, strncat, strncmp,
strncpy, strncasecmp, strpbrk, strrchr, strsep, strspn, strstr, strtok, strxfrm,
index, rindex

strcasecmp, strcat, strchr, strcmp, strcoll, strcpy, strcspn, strdup, strfry, strlen, strncat, strncmp, strncpy, strncasecmp, strpbrk, strrchr, strsep, strspn, strstr, strtok, strxfrm, index, rindex—String operations

SYNOPSIS

#include <string.h>
int strcasecmp(const char *s1, const char *s2);
char *strcat(char *dest, const char *src);
char *strchr(const char *s,int c);
int strcmp(const char *s1, const char *s2);
int strcoll(const char *s1, const char *s2);
char *strcpy(char *dest, const char *src);
size_t strcspn(const char *s, const char *reject);
char *strdup(const char *s);

Page 1035

char *strfry(char *string);
size_t strlen(const char *s);
char *strncat(char *dest, const char *src, size_t n);
int strncmp(const char *s1, const char *s2, size_t n);
char *strncpy(char *dest, const char *src, size_t n);
int strncasecmp(const char *s1, const char *s2, size_t n);
char *strpbrk(const char *s, const char *accept);
char *strrchr(const char *s,int c);
char *strsep(char **stringp, const char *delim);
size_t strspn(const char *s, const char *accept);
char *strstr(const char *haystack, const char *needle);
char *strtok(char *s, const char *delim);
size_t strxfrm(char *dest, const char *src, size_t n);
char *index(constchar*"s,int c);
char *rindex(const char *s,int c);

DESCRIPTION

The string functions perform string operations on NULL-terminated strings. See the individual man pages for descriptions of each function.

SEE ALSO

index(3), rindex(3), strcasecmp(3), strcat(3), strchr(3), strcmp(3), strcoll(3), strcpy(3), strcspn(3), strdup(3), strfry(3), strlen(3), strncat(3), strncmp(3), strncpy(3), strncasecmp(3), strpbrk(3), strrchr(3), strsep(3), strspn(3), strstr(3), strtok(3), strxfrm(3)

9 April 1993

strlen

strlen—Calculates the length of a string


SYNOPSIS

#include <string.h>
size_t strlen(const char *s);

DESCRIPTION

The strlen() function calculates the length of the string s, not including the terminating \0 character.

RETURN VALUE

The strlen() function returns the number of characters in s.

CONFORMS TO

SVID 3, POSIX, BSD 4.3, ISO 9899

SEE ALSO

string(3)

12 April 1993

strpbrk

strpbrk—Searches a string for any of a set of characters

Page 1036

SYNOPSIS

#include <string.h>
char *strpbrk(const char *s, const char *accept);

DESCRIPTION

The strpbrk() function locates the first occurrence in the string s of any of the characters in the string accept.

RETURN VALUE

The strpbrk() function returns a pointer to the character in s that matches one of the characters in accept.

CONFORMS TO

SVID 3, POSIX, BSD 4.3, ISO 9899

SEE ALSO

index(3), memchr(3), rindex(3), strchr(3), strsep(3), strspn(3), strstr(3), strtok(3)

12 April 1993

strptime

strptime—Converts a string representation of time to a time tm structure

SYNOPSIS

#include <time.h>
char *strptime(char *buf, const char *format, const struct tm *tm);

DESCRIPTION

strptime() is the complementary function to strftime() and converts the character string pointed to by buf to a time value, which is stored in the tm structure pointed to by tm, using the format specified by format. format is a character string that consists of field descriptors and text characters, reminiscent of scanf(3). Each field descriptor consists of a % character followed by another character that specifies the replacement for the field descriptor. All other characters are copied from format into the result. The following field descriptors are supported:

%% Same as %.
%a, %A Day of week, using locale's weekday names; either the abbreviated or full name may be specified.
%b, %B, %h Month, using locale's month names; either the abbreviated or full name may be specified.
%c Date and time as %x, %X.
%C Date and time, in locale's long-format date and time representation.
%d, %e Day of month (1_31; leading zeroes are permitted but not required).
%D Date as %m/%d/%y.
%H, %k Hour (0_23; leading zeroes are permitted but not required).
%I, %l Hour (0_12; leading zeroes are permitted but not required).
%j Day number of year (001_366).
%m Month number (1_12; leading zeroes are permitted but not required).
%M Minute (0_59; leading zeroes are permitted but not required).
%p Locale's equivalent of a.m. or p.m.
%r Time as %I:%M:%S %p.

Page 1037

%R Time as %H:%M.
%S Seconds (0_61; leading zeroes are permitted but not required; extra second allowed for leap years).
%T Time as %H:%M:%S.
%w Weekday number (0_6) with Sunday as the first day of the week.
%x Date, using locale's date format.
%X Time, using locale's time format.
%y Year within century (0_99; leading zeroes are permitted but not required. Unfortunately, this makes the assumption that we are stuck in the 20th century, as 1900 is automatically added onto this number for the tm year field.)
%Y Year, including century (for example, 1988).

Case is ignored when matching items such as month or weekday names.

The broken-down time structure tm is defined in <time.h> as follows:

struct tm
{
   int tm_sec; /* seconds */
   int tm_min; /* minutes */
   int tm_hour; /* hours */
   int tm_mday; /* day of the month */
   int tm_mon; /* month */
   int tm_year; /* year */
   int tm_wday; /* day of the week */
   int tm_yday; /* day in the year */
   int tm_isdst; /* daylight saving time */
};

RETURN VALUE

The strptime() function returns a pointer to the character following the last character in the string pointed to by buf.

SEE ALSO

strftime(3), time(2), setlocale(3), scanf(3)

BUGS

The return values point to static data, whose contents are overwritten by each call.

NOTES

This function is only available in libraries newer than version 4.6.5.

The function supports only those locales specified in locale(7).

GNU, 26 September 1994


strsep

strsep—Extracts token from string

SYNOPSIS

#include <string.h>
char *strsep(char **stringp, const char *delim);

Page 1038

DESCRIPTION

The strsep() function returns the next token from the string stringp which is delimited by delim. The token is terminated with a \0 character and stringp is updated to point past the token.

RETURN VALUE

The strsep() function returns a pointer to the token, or NULL if delim is not found in stringp.

CONFORMS TO

BSD 4.3

SEE ALSO

index(3), memchr(3), rindex(3), strchr(3), strpbrk(3), strspn(3), strstr(3), strtok(3)

GNU, 12 April 1993

strsignal

strsignal—Returns string describing signal

SYNOPSIS

#include <string.h>
char *strsignal(int sig);
extern const char * const sys_siglist[]

DESCRIPTION

The strsignal() function returns a string describing the signal number passed in the argument sig. The string can only be used until the next call to strsignal().

The array sys_siglist holds the signal description strings indexed by signal number.

RETURN VALUE

The strsignal() function returns the appropriate description string, or an unknown signal message if the signal number is invalid.

SEE ALSO

psignal(3), strerror(3)

GNU, 13 April 1993

strspn, strcspn

strspn, strcspn—Search a string for a set of characters

SYNOPSIS

#include <string.h>
size t strspn(const char *s, const char *accept);
size t strcspn(const char *s, const char *reject);

DESCRIPTION

The strspn() function calculates the length of the initial segment of s, which consists entirely of characters in accept.

The strcspn() function calculates the length of the initial segment of s, which consists entirely of characters not in reject.

Previous | Table of Contents | Next

1