C library reference

Back   |  HI-TECH   |  C-Library   |  #include   |  HPDPIC   |  Editor   |  PICL   |  Support  
A ABS ACOS ASCTIME ASIN ATAN ATAN2 ATOF ATOI ATOL
C CEIL COS COSH CTIME
D DI DIV
E EEPROM_READ EEPROM_WRITE EI EVAL_POLY EXP
F FABS FLOOR FREXP
G GET_CAL_DATA GMTIME
I ISALNUM ISALPHA ISASCII ISCNTRL ISDIGIT ISGRAPH ISLOWER ISPRINT ISPUNCT ISSPACE ISUPPER ISXDIGIT
K KBHIT
L LDEXP LDIV LOCALTIME LOG LOG10
M MEMCHR MEMCMP MEMCPY MEMMOVE MEMSET MODF
P PERSIST_CHECK PERSIST_VALIDATE POW PRINTF
R RAND
S SIN SINH SPRINTF SQRT SRAND STRCAT STRCHR STRCMP STRCPY STRCSPN STRICHR STRICMP STRISTR STRLEN STRNCAT STRNCMP STRNCPY STRNICMP STRPBRK STRRCHR STRRICHR STRSPN STRSTR STRTOK
T TAN TANH TIME TOASCII TOLOWER TOUPPER
V VA_ARG VA_END VA_START
X XTOI

ABS

Synopsis

#include <stdlib.h> int abs (int j)
Description

The abs() function returns the absolute value of j .

Example

#include <stdio.h> #include <stdlib.h> void main (void) { int a = -5; printf("The absolute value of %d is %d\n", a, abs(a)); }
Return Value

The absolute value of j .
Index
ACOS

Synopsis

#include <math.h> double acos (double f)
Description

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 <math.h> #include <stdio.h> /* 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

sin(), cos(), tan(), asin(), atan(), atan2()

Return Value

An angle in radians, in the range 0 to p . Where the argument value is outside the domain -1 to 1, the return value will be zero.
Index
ASCTIME

Synopsis

#include <time.h> char * asctime (struct tm * t)
Description

The asctime() function takes the time broken down into the struct tm 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 struct tm 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 <stdio.h> #include <time.h> void main (void) { time_t clock; struct tm * tp; time(&clock); tp = localtime(&clock); printf("%s", asctime(tp)); }
See Also

ctime(), gmtime(), localtime(), time()

Return Value

A pointer to the string.

Note

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; }; Index
ASIN

Synopsis

#include <math.h> 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 <math.h> #include <stdio.h> 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

sin(), cos(), tan(), acos(), atan(), atan2()

Return Value

An angle in radians, in the range -p /2 to +p /2. Where the argument value is outside the domain -1 to 1, the return value will be zero.
Index
ATAN

Synopsis

#include <math.h> double atan (double x)
Description

This function returns the arc tangent of its argument, i.e. it returns an angle e in the range -p /2 to p /2 such that tan(e) == x .

Example

#include <stdio.h> #include <math.h> void main (void) { printf("%f\n", atan(1.5)); }
See Also

sin(), cos(), tan(), asin(), acos(), atan2()

Return Value

The arc tangent of its argument.
Index
ATAN2

Synopsis

#include <math.h> double atan2 (double y, double x)
Description

This function returns the arc tangent of y/x , using the sign of both arguments to determine the quadrant of the return value.

Example

#include <stdio.h> #include <math.h> void main (void) { printf("%f\n", atan2(1.5, 1)); }
See Also

sin(), cos(), tan(), asin(), acos(), atan()

Return Value

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.
Index
ATOF

Synopsis

#include <stdlib.h> 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 <stdlib.h> #include <stdio.h> void main (void) { char buf[80]; double i; gets(buf); i = atof(buf); printf("Read %s: converted to %f\n", buf, i); }
See Also

atoi(), atol()

Return Value

A double precision floating point number. If no number is found in the string, 0.0 will be returned.
Index
ATOI

Synopsis

