Part IV • Documenting the Differences
sqrt()
Header: |
math.h |
Syntax: |
double sqrt(double dValue); |
Description: |
Computes the square root of dValue. |
Parameters: |
dValue—Value for which square root is desired. |
Returns: |
Square root of dValue. |
Example: |
double dSquareRoot = sqrZXt(2); |
|
/* dSquareRoot will be 1.41 */ |
Note: |
The argument must not be negative. |
srand()
Header: |
stdlib.h |
Syntax: |
void srand(unsigned int nSeed); |
Description: |
Seeds (sets the starting point) of the random number generator. |
Parameters: |
nSeed—A seed value. |
Returns: |
No return value. |
Example: |
srand((unsigned)time(NULL)); |
Note: |
The sequence of numbers returned by rand() is identical if |
|
identical seeds are used. Using the time() function assures a |
|
reasonably random starting point. |
sscanf()
Header: |
stdio.h |
|
|
Syntax: |
int sscanf(const |
char * |
szInput, |
|
const |
char * |
szFormat, ...); |
Description: |
Reads from the buffer pointed to by szInput, formatted input. |
chChar or
Part IV • Documenting the Differences
Description: Searches for the first occurrence of chChar in szString.
Parameters: szString—Pointer to the string to be searched. chChar—Character to search for.
Returns: Pointer to the first occurrence of NULL if it is not found.
Example: char szString[100] =
{“Now is the time for all good men”};
printf(“Not the time %s”, strchr(szString, ‘f’));
/* Will print Not the time for all good men */
Note: See memchr().
strcmp()
Header: string.h
Syntax: int strcmp(const char * szString1, const char * szString2);
Description: Compares two strings and returns a value indicating if they are equal or if one is less than the other.
Parameters: szString1—The first string to compare.
szString2—The second string to compare.
Returns: Zero if they are equal, < 0 if szString1 is less than szString2, or > 0 if szString1 is greater than szString2.
|
|
|
|
|
|
Example: |
char |
szString1[] = |
|
{“Now is |
all |
the |
time for all good men”}; |
|
char |
szString2[] = |
|
{“Now is |
not |
the |
time for all good men”}; |
if (strcmp(szString1, szString2) == 0)
{
printf(“‘%s’ ‘%s’ are equal”, szString1,
ANSI C’s Library Function
szString2);
}
/* Will not print since the strings are not equal */
Note: See memchr(). Notice that memcmp() doesn’t assume the string is a NULL terminated character string.
strcoll( )
Header: string.h
Syntax: int strcoll(const char * szString1, const char * szString2);
Description: Compares two strings using the collating sequence selected by setlocale() and returns a value indicating whether they are equal or if one is less than the other.
Parameters: szString1—The first string to compare.
szString2—The second string to compare.
Returns: Zero if they are equal, < 0 if szString1 is less than szString2, or > 0 if szString1 is greater than szString2.
|
|
|
|
|
|
Example: |
char |
szString1[] = |
|
{“Now is |
all |
the |
time for all good men”}; |
|
char |
szString2[] = |
|
{“Now is |
not |
the |
time for all good men”}; |
if (strcoll(szString1, szString2) == 0)
{
printf(“‘%s’ ‘%s’ are equal”, szString1,
szString2);
}
/* Will not print since the strings are not equal */
Note: See memchr(). Notice that memcmp() doesn’t assume the string is a NULLterminated characterstring. This function is equal to strcmp() when the default collating sequence specified by locale “C” is used.
Part IV • Documenting the Differences
strcpy( )
Header: |
string.h |
Syntax: |
char * strcpy(char * szDestination, |
|
const char * szSource); |
Description: |
Copies the string to szDestination that is pointed to by szSource. |
Parameters: |
szDestination—A string that has szSource copied to it. |
|
szSource—The string copied to szDestination. |
Returns: |
Pointer szDestination. |
Example: |
char szString[100] = {“Now is the time”}; |
|
strcpy(szString, “ for all good men”); |
|
/* szString will be for all good men */ |
Note: |
Be sure the destination is large enough to hold the resultant string. |
strcspn( )
Header: |
string.h |
|
Syntax: |
size_t strcspn(const char * szString, |
|
|
const char * szChars); |
Description: |
Returns the length of the initial string that does not contain any |
|
characters found in szChars. |
Parameters: |
szString—Pointer to a string to be searched. |
|
szChars—String containing characters to be searched for. |
Returns: |
Length of the initial string that contains no characters from |
|
szChars, up to the length of szString. |
Example: |
char |
szString[100] = |
|
{“Now is the time for all good men.”}; |
|
int |
nCount = strcspn(szString, “fzx”); |
printf(“Never a good time %s”, &szString[nCount]);
|
ANSI C’s Library Function |
C C C |
|
|
14C |
|
|
C C C |
|
/* will print Never a good time for all good men. |
C C |
|
|
|
*/ |
|
Note: |
Also see strspn(). |
|
strerror( ) |
|
|
Header: |
string.h |
|
Syntax: |
char * strerror(int nError); |
|
Description: |
Returns a pointer to a string describing the error contained in |
|
nError. |
|
Parameters: |
nError—Error value (usually from errno). |
|
Returns: |
Pointer to an error message or a message indicating a message |
|
doesn’t exist for this error. |
|
Example: |
printf(“Had an error: %s\n”, |
|
|
strerror(ENOMEM)); |
|
Note: |
See errno and the header file errno.h. |
|
strftime( ) |
|
|
Header: |
time.h |
|
Syntax: |
size_t strftime(char * szBuffer, size_t |
|
|
nBufferSize, const char * |
|
|
szFormat, const struct tm * Time); |
|
Description: |
Prints to szBuffer the time contained in Time according to the |
|
format specified in szFormat (see Table 14.7 for the format |
|
characters). |
|
Parameters: |
szBuffer—Pointer to the destination buffer that will receive the |
formatted time string.
nBufferSize—Size of szBuffer.
szFormat—Pointer format string.
Time—Pointer to a tm time structure.
Part IV • Documenting the Differences
Returns: The number of characters placed in szBuffer or NULL if an error occurs.
Example: time_t OurTime;
OurTime = time(NULL);
strftime(szBuffer, sizeof(szBuffer), “Today is %A %B %d, %Y”,
localtime(&OurTime)); printf(“%s\n”, szBufer);
/* Will print Today is Friday June 26, 1992 */
Note: See the format characters in Table 14.7. This function makes the creation of attractive time displays easy.
Table 14.7. Function strftime()’s format codes.
Format |
Description |
%a |
Abbreviated weekday name. |
%A |
Full weekday name. |
%b |
Abbreviated month name. |
%B |
Full month name. |
%c |
Full date and time (like ctime()) representation |
|
appropriate for the locale. |
Numeric day of the month.
Hour in 24–hour format (00–23).
Hour in 12–hour format (01–12).
Day of the year (001–366).
Month (01–12).
Minute (00–59).
AM/PM indicator for a 12–hour clock.
Seconds (00–61).
Part IV • Documenting the Differences
strncat()
Header: |
string.h |
Syntax: |
char * strncat(char * szDestination, const char * |
|
szSource, size_t nCount); |
Description: |
Concatenates nCount characters of string pointed to by szSource |
|
to szDestination. |
Parameters: |
szDestination—String that will have szSource appended to it. |
|
szSource—String to append to szDestination. |
|
nCount—Number of characters from szSource to append. |
Returns: |
Pointer szDestination. |
Example: |
char szString[100] = {“Now is the time”}; |
|
strncat(szString, “ for all good men”, 15); |
|
/* szString will be Now is the time for all good m |
|
*/ |
Note: |
Be sure the destination is large enough to hold the resultant string, |
|
and that it has been properly initialized. The destination can be a |
|
string of zero length. |
strncmp()
Header: |
string.h |
Syntax: |
int strncmp(const char * szString1, const char * |
|
szString2, size_t nCount); |
Description: |
Compares up to nCount characters of the two strings and returns |
|
a value indicating whether they are equal or if one is less than the |
|
other. |
Parameters: |
szString1—The first string to compare. |
|
szString2—The second string to compare. |
|
nCount—Number of characters to compare. |
ANSI C’s Library Function |
C C C |
|
14C |
|
C C C |
|
C C |
Returns: Zero if they are equal, < 0 if szString1 is less than szString2, or > 0 if szString1 is greater than szString2.
|
|
|
|
|
|
Example: |
char |
szString1[] = |
|
{“Now is |
all |
the |
time for all good men”}; |
|
char |
szString2[] = |
|
{“Now is |
all |
the |
time for all Bad men”}; |
if (strncmp(szString1, szString2, 20) == 0)
{
printf(“‘%s’ ‘%s’ are equal”, szString1,
szString2);
}
/* Will print since the strings are equal for the first 20 characters*/
Note: See strcmp().
strncpy()
Header: string.h
Syntax: char * strncpy(char * szDestination, const char * szSource, size_t nCount);
Description: Copies to szDestination up to nCount characters from the string pointed to by szSource.
Parameters: szDestination—String that will have szSource copied to it.
szSource—String copied to szDestination.
nCount—Number of characters to copy.
Returns: Pointer szDestination.
Example: char szString[100] = {“Now is the time”}; strncpy(szString, “ for all good men”, 10);
/* szString will be for all g */
Note: |
Be sure the destination is large enough to hold the resultant string. |