The acos() function implements the converse of cos() , i.e.
it is passed a value in the range -1 to +1, and returns an angle
in radians whose cosine is equal to that value.
Example
#include
#include
/* Print acos() values for -1 to 1 in degrees. */
void
main (void)
{
float i, a;
for(i = -1.0; i < 1.0 ; i += 0.1) {
a = acos(i)*180.0/3.141592;
printf("acos(%f) = %f degrees\n", i, a);
}
}
See Also
The asctime() function takes the time broken down into the
structtm structure, pointed to by its argument, and returns a
26 character string describing the current date and time in the
format: Sun Sep 16 01:03:52 197300 Note the newline at the end
of the string. The width of each field in the string is fixed.
The example gets the current time, converts it to a structtm
pointer with localtime() , it then converts this to ASCII and
prints it. The time() function will need to be provided by the
user (see time() for details).
Example
#include
#include
void
main (void)
{
time_t clock;
struct tm * tp;
time(&clock);
tp = localtime(&clock);
printf("%s", asctime(tp));
}
See Also
The example will require the user to provide the time() routine
as it cannot be supplied with the compiler. See time() for more
details.
Data Types
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
IndexASIN
Synopsis
#include
double asin (double f)
Description
The asin() function implements the converse of sin() , i.e.
it is passed a value in the range -1 to +1, and returns an angle
in radians whose sine is equal to that value.
Example
#include
#include
void
main (void)
{
float i, a;
for(i = -1.0; i < 1.0 ; i += 0.1) {
a = asin(i)*180.0/3.141592;
printf("asin(%f) = %f degrees\n", i, a);
}
}
See Also
The arc tangent of y/x in the range -p to +p radians. If both
y and x are zero, a domain error occurs and zero is returned.
IndexATOF
Synopsis
#include
double atof (const char * s)
Description
The atof() function scans the character string passed to it,
skipping leading blanks. It then converts an ASCII representation
of a number to a double. The number may be in decimal, normal
floating point or scientific notation.
Example
#include
#include
void
main (void)
{
char buf[80];
double i;
gets(buf);
i = atof(buf);
printf("Read %s: converted to %f\n", buf, i);
}
See Also
A double precision floating point number. If no number is found
in the string, 0.0 will be returned.
IndexATOI
Synopsis
#include
int atoi (const char * s)
Description
The atoi() function scans the character string passed to it,
skipping leading blanks and reading an optional sign. It then
converts an ASCII representation of a decimal number to an integer.
Example
#include
#include
void
main (void)
{
char buf[80];
int i;
gets(buf);
i = atoi(buf);
printf("Read %s: converted to %d\n", buf, i);
}
See Also
A signed integer. If no number is found in the string, 0 will be
returned.
IndexATOL
Synopsis
#include
long atol (const char * s)
Description
The atol() function scans the character string passed to it,
skipping leading blanks. It then converts an ASCII representation
of a decimal number to a long integer.
Example
#include
#include
void
main (void)
{
char buf[80];
long i;
gets(buf);
i = atol(buf);
printf("Read %s: converted to %ld\n", buf, i);
}
See Also
A long integer. If no number is found in the string, 0 will be
returned.
IndexCEIL
Synopsis
#include
double ceil (double f)
Description
This routine returns the smallest whole number not less than f .
Example
#include
#include
void
main (void)
{
double j;
scanf("%lf", &j);
printf("The ceiling of %lf is %lf\n", j, ceil(j));
}
IndexCOS
Synopsis
#include
double cos (double f)
Description
This function yields the cosine of its argument, which is an angle in radians. The cosine is calculated by expansion of a poly-
nomial series approximation.
Example
#include
#include
#define C 3.141592/180.0
void
main (void)
{
double i;
for(i = 0 ; i <= 180.0 ; i += 10)
printf("sin(%3.0f) = %f, cos = %f\n", i, sin(i*C), cos(i*C));
}
See Also
These functions are the hyperbolic implementations of the
trigonometric functions; cos(), sin() and tan() .
Example
#include
#include
void
main (void)
{
printf("%f\n", cosh(1.5));
printf("%f\n", sinh(1.5));
printf("%f\n", tanh(1.5));
}
Return Value
The function cosh() returns the hyperbolic cosine value.
The function sinh() returns the hyperbolic sine value.
The function tanh() returns the hyperbolic tangent value.
IndexCTIME
Synopsis
#include
char * ctime (time_t * t)
Description
The ctime() function converts the time in seconds pointed to by
its argument to a string of the same form as described for asctime() . Thus the example program prints the
current time and date.
Example
#include
#include
void
main (void)
{
time_t clock;
time(&clock);
printf("%s", ctime(&clock));
}
See Also
ei and di enable and disable interrupts respectively. These are
implemented as macros defined in pic.h . They will expand to an
in-line assembler instruction that sets or clears the interrupt
enable bit.The example shows the use of ei and di around access
to a long variable that is modified during an interrupt. If this
was not done, it would be possible to return an incorrect value,
if the interrupt occurred between accesses to successive words of
the count value.
Example
#include
long count;
void interrupt tick(void)
{
count++;
}
long getticks(void)
{
long val; /* Disable interrupts around access
to count, to ensure consistency.*/
di();
val = count;
ei();
return val;
}
IndexDIV
Synopsis
#include
div_t div (int numer, int demon)
Description
The div() function computes the quotient and remainder of the
numerator divided by the denominator.
Example
#include
#include
void
main (void)
{
div_t x;
x = div(12345, 66);
printf("quotient = %d, remainder = %d\n", x.quot, x.rem);
}
Return Value
Returns the quotient and remainder into the div_t structure.
Data Types
typedef struct
{
int quot;
int rem;
} div_t;
IndexEEPROM_READ, EEPROM_WRITE
These function allow access
to the on-board eeprom (when present). The eeprom is not in the directly-accessible memory space
and a special byte sequence is loaded to the eeprom control registers to access the device.
Writing a value to the eeprom is a
slow process and the eeprom_write() function polls the appropriate registers
to ensure that any previous writes have completed before writing the next piece of data.
Reading data is completed in the one cycle and no polling is necessary to check for
a read completion.
Example
#include
void
main (void)
{
unsigned char data;
unsigned char address;
address = 0x10;
data = eeprom_read(address);
}
Note
It may be necessary to poll the eeprom registers to ensure that
the write has completed if an eeprom_write() call is immediately followed by
an eeprom_read() . The global interrupt enable
bit (GIE) is cleared by the eeprom_write() routine.
IndexEVAL_POLY
The eval_poly() function evaluates a polynomial, whose
coefficients are contained in the array d , at x , for example:
y = x*x*d2 + x*d1 + d0. The order of the polynomial is
passed in n .
Example
#include
#include
void
main (void)
{
double x, y;
double d[3] = {1.1, 3.5, 2.7};
x = 2.2;
y = eval_poly(x, d, 2);
printf("The polynomial evaluated at %f is %f\n", x, y);
}
Return Value
A double value, being the polynomial evaluated at x .
IndexEXP
Synopsis
#include
double exp (double f)
Description
The exp() routine returns the exponential function of its argument, i.e.
e to the power of f .
Example
#include
#include
void
main (void)
{
double f;
for(f = 0.0 ; f <= 5 ; f += 1.0)
printf("e to %1.0f = %f\n", f, exp(f));
}
See Also
#include
double frexp (double f, int * p)
Description
The frexp() function breaks a floating point number into a normalized
fraction and an integral power of 2. The integer is
stored into the int object pointed to by p . Its return value
x is in the interval [0.5, 1.0) or zero, and f equals x
times 2 raised to the power stored in *p . If f is zero, both
parts of the result are zero.
Example
#include
#include
void
main (void)
{
double f;
int i;
f = frexp(23456.34, &i);
printf("23456.34 = %f * 2^%d\n", f, i);
}
See Also
This function returns the 32-bit floating point calibration data
from the PIC 14000 calibration space. Only use this function to
access KREF , KBG , VHTHERM and KTC (that is, the 32-bit
floating point parameters). FOSC and TWDT can be accessed directly as they are bytes.
Example
#include
void
main (void)
{
double x;
unsigned char y;
/* Get the slope reference ratio. */
x = get_cal_data(KREF);
/* Get the WDT time-out. */
y =TWDT;
}
Return Value
The value of the calibration parameter
Note
This function can only be used on the PIC 14000.
IndexGMTIME
This function converts the time pointed to by t which is in
seconds since 00:00:00 on Jan 1, 1970, into a broken down time
stored in a structure as defined in time.h . The structure is
defined in the 'Data Types' section.
Example
#include
#include
void
main (void)
{
time_t clock;
struct tm * tp;
time(&clock);
tp = gmtime(&clock);
printf("It's %d in London\n", tp->tm_year+1900);
}
See Also
The example will require the user to provide the time() routine
as one cannot be supplied with the compiler. See time() for
more detail.
Data Types
typedef long time_t;
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};IndexISALNUM, ISALPHA, ISDIGIT, ISLOWER et. al.
Synopsis
#include
int isalnum (char c)
int isalpha (char c)
int isascii (char c)
int iscntrl (char c)
int isdigit (char c)
int islower (char c)
int isprint (char c)
int isgraph (char c)
int ispunct (char c)
int isspace (char c)
int isupper (char c)
int isxdigit (char c)
Description
These macros, defined in ctype.h , test the supplied character
for membership in one of several overlapping groups of characters. Note that all except
isascii() are defined for c , if isascii(c) is true or if c = EOF . isalnum (c) c is in 0-9 or a-z or A-Z isalpha (c) c is in A-Z or a-z isascii (c) c is a 7 bit ascii character iscntrl (c) c is a control character isdigit (c) c is a decimal digit islower (c) c is in a-z isprint (c) c is a printing char isgraph (c) c is a non-space printable character ispunct (c) c is not alphanumeric isspace (c) c is a space, tab or newline isupper (c) c is in A-Z isxdigit (c) c is in 0-9 or a-f or A-F
Example
#include
#include
void
main (void)
{
char buf[80];
int i;
gets(buf);
i = 0;
while(isalnum(buf[i]))
i++;
buf[i] = 0;
printf("'%s' is the word\n", buf);
}
See Also
Returns one if a character has been pressed on the console keyboard,
zero otherwise. Note: the return value is a bit.
Note
The body of the routine will need to be implemented by the user.
The skeleton function will be found in the sources direstory.
IndexLDEXP
Synopsis
#include
double ldexp (double f, int i)
Description
The ldexp() function performs the inverse of frexp() operation;
the integer i is added to the exponent of the floating
point f and the resultant returned.
Example
#include
#include
void
main (void)
{
double f;
f = ldexp(1.0, 10);
printf("1.0 * 2^10 = %f\n", f);
}
See Also
The return value is the integer i added to the exponent of the
floating point value f .
IndexLDIV
Synopsis
#include
ldiv_t ldiv (long number, long denom)
Description
The ldiv() routine divides the numerator by the denominator,
computing the quotient and the remainder. The sign of the quotient is the same as that of the
mathematical quotient. Its absolute value is the largest integer which is less than the absolute
value of the mathematical quotient.The ldiv() function is
similar to the div() function, the difference being that the
arguments and the members of the returned structure are all of
type longint .
Example
#include
#include
void
main (void)
{
ldiv_t lt;
lt = ldiv(1234567, 12345);
printf("Quotient = %ld, remainder = %ld\n", lt.quot, lt.rem);
}
See Also
The localtime() function converts the time pointed to by t
which is in seconds since 00:00:00 on Jan 1, 1970, into a broken
down time stored in a structure as defined in time.h . The routine localtime()
takes into account the contents of the global
integer time_zone . This should contain the number of minutes
that the local time zone is westward of Greenwich. Since there
is no way under MS-DOS of actually predetermining this value, by
default localtime() will return the same result as gmtime() .
Example
#include
#include
char * wday[] = {
"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"
};
void
main (void)
{
time_t clock;
struct tm * tp;
time(&clock);
tp = localtime(&clock);
printf("Today is %s\n", wday[tp->tm_wday]);
}
See Also
The example will require the user to provide the time() routine
as one cannot be supplied with the compiler. See time() for
more detail.
Data Types
typedef long time_t;
struct tm {
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;
};
IndexLOG, LOG10
#include
/* For baseline and midrange processors */
const void * memchr (const void * block, int val, size_t length)
/* For high-end processors */
void * memchr (const void * block, int val, size_t length)
Description
The memchr() function is similar to strchr() except that
instead of searching null terminated strings, it searches a block
of memory specified by length for a particular byte. Its arguments
are a pointer to the memory to be searched, the value of
the byte to be searched for, and the length of the block. A
pointer to the first occurrence of that byte in the block is returned.
Example
#include
#include
unsigned int ary[] = {1, 5, 0x6789, 0x23};
void
main (void)
{
char * cp;
cp = memchr(ary, 0x89, sizeof ary);
if(!cp)
printf("not found\n");
else
printf("Found at offset %u\n", cp - (char *)ary);
}
See Also
The memcmp() function compares two blocks of memory, of length
n , and returns a signed value similar to strncmp() . Unlike
strncmp() the comparison does not stop on a null character. The
ASCII collating sequence is used for the comparison, but the effect
of including non-ASCII characters in the memory blocks on
the sense of the return value is indeterminate. Testing for
equality is always reliable.
Example
#include
#include
void
main (void)
{
int buf[10], cow[10], i;
buf[0] = 1;
buf[2] = 4;
cow[0] = 1;
cow[2] = 5;
buf[1] = 3;
cow[1] = 3;
i = memcmp(buf, cow, 3*sizeof(int));
if(i < 0)
printf("less than\n");
else if(i > 0)
printf("Greater than\n");
else
printf("Equal\n");
}
See Also
Returns negative one, zero or one, depending on whether s1
points to string which is less than, equal to or greater than the
string pointed to by s2 in the collating sequence.
IndexMEMCPY
Synopsis
#include
/* For baseline and midrange processors */
void * memcpy (void * d, const void * s, size_t n)
/* For high-end processors */
far void * memcpy (far void * d, const void * s, size_t n)
Description
The memcpy() function copies n bytes of memory starting from
the location pointed to by s to the block of memory pointed to
by d . The result of copying overlapping blocks is undefined.
The memcpy() function differs from strcpy() in that it copies
a specified number of bytes, rather than all bytes up to a null
terminator.
Example
#include
#include
void
main (void)
{
char buf[80];
memset(buf, 0, sizeof buf);
memcpy(buf, "a partial string", 10);
printf("buf = '%s'\n", buf);
}
See Also
The memcpy() routine returns its first argument.
IndexMEMMOVE
Synopsis
#include
/* For baseline and midrange processors */
void * memmove (void * s1, const void * s2, size_t n)
/* For high-end processors */
far void * memmove (far void * s1, const void * s2, size_t n)
Description
The memmove() function is similar to the function memcpy()
except copying of overlapping blocks is handled correctly. That
is, it will copy forwards or backwards as appropriate to correctly copy one block to another that overlaps it.
The modf() function splits the argument value into integral
and fractional parts, each having the same sign as value . For
example, -3.17 would be split into the intergral part (-3) and
the fractional part (-0.17).The integral part is stored as a double
in the object pointed to by iptr .
Example
#include
#include
void
main (void)
{
double i_val, f_val;
f_val = modf( -3.17, &i_val);
}
Return Value
The signed fractional part of value .
IndexPERSIST_CHECK, PERSIST_VALIDATE
Synopsis
#include
int persist_check (int flag)
void persist_validate (void)
Description
The persist_check() function is used with non-volatile RAM
variables, declared with the persistent qualifier. It tests the
nvram area, using a magic number stored in a hidden variable by a
previous call to persist_validate() and a checksum also calculated
by persist_validate() . If the magic number and checksum
are correct, it returns true (non-zero). If either are incorrect,
it returns zero. In this case it will optionally zero out and revalidate
the non-volatile RAM area (by calling persist_validate() ). This is done if the flag argument is true.The persist_validate()
routine should be called after each change to a
persistent variable. It will set up the magic number and recalculate the checksum.
Example
#include
#include
persistent long reset_count;
void
main (void)
{
if(!persist_check(1))
printf("Reset count invalid - zeroed\n");
else
printf("Reset number %ld\n", reset_count);
reset_count++; /* update count */
persist_validate(); /* and checksum */
for(;;)
continue; /* sleep until next reset */
}
Return Value
FALSE (zero) if the NV-RAM area is invalid; TRUE (non-zero)
if the NVRAM area is valid.
IndexPOW
The printf() function is a formatted output routine, operating
on stdout . There are corresponding routines operating into a
string buffer ( sprintf() ). The printf() routine is passed a
format string, followed by a list of zero or more arguments. In
the format string are conversion specifications, each of which is
used to print out one of the argument list values.
Each conversion specification is of the form %m.nc where the percent symbol
% introduces a conversion, followed by an optional width specification
m . The n specification is an optional precision
specification (introduced by the dot) and c is a letter specifying
the type of the conversion. Field widths and precision are
only supported on the midrange and high-end processors, with the
precision specification only applicable to %s .
If the character * is used in place of a decimal constant, e.g. in the format
%*d , then one integer argument will be taken from the list to
provide that value. The types of conversion for the Baseline series are: oxXud
Integer conversion - in radices 8, 16, 16, 10 and 10 respectively.
The conversion is signed in the case of d , unsigned otherwise.
The precision value is the total number of digits to print,
and may be used to force leading zeroes. E.g. %8.4x will print
at least 4 hex digits in an 8 wide field. The letter X prints
out hexadecimal numbers using the upper case letters A-F rather
than a-f as would be printed when using x . When the alternate
format is specified, a leading zero will be supplied for the octal format,
and a leading 0x or 0X for the hex format. s
Print a string - the value argument is assumed to be a character
pointer. At most n characters from the string will be printed,
in a field m characters wide. c
The argument is assumed to be a single character and is printed
literally.Any other characters used as conversion specifications
will be printed. Thus %% will produce a single percent
sign.
For the Midrange and High-end series, the types of conversions
are as for the Baseline with the addition of: l
Long integer conversion - Preceding the integer conversion key
letter with an l indicates that the argument list is long. f
Floating point - m is the total width and n is the number of
digits after the decimal point. If n is omitted it defaults to
6. If the precision is zero, the decimal point will be omitted
unless the alternate format is specified.
Example
printf("Total = %4d ", 23)
yields 'Total = 23 '
printf("Size is %lx" , size)
where size is a long, prints size as hexadecimal.
Note that the precision number is only available when using
Midrange
and High-end processors when using the s placeholder.
printf("Name = %.8s", "a1234567890")
yields 'Name = a1234567'
Note that the variable width number is only available when using
Midrange
and High-end processors placeholder.
printf("xx%*d", 3, 4)
yields 'xx 4'
/* vprintf example */
#include
int
error (char * s, ...)
{
va_list ap;
va_start(ap, s);
printf("Error: ");
vprintf(s, ap);
putchar('\n');
va_end(ap);
}
void
main (void)
{
int i;
i = 3;
error("testing 1 2 %d", i);
}
See Also
The printf() routine returns the number of characters written
to stdout .
NB The return value is a char, NOT an int .
Note
Certain features of printf are only available for the midrange
and high-end processors. Read the description for details. Printing
floating point numbers requires that the float to be printed
be no larger than the largest possible long integer.In order to
use long or float formats, the appropriate supplemental library
must be included. See thedescription on the PICC -L option and
the HPDPIC Options/Long formats in printf menu for more details.
IndexRAND
Synopsis
#include
int rand (void)
Description
The rand() function is a pseudo-random number generator.
It returns an integer in the range 0 to 32767, which changes in a
pseudo-random fashion on each call. The algorithm will produce a
deterministic sequence if started from the same point. The starting point
is set using the srand() call. The example shows use
of the time() function to generate a different starting point
for the sequence each time.
Example
#include
#include
#include
void
main (void)
{
time_t toc;
int i;
time(&toc);
srand((int)toc);
for(i = 0 ; i != 10 ; i++)
printf("%d\t", rand());
putchar('\n');
}
See Also
The example will require the user to provide the time() routine
as one cannot be supplied with the compiler. See time() for
more detail.
IndexSIN
Synopsis
#include
double sin (double f)
Description
This function returns the sine function of its argument.
Example
#include
#include
#define C 3.141592/180.0
void
main (void)
{
double i;
for(i = 0 ; i <= 180.0 ; i += 10)
printf("sin(%3.0f) = %f, cos = %f\n", i, sin(i*C), cos(i*C));
}
See Also
The sprintf() function operates in a similar fashion to
printf() , except that instead of placing the converted output on
the stdoutstream , the characters are placed in the buffer at
buf . The resultant string will be null terminated, and the number
of characters in the buffer will be returned.
A domain error occurs if the argument is negative.
IndexSRAND
Synopsis
#include
void srand (unsigned int seed)
Description
The srand() function initializes the random number generator
accessed by rand() with the given seed . This provides a mechanism
for varying the starting point of the pseudo-random sequence
yielded by rand() . On the z80, a good place to get a
truly random seed is from the refresh register. Otherwise timing
a response from the console will do, or just using the system
time.
Example
#include
#include
#include
void
main (void)
{
time_t toc;
int i;
time(&toc);
srand((int)toc);
for(i = 0 ; i != 10 ; i++)
printf("%d\t", rand());
putchar('\n');
}
See Also
#include
/* For baseline and midrange processors */
char * strcat (char * s1, const char * s2)
/* For high-end processors */
far char * strcat (far char * s1, const char * s2)
Description
This function appends (catenates) string s2 to the end of
string s1 . The result will be null terminated. The argument s1
must point to a character array big enough to hold the resultant
string.
Example
#include
#include
void
main (void)
{
char buffer[256];
char * s1, * s2;
strcpy(buffer, "Start of line");
s1 = buffer;
s2 = " ... end of line";
strcat(s1, s2);
printf("Length = %d\n", strlen(buffer));
printf("string = \"%s\"\n", buffer);
}
See Also
#include
/* For baseline and midrange processors */
const char * strchr (const char * s, int c)
const char * strichr (const char * s, int c)
/* For high-end processors */
char * strchr (const char * s, int c)
char * strichr (const char * s, int c)
Description
The strchr() function searches the string s for an occurrence
of the character c . If one is found, a pointer to that character
is returned, otherwise NULL is returned.The strichr()
function is the case-insensitive version of this function.
Example
#include
#include
void
main (void)
{
static char temp[] = "Here it is...";
char c = 's';
if(strchr(temp, c))
printf("Character %c was found in string\n", c);
else
printf("No character was found in string\n");
}
See Also
The strcmp() function compares its two, null terminated,
string arguments and returns a signed integer to indicate
whether s1 is less than, equal to or greater than s2 . The
comparison is done with the standard collating sequence, which is
that of the ASCII character set.The stricmp() function is the
case-insensitive version of this function.
Example
#include
#include
void
main (void)
{
int i;
if((i = strcmp("ABC", "ABc")) < 0)
printf("ABC is less than ABc\n");
else if(i > 0)
printf("ABC is greater than ABc\n");
else
printf("ABC is equal to ABc\n");
}
See Also
A signed integer less than, equal to or greater than zero.
Note
Other C implementations may use a different collating sequence;
the return value is negative, zero or positive, i.e. do not test
explicitly for negative one (-1) or one (1).
IndexSTRCPY
Synopsis
#include
/* For baseline and midrange processors */
char * strcpy (char * s1, const char * s2)
/* For high-end processors */
far char * strcpy (far char * s1, const char * s2)
Description
This function copies a null terminated string s2 to a character
array pointed to by s1 . The destination array must be large
enough to hold the entire string, including the null terminator.
Example
#include
#include
void
main (void)
{
char buffer[256];
char * s1, * s2;
strcpy(buffer, "Start of line");
s1 = buffer;
s2 = " ... end of line";
strcat(s1, s2);
printf("Length = %d\n", strlen(buffer));
printf("string = \"%s\"\n", buffer);
}
See Also
The strcspn() function returns the length of the
initial segment of the string pointed to by s1 which consists of characters
NOT from the string pointed to by s2 .
Example
#include
#include
void
main (void)
{
static char set[] = "xyz";
printf("%d\n", strcspn( "abcdevwxyz", set));
printf("%d\n", strcspn( "xxxbcadefs", set));
printf("%d\n", strcspn( "1234567890", set));
}
See Also
The strlen() function returns the number of characters in the
string s , not including the null terminator.
Example
#include
#include
void
main (void)
{
char buffer[256];
char * s1, * s2;
strcpy(buffer, "Start of line");
s1 = buffer;
s2 = " ... end of line";
strcat(s1, s2);
printf("Length = %d\n", strlen(buffer));
printf("string = \"%s\"\n", buffer);
}
Return Value
The number of characters preceding the null terminator.
IndexSTRNCAT
Synopsis
#include
/* For baseline and midrange processors */
char * strncat (char * s1, const char * s2, size_t n)
/* For high-end processors */
far char * strncat (far char * s1, const char * s2, size_t n)
Description
This function appends (catenates) string s2 to the end of
string s1 . At most n characters will be copied, and the result
will be null terminated. s1 must point to a character array big enough to hold the resultant string.
Example
#include
#include
void
main (void)
{
char buffer[256];
char * s1, * s2;
strcpy(buffer, "Start of line");
s1 = buffer;
s2 = " ... end of line";
strncat(s1, s2, 5);
printf("Length = %d\n", strlen(buffer));
printf("string = \"%s\"\n", buffer);
}
See Also
The strcmp() function compares its two, null terminated,
string arguments, up to a maximum of n characters, and returns
a signed integer to indicate whether >s1s2 . The comparison is done with the standard
collating sequence, which is that of the ASCII character set.The
stricmp() function is the case-insensitive version of this function.
Example
#include
#include
void
main (void)
{
int i;
i = strcmp("abcxyz", "abcxyz");
if(i == 0)
printf("Both strings are equal\n");
else if(i > 0)
printf("String 2 less than string 1\n");
else
printf("String 2 is greater than string 1\n");
}
See Also
A signed integer less than, equal to or greater than zero.
Note
Other C implementations may use a different collating sequence;
the return value is negative, zero or positive, i.e. do not test
explicitly for negative one (-1) or one (1).
IndexSTRNCPY
Synopsis
#include
/* For baseline and midrange processors */
char * strncpy (char * s1, const char * s2, size_t n)
/* For high-end processors */
far char * strncpy (far char * s1, const char * s2, size_t n)
Description
This function copies a null terminated string s2 to a character
array pointed to by s1 . At most n characters are copied. If
string s2 is longer than n then the destination string will
not be null terminated. The destination array must be large
enough to hold the entire string, including the null terminator.
Example
#include
#include
void
main (void)
{
char buffer[256];
char * s1, * s2;
strncpy(buffer, "Start of line", 6);
s1 = buffer;
s2 = " ... end of line";
strcat(s1, s2);
printf("Length = %d\n", strlen(buffer));
printf("string = \"%s\"\n", buffer);
}
See Also
The strpbrk() function returns a pointer to the first
occurrence in string s1 of any character from string s2 , or a null
pointer if no character from s2 exists in s1 .
Example
#include
#include
void
main (void)
{
char * str = "This is a string.";
while(str != NULL) {
printf( "%s\n", str );
str = strpbrk( str+1, "aeiou" );
}
}
Return Value
Pointer to the first matching character, or NULL if no character found.
IndexSTRRCHR, STRRICHR
Synopsis
#include
/* For baseline and midrange processors */
const char * strrchr (char * s, int c)
const char * strrichr (char * s, int c)
/* For high-end processors */
char * strrchr (char * s, int c)
char * strrichr (char * s, int c)
Description
The strrchr() function is similar to the strchr() function,
but searches from the end of the string rather than the beginning, i.e.
it locates the last occurrence of the character c
in the null terminated string s . If successful it returns a
pointer to that occurrence, otherwise it returns NULL .The strrichr() function is the case-insensitive version of this func-
tion.
Example
#include
#include
void
main (void)
{
char * str = "This is a string.";
while(str != NULL) {
printf( "%s\n", str );
str = strrchr( str+1, 's');
}
}
See Also
The strspn() function returns the length of the initial segment
of the string pointed to by s1 which consists entirely of characters
from the string pointed to by s2 .
Example
#include
#include
void
main (void)
{
printf("%d\n", strspn("This is a string", "This"));
printf("%d\n", strspn("This is a string", "this"));
}
See Also
The strstr() function locates the first occurrence of
the sequence of characters in the string pointed to by s2 in the
string pointed to by s1 .The stristr() routine is the case-insensitive version of this function.
Example
#include
#include
void
main (void)
{
printf("%d\n", strstr("This is a string", "str"));
}
Return Value
Pointer to the located string or a null pointer if the string was not found.
IndexSTRTOK
Synopsis
#include
/* For baseline and midrange processors */
char * strtok (char * s1, const char * s2)
/* For high-end processors */
far char * strtok (far char * s1, const char * s2)
Description
A number of calls to strtok() breaks the string s1 (which
consists of a sequence of zero or more text tokens separated by
one or more characters from the separator string s2 ) into its
separate tokens.The first call must have the string s1 . This
call returns a pointer to the first character of the first token,
or NULL if no tokens were found. The inter-token separator
character is overwritten by a null character, which terminates
the current token.For subsequent calls to strtok() , s1 should
be set to a null pointer. These calls start searching from the
end of the last token found, and again return a pointer to the
first character of the next token, or NULL if no further tokens
were found.
Example
#include
#include
void
main (void)
{
char * ptr;
char * buf = "This is a string of words.";
char * sep_tok = ".,?! ";
ptr = strtok(buf, sep_tok);
while(ptr != NULL) {
printf("%s\n", ptr);
ptr = strtok(NULL, sep_tok);
}
}
Return Value
Returns a pointer to the first character of a token, or a null
pointer if no token was found.
Note
The separator string s2 may be different from call to call.
IndexTAN
Synopsis
#include
double tan (double f)
Description
The tan() function calculates the tangent of f .
Example
#include
#include
#define C 3.141592/180.0
void
main (void)
{
double i;
for(i = 0 ; i <= 180.0 ; i += 10)
printf("tan(%3.0f) = %f\n", i, tan(i*C));
}
See Also
This function is not provided as it is dependant on the target
system supplying the current time. This function will be user implemented.
When implemented, this function should return the current time in seconds
since 00:00:00 on Jan 1, 1970. If the argument t is not equal to NULL ,
the same value is stored into the object pointed to by t .
Example
#include
#include
void
main (void)
{
time_t clock;
time(&clock);
printf("%s", ctime(&clock));
}
See Also
This routine when implemented will return the current time in
seconds since 00:00:00 on Jan 1, 1970.
Note
The time() routine is not supplied, if required the user will
have to implement this routine to the specifications outlined above.
IndexTOLOWER, TOUPPER, TOASCII
Synopsis
#include
char toupper (int c)
char tolower (int c)
char toascii (int c)
Description
The toupper() function converts its lower case alphabetic
argument to upper case, the tolower() routine performs the reverse
conversion and the toascii() macro returns a result that is
guaranteed in the range 0-0177. The functions toupper() and
tolower() return their arguments if it is not an alphabetic
character.
Example
#include
#include
#include
void
main (void)
{
char * array1 = "aBcDE";
int i;
for(i=0;i < strlen(array1); ++i) {
printf("%c", tolower(array1[i]));
}
printf("\n");
}
See Also
#include
void va_start (va_list ap, parmN )
type va_arg (ap, type )
void va_end (va_list ap)
Description
These macros are provided to give
access in a portable way to parameters to a function represented in a prototype by the ellipsis
symbol (...), where type and number of arguments supplied to the
function are not known at compile time.The rightmost parameter to
the function (shown as parmN ) plays an important role in these
macros, as it is the starting point for access to further parameters.
In a function taking variable numbers of arguments, a variable of type
va_list should be declared, then the macro
va_start() invoked with that variable and the name of parmN .
This will initialize the variable to allow subsequent calls of
the macro va_arg() to access successive parameters.Each call to
va_arg() requires two arguments; the variable previously defined
and a type name which is the type that the next parameter is expected to
be. Note that any arguments thus accessed will have
been widened by the default conventions to int, unsignedint or
double . For example if a character argument has been passed, it
should be accessed by va_arg(ap, int) since the char will have
been widened to int .An example is given below of a function taking
one integer parameter, followed by a number of other parameters.
In this example the function expects the subsequent parameters
to be pointers to char, but note that the compiler is not
aware of this, and it is the programmers responsibility to ensure
that correct arguments are supplied.
The xtoi() function scans the character string passed to it,
skipping leading blanks reading an optional sign, and converts an
ASCII representation of a hexadecimal number to an integer.
Example
#include
#include
void
main (void)
{
char buf[80];
int i;
gets(buf);
i = xtoi(buf);
printf("Read %s: converted to %x\n", buf, i);
}
See Also
A signed integer. If no number is found in the string, zero will be returned.
IndexConverted to HTML by Philippe Corbes (Email:philippe.corbes@laposte.net) 11/11/2002.