#include <stdlib.h> 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 <stdlib.h> #include <stdio.h> void main (void) { char buf[80]; int i; gets(buf); i = atoi(buf); printf("Read %s: converted to %d\n", buf, i); }
See Also

xtoi(), atof(), atol()

Return Value

A signed integer. If no number is found in the string, 0 will be returned.
Index
ATOL

Synopsis

#include <stdlib.h> 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 <stdlib.h> #include <stdio.h> void main (void) { char buf[80]; long i; gets(buf); i = atol(buf); printf("Read %s: converted to %ld\n", buf, i); }
See Also

atoi(), atof()

Return Value

A long integer. If no number is found in the string, 0 will be returned.
Index
CEIL

Synopsis

#include <math.h> double ceil (double f)
Description

This routine returns the smallest whole number not less than f .

Example

#include <stdio.h> #include <math.h> void main (void) { double j; scanf("%lf", &j); printf("The ceiling of %lf is %lf\n", j, ceil(j)); } Index
COS

Synopsis

#include <math.h> 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 <math.h> #include <stdio.h> #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

sin(), tan(), asin(), acos(), atan(), atan2()

Return Value

A double in the range -1 to +1.
Index
COSH, SINH, TANH

Synopsis

#include <math.h> double cosh (double f) double sinh (double f) double tanh (double f)
Description

These functions are the hyperbolic implementations of the trigonometric functions; cos(), sin() and tan() .

Example

#include <stdio.h> #include <math.h> 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.
Index
CTIME

Synopsis

#include <time.h> 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 <stdio.h> #include <time.h> void main (void) { time_t clock; time(&clock); printf("%s", ctime(&clock)); }
See Also

gmtime(), localtime(), asctime(), time()

Return Value

A pointer to the string.

Note

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; Index
DI, EI

Synopsis

#include <pic.h> void ei(void) void di(void)
Description

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 <pic.h> 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; } Index
DIV

Synopsis

#include <stdlib.h> 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 <stdlib.h> #include <stdio.h> 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; Index
EEPROM_READ, EEPROM_WRITE

Synopsis

#include <pic.h> unsigned char eeprom_read (unsigned char addr); void eeprom_write (unsigned char addr, unsigned char value);
Description

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 <pic.h> 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.
Index
EVAL_POLY

Synopsis

#include <math.h> double eval_poly (double x, const double * d, int n)
Description

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 <stdio.h> #include <math.h> 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 .
Index
EXP

Synopsis

#include <math.h> 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 <math.h> #include <stdio.h> 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

log(), log10(), pow()
Index
FABS

Synopsis

#include <math.h> double fabs (double f)
Description

This routine returns the absolute value of its double argument.

Example

#include <stdio.h> #include <math.h> void main (void) { printf("%f %f\n", fabs(1.5), fabs(-1.5)); }
See Also

abs()
Index
FLOOR

Synopsis

#include <math.h> double floor (double f)
Description

This routine returns the largest whole number not greater than f .

Example

#include <stdio.h> #include <math.h> void main (void) { printf("%f\n", floor( 1.5 )); printf("%f\n", floor( -1.5)); } Index
FREXP

Synopsis

#include <math.h> 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 <math.h> #include <stdio.h> void main (void) { double f; int i; f = frexp(23456.34, &i); printf("23456.34 = %f * 2^%d\n", f, i); }
See Also

ldexp()
Index
GET_CAL_DATA

Synopsis

#include <pic.h> double get_cal_data (const unsigned char * code_ptr)
Description

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 <pic.h> 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.
Index
GMTIME

Synopsis

#include <time.h> struct tm * gmtime (time_t * t)
Description

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 <stdio.h> #include <time.h> 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

ctime(), asctime(), time(), localtime()

Return Value

Returns a structure of type tm .

Note

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; }; Index
ISALNUM, ISALPHA, ISDIGIT, ISLOWER et. al.

Synopsis

#include <ctype.h> 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 <ctype.h> #include <stdio.h> 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

toupper(), tolower(), toascii()
Index
KBHIT

Synopsis

#include <conio.h> bit kbhit (void)
Description

This function returns 1 if a character has been pressed on the console keyboard, 0 otherwise. Normally the character would then be read via getch() .

Example

#include <conio.h> void main (void) { int i; while(!kbhit()) { cputs("I'm waiting.."); for(i = 0 ; i != 1000 ; i++) continue; } }
See Also

getch(), getche()

Return Value

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.
Index
LDEXP

Synopsis

#include <math.h> 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 <math.h> #include <stdio.h> void main (void) { double f; f = ldexp(1.0, 10); printf("1.0 * 2^10 = %f\n", f); }
See Also

frexp()

Return Value

The return value is the integer i added to the exponent of the floating point value f .
Index
LDIV

Synopsis

#include <stdlib.h> 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 long int .

Example

#include <stdlib.h> #include <stdio.h> void main (void) { ldiv_t lt; lt = ldiv(1234567, 12345); printf("Quotient = %ld, remainder = %ld\n", lt.quot, lt.rem); }
See Also

div()

Return Value

Returns a structure of type ldiv_t

Data Types

typedef struct { long quot; /* quotient */ long rem; /* remainder */ } ldiv_t; Index
LOCALTIME

Synopsis

#include <time.h> struct tm * localtime (time_t * t)
Description

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 <stdio.h> #include <time.h> 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

ctime(), asctime(), time()

Return Value

Returns a structure of type tm .

Note

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; }; Index
LOG, LOG10

Synopsis

#include <math.h> double log (double f) double log10 (double f)
Description

The log() function returns the natural logarithm of f . The function log10() returns the logarithm to base 10 of f .

Example

#include <math.h> #include <stdio.h> void main (void) { double f; for(f = 1.0 ; f <= 10.0 ; f += 1.0) printf("log(%1.0f) = %f\n", f, log(f)); }
See Also

exp(), pow()

Return Value

Zero if the argument is negative.
Index
MEMCHR

Synopsis

#include <string.h> /* 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 <string.h> #include <stdio.h> 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

strchr()

Return Value

A pointer to the first byte matching the argument if one exists; NULL otherwise.
Index
MEMCMP

Synopsis

#include <string.h> int memcmp (const void * s1, const void * s2, size_t n)
Description

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 <stdio.h> #include <string.h> 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

strncpy(), strncmp(), strchr(), memset(), memchr()

Return Value

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.
Index
MEMCPY

Synopsis

#include <string.h> /* 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 <string.h> #include <stdio.h> void main (void) { char buf[80]; memset(buf, 0, sizeof buf); memcpy(buf, "a partial string", 10); printf("buf = '%s'\n", buf); }
See Also

strncpy(), strncmp(), strchr(), memset()

Return Value

The memcpy() routine returns its first argument.
Index
MEMMOVE

Synopsis

#include <string.h> /* 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.

See Also

strncpy(), strncmp(), strchr(), memcpy()

Return Value

The function memmove() returns its first argument.
Index
MEMSET

Synopsis

#include <string.h> /* For baseline and midrange processors */ void * memset (void * s, int c, size_t n) /* For high-end processors */ far void * memset (far void * s, int c, size_t n)
Description

The memset() function fills n bytes of memory starting at the location pointed to by s with the byte c .

Example

#include <string.h> #include <stdio.h> void main (void) { char abuf[20]; strcpy(abuf, "This is a string"); memset(abuf, 'x', 5); printf("buf = '%s'\n", abuf); }
See Also

strncpy(), strncmp(), strchr(), memcpy(), memchr()
Index
MODF

Synopsis

#include <math.h> double modf (double value, double * iptr)
Description

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 <math.h> #include <stdio.h> void main (void) { double i_val, f_val; f_val = modf( -3.17, &i_val); }
Return Value

The signed fractional part of value .
Index
PERSIST_CHECK, PERSIST_VALIDATE

Synopsis

#include <sys.h> 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 <sys.h> #include <stdio.h> 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.
Index
POW

Synopsis

#include <math.h> double pow (double f, double p)
Description

The pow() function raises its first argument, f , to the power p .

Example

#include <math.h> #include <stdio.h> void main (void) { double f; for(f = 1.0 ; f <= 10.0 ; f += 1.0) printf("pow(2, %1.0f) = %f\n", f, pow(2, f)); }
See Also

log(), log10(), exp()

Return Value

f to the power of p .
Index
PRINTF

Synopsis

#include <stdio.h> unsigned char printf (const char * fmt, ...)
Description

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:
o x X u d
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 <stdio.h> 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

sprintf()

Return Value

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.
Index
RAND

Synopsis

#include <stdlib.h> 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 <stdlib.h> #include <stdio.h> #include <time.h> 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

srand()

Note

The example will require the user to provide the time() routine as one cannot be supplied with the compiler. See time() for more detail.
Index
SIN

Synopsis

#include <math.h> double sin (double f)
Description

This function returns the sine function of its argument.

Example

#include <math.h> #include <stdio.h> #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

cos(), tan(), asin(), acos(), atan(), atan2()

Return Value

Sine vale of f .
Index
SPRINTF

Synopsis

#include <stdio.h> /* For baseline and midrange processors */ unsigned char sprintf (char *buf, const char * fmt, ...) /* For high-end processors */ unsigned char sprintf (far char *buf, const char * fmt, ...)
Description

The sprintf() function operates in a similar fashion to printf() , except that instead of placing the converted output on the stdout stream , 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.

See Also

printf()

Return Value

The sprintf() routine returns the number of characters placed into the buffer. NB: The return value is a char not an int .

Note

For High-end processors the buffer is accessed via a far pointer.
Index
SQRT

Synopsis

#include <math.h> double sqrt (double f)
Description

The function sqrt() , implements a square root routine using Newton's approximation.

Example

#include <math.h> #include <stdio.h> void main (void) { double i; for(i = 0 ; i <= 20.0 ; i += 1.0) printf("square root of %.1f = %f\n", i, sqrt(i)); }
See Also

exp()

Return Value

Returns the value of the square root.

Note

A domain error occurs if the argument is negative.
Index
SRAND

Synopsis

#include <stdlib.h> 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 <stdlib.h> #include <stdio.h> #include <time.h> 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

rand()
Index
STRCAT

Synopsis

#include <string.h> /* 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 <string.h> #include <stdio.h> 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

strcpy(), strcmp(), strncat(), strlen()

Return Value

The value of s1 is returned.
Index
STRCHR, STRICHR

Synopsis

#include <string.h> /* 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 <strings.h> #include <stdio.h> 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

strrchr(), strlen(), strcmp()

Return Value

A pointer to the first match found, or NULL if the character does not exist in the string.

Note

The functions takes an integer argument for the character, only the lower 8 bits of the value are used.
Index
STRCMP, STRICMP

Synopsis

#include <string.h> int strcmp (const char * s1, const char * s2) int stricmp (const char * s1, const char * s2)
Description

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 <string.h> #include <stdio.h> 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

strlen(), strncmp(), strcpy(), strcat()

Return Value

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).
Index
STRCPY

Synopsis

#include <string.h> /* 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 <string.h> #include <stdio.h> 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

strncpy(), strlen(), strcat(), strlen()

Return Value

The destination buffer pointer s1 is returned.
Index
STRCSPN

Synopsis

#include <string.h> size_t strcspn (const char * s1, const char * s2)
Description

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 <stdio.h> #include <string.h> 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

strspn()

Return Value

Returns the length of the segment.
Index
STRLEN

Synopsis

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

The strlen() function returns the number of characters in the string s , not including the null terminator.

Example

#include <string.h> #include <stdio.h> 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.
Index
STRNCAT

Synopsis

#include <string.h> /* 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 <string.h> #include <stdio.h> 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

strcpy(), strcmp(), strcat(), strlen()

Return Value

The value of s1 is returned.
Index
STRNCMP, STRNICMP

Synopsis

#include <string.h> int strncmp (const char * s1, const char * s2, size_t n) int strnicmp (const char * s1, const char * s2, size_t n)
Description

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 <stdio.h> #include <string.h> 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

strlen(), strcmp()STRCMP, strcpy(), strcat()

Return Value

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).
Index
STRNCPY

Synopsis

#include <string.h> /* 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 <string.h> #include <stdio.h> 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

strcpy(), strcat(), strlen(), strcmp()

Return Value

The destination buffer pointer s1 is returned.
Index
STRPBRK

Synopsis

#include <string.h> /* For baseline and midrange processors */ const char * strpbrk (const char * s1, const char * s2) /* For high-end processors */ char * strpbrk (const char * s1, const char * s2)
Description

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 <stdio.h> #include <string.h> 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.
Index
STRRCHR, STRRICHR

Synopsis

#include <string.h> /* 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 <stdio.h> #include <string.h> void main (void) { char * str = "This is a string."; while(str != NULL) { printf( "%s\n", str ); str = strrchr( str+1, 's'); } }
See Also

strchr(), strlen(), strcmp(), strcpy(), strcat()

Return Value

A pointer to the character, or NULL if none is found.
Index
STRSPN

Synopsis

#include <string.h> size_t strspn (const char * s1, const char * s2)
Description

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 <stdio.h> #include <string.h> void main (void) { printf("%d\n", strspn("This is a string", "This")); printf("%d\n", strspn("This is a string", "this")); }
See Also

strcspn()

Return Value

The length of the segment.
Index
STRSTR, STRISTR

Synopsis

#include <string.h> /* For baseline and midrange processors */ const char * strstr (const char * s1, const char * s2) const char * stristr (const char * s1, const char * s2) /* For high-end processors */ char * strstr (const char * s1, const char * s2) char * stristr (const char * s1, const char * s2)
Description

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 <stdio.h> #include <string.h> 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.
Index
STRTOK

Synopsis

#include <string.h> /* 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 <stdio.h> #include <string.h> 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.
Index
TAN

Synopsis

#include <math.h> double tan (double f)
Description

The tan() function calculates the tangent of f .

Example

#include <math.h> #include <stdio.h> #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

sin(), cos(), asin(), acos(), atan(), atan2()

Return Value

The tangent of f .
Index
TIME

Synopsis

#include <time.h> time_t time (time_t * t)
Description

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 <stdio.h> #include <time.h> void main (void) { time_t clock; time(&clock); printf("%s", ctime(&clock)); }
See Also

ctime(), gmtime(), localtime(), asctime()

Return Value

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.
Index
TOLOWER, TOUPPER, TOASCII

Synopsis

#include <ctype.h> 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 <stdio.h> #include <ctype.h> #include <string.h> void main (void) { char * array1 = "aBcDE"; int i; for(i=0;i < strlen(array1); ++i) { printf("%c", tolower(array1[i])); } printf("\n"); }
See Also

islower(), isupper(), isascii(), et. al.
Index
VA_START, VA_ARG, VA_END

Synopsis

#include <stdarg.h> void va_start (va_list ap, <b>parmN</b> ) <b>type</b> va_arg (ap, <b>type</b> ) 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, unsigned int 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.

Example

#include <stdio.h> #include <stdarg.h> void pf (int a, ...) { va_list ap; va_start(ap, a); while(a--) puts(va_arg(ap, char *)); va_end(ap); } void main (void) { pf(3, "Line 1", "line 2", "line 3"); } Index
XTOI

Synopsis

#include <stdlib.h> unsigned xtoi (const char * s)
Description

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 <stdlib.h> #include <stdio.h> void main (void) { char buf[80]; int i; gets(buf); i = xtoi(buf); printf("Read %s: converted to %x\n", buf, i); }
See Also

atoi()

Return Value

A signed integer. If no number is found in the string, zero will be returned.
Index
Converted to HTML by Philippe Corbes (Email:philippe.corbes@laposte.net) 11/11/2002.