XC16 Libraries Reference Guide
XC16 Libraries Reference Guide
Notice to Customers
Important:
All documentation becomes dated and this manual is no exception. Microchip tools and documentation are
constantly evolving to meet customer needs, so some actual dialogs and/or tool descriptions may differ
from those in this document. Please refer to our website (www.microchip.com) to obtain the latest
documentation available.
Documents are identified with a “DS” number. This number is located on the bottom of each page, in front
of the page number. The numbering convention for the DS number is “DSXXXXXA,” where “XXXXX” is the
document number and “A” is the alphabetic revision level of the document.
For the most up-to-date information on development tools, see the MPLAB® IDE online help. Select the
Help menu, and then Help Content to open a list of available online help files.
1. Libraries Overview.................................................................................................................................. 4
1.1. OMF-Specific Libraries and Startup Modules...............................................................................4
1.2. Startup Code................................................................................................................................ 4
1.3. DSP Library.................................................................................................................................. 5
1.4. 16-Bit Peripheral Libraries............................................................................................................5
1.5. Standard C Libraries with Math and Support Functions...............................................................5
1.6. Fixed-Point Math Functions..........................................................................................................5
1.7. Compiler Built-in Functions.......................................................................................................... 5
2. Standard C Libraries............................................................................................................................... 6
2.1. Using the Standard C Libraries.................................................................................................... 6
2.2. Multiple Header Types and Macros..............................................................................................6
2.3. <assert.h> Diagnostics.................................................................................................................7
2.4. <ctype.h> Character Handling......................................................................................................8
2.5. <errno.h> Errors......................................................................................................................... 19
2.6. <float.h> Floating-Point Characteristics..................................................................................... 19
2.7. <iso646.h> Alternate Spellings...................................................................................................25
2.8. <limits.h> Implementation-Defined Limits.................................................................................. 26
2.9. <locale.h> Localization...............................................................................................................26
2.10. <setjmp.h> Non-Local Jumps.....................................................................................................27
2.11. <signal.h> Signal Handling.........................................................................................................28
2.12. <stdarg.h> Variable Argument Lists........................................................................................... 34
2.13. <stddef.h> Common Definitions................................................................................................. 36
2.14. <stdio.h> Input and Output.........................................................................................................36
2.15. <stdlib.h> Utility Functions......................................................................................................... 81
2.16. <string.h> String Functions........................................................................................................ 97
2.17. <time.h> Date and Time Functions...........................................................................................118
4. Support Libraries.................................................................................................................................165
4.1. Rebuilding the libpic30 Library................................................................................................. 165
4.2. Standard C Library Helper Functions....................................................................................... 165
4.3. Standard C Library Functions That Require Modification.........................................................171
4.4. Functions/Constants to Support A Simulated UART................................................................171
4.5. Functions for Erasing and Writing EEDATA Memory............................................................... 175
4.6. Functions for Erasing and Writing Flash Memory.................................................................... 178
4.7. Functions for Specialized Copying and Initialization................................................................ 182
4.8. Functions to Support Slave PRAM Program Memory.............................................................. 189
Trademarks................................................................................................................................................ 226
1. Libraries Overview
®
A library is a collection of functions grouped for reference and ease of linking. See the “MPLAB XC16 Assembler,
Linker and Utilities User’s Guide” (DS50002106) for more information about making and using libraries.
Compiler Installation Locations
The majority of the libraries discussed in this manual come with the MPLAB XC16 C Compiler, which is installed by
default in the following locations:
• Windows OS 32-bit - C:\Program Files\Microchip\xc16\x.xx
• Windows OS 64-bit - C:\Program Files (x86)\Microchip\xc16\x.xx
• Mac OS - Applications/microchip/xc16/x.xx
• Linux OS - /opt/microchip/xc16/x.xx
where x.xx is the version number.
Assembly Code Applications
Free versions of the 16-bit language tool libraries are available from the Microchip web site. DSP and 16-bit
peripheral libraries are provided with object files and source code. A math library (containing functions from the
standard C header file <math.h> ) is provided as an object file only. The complete standard C library is provided with
the MPLAB XC16 C Compiler.
C Code Applications
The 16-bit language tool libraries are included in the lib subdirectory of the MPLAB XC16 C Compiler install
directory (see “Compiler Installation Locations”). These libraries can be linked directly into an application with a 16-bit
linker.
2. Standard C Libraries
Standard ANSI C library functions are contained in the file libc-omf.a, where omf will be elf or coff depending
upon the selected object module format.
Assembly Code Applications
A free version of the math functions library and header file is available from the Microchip web site. No source code is
available with this free version.
C Code Applications
The MPLAB XC16 C Compiler install directory (see “Libraries Overview, Compiler Installation Locations”) contains
the following subdirectories with library-related files:
• lib – standard C library files
• src\libm – source code for math library functions, batch file to rebuild the library
• support\h – header files for libraries
The standard headers can be included in any order. Do not include a standard header within a declaration. Do not
define macros that have the same names as keywords before including a standard header.
A standard header never includes another standard header.
Library Files
The archived library files contain all the individual object files for each library function.
When linking an application, the library file must be provided as an input to the linker (using the --library or -l
linker option) such that the functions used by the application may be linked into the application.
A typical C application will require three library files: libc-omf.a, libm-omf.a, and libpic30-omf.a (see
“Libraries Overview, OMF-Specific Libraries and Startup Modules” for more on OMF-specific libraries). These libraries
will be included automatically if linking is performed using the compiler.
Note: Some standard library functions require a heap. These include the standard I/O functions that open files and
®
the memory allocation functions. See the “MPLAB XC16 Assembler, Linker and Utilities User’s Guide” (DS52106)
®
and “MPLAB XC16 C Compiler User’s Guide” (DS00052071) for more information on the heap.
Include
<locale.h>
<stddef.h>
<stdio.h>
<stdlib.h>
<string.h>
<time.h>
Remarks
The expression evaluates to zero or non-zero. If zero, the assertion fails and a message is printed to stderr. The
message includes the source file name (__FILE__), the source line number (__LINE__), the expression being
evaluated and the message. The macro then calls the function abort(). If the macro _VERBOSE_DEBUGGING is
defined, a message will be printed to stderr each time assert() is called.
Assertion testing may be turned off without removing the code by defining NDEBUG before including <assert.h>. If
the macro NDEBUG is defined, assert() is ignored and no code is generated.
Example
#include <assert.h>
int main(void)
{
int a;
a = 2 * 2;
assert(a == 4); /* if true-nothing prints */
assert(a == 6); /* if false-print message */
/* and abort */
}
Example Output
sampassert.c:8 a == 4 -- OK
sampassert.c:9 a == 6 -- assertion failed
ABRT
Remarks
The expression evaluates to zero or non-zero. If zero, a software breakpoint is triggered and execution will be
suspended. If the target device does not support software breakpoints, a compiler error is triggered.
Breakpoints may be turned off without removing the code by defining NDEBUG before including <assert.h>. If the
macro NDEBUG is defined, __conditional_software_breakpoint() is ignored and no code is generated.
Example
#include <assert.h>
int main(void)
{
int a;
a = 2 * 2;
__conditional_software_breakpoint(a == 4); /* if true-no action */
__conditional_software_breakpoint(a == 6); /* if false-break on this line */
}
Return Value
Returns a non-zero integer value if the character, c, is alphanumeric; otherwise, returns a zero.
Remarks
Alphanumeric characters are included within the ranges A-Z, a-z or 0-9.
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = '3';
if (isalnum(ch))
printf("3 is an alphanumeric\n");
else
printf("3 is NOT an alphanumeric\n");
ch = '#';
if (isalnum(ch))
printf("# is an alphanumeric\n");
else
printf("# is NOT an alphanumeric\n");
}
Example Output
3 is an alphanumeric
# is NOT an alphanumeric
Return Value
Returns a non-zero integer value if the character is alphabetic; otherwise, returns zero.
Remarks
Alphabetic characters are included within the ranges A-Z or a-z.
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = 'B';
if (isalpha(ch))
printf("B is alphabetic\n");
else
printf("B is NOT alphabetic\n");
ch = '#';
if (isalpha(ch))
printf("# is alphabetic\n");
else
printf("# is NOT alphabetic\n");
}
Example Output
B is alphabetic
# is NOT alphabetic
Return Value
Returns a non-zero integer value if the character is a space or tab character; otherwise, returns zero.
Remarks
A character is considered to be a white-space character if it is one of the following: space (' ') or horizontal tab
('\t').
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = '&';
if (isblank(ch))
printf("& is a white-space character\n");
else
printf("& is NOT a white-space character\n");
ch = '\t';
if (isblank(ch))
printf("a tab is a white-space character\n");
else
printf("a tab is NOT a white-space character\n");
}
Example Output
Return Value
Returns a non-zero integer value if the character, c, is a control character; otherwise, returns zero.
Remarks
A character is considered to be a control character if its ASCII value is in the range 0x00 to 0x1F inclusive, or 0x7F.
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
char ch;
ch = 'B';
if (iscntrl(ch))
printf("B is a control character\n");
else
printf("B is NOT a control character\n");
ch = '\t';
if (iscntrl(ch))
printf("A tab is a control character\n");
else
printf("A tab is NOT a control character\n");
}
Example Output
Return Value
Returns a non-zero integer value if the character, c, is a digit; otherwise, returns zero.
Remarks
A character is considered to be a digit character if it is in the range of ‘0’-‘9’.
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = '3';
if (isdigit(ch))
printf("3 is a digit\n");
else
printf("3 is NOT a digit\n");
ch = '#';
if (isdigit(ch))
printf("# is a digit\n");
else
printf("# is NOT a digit\n");
}
Example Output
3 is a digit
# is NOT a digit
Return Value
Returns a non-zero integer value if the character, c, is a graphical character; otherwise, returns zero.
Remarks
A character is considered to be a graphical character if it is any printable character except a space.
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = '3';
if (isgraph(ch))
printf("3 is a graphical character\n");
else
printf("3 is NOT a graphical character\n");
ch = '#';
if (isgraph(ch))
printf("# is a graphical character\n");
else
printf("# is NOT a graphical character\n");
ch = ' ';
if (isgraph(ch))
printf("a space is a graphical character\n");
else
printf("a space is NOT a graphical character\n");
}
Example Output
3 is a graphical character
# is a graphical character
a space is NOT a graphical character
Return Value
Returns a non-zero integer value if the character, c, is a lowercase alphabetic character; otherwise, returns zero.
Remarks
A character is considered to be a lowercase alphabetic character if it is in the range of ‘a’-‘z’.
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = 'B';
if (islower(ch))
printf("B is lowercase\n");
else
printf("B is NOT lowercase\n");
ch = 'b';
if (islower(ch))
printf("b is lowercase\n");
else
printf("b is NOT lowercase\n");
}
Example Output
B is NOT lowercase
b is lowercase
Return Value
Returns a non-zero integer value if the character, c, is printable; otherwise, returns zero.
Remarks
A character is considered to be a printable character if it is in the range 0x20 to 0x7e inclusive.
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = '&';
if (isprint(ch))
printf("& is a printable character\n");
else
printf("& is NOT a printable character\n");
ch = '\t';
if (isprint(ch))
printf("a tab is a printable character\n");
else
printf("a tab is NOT a printable character\n");
}
Example Output
Return Value
Returns a non-zero integer value if the character, c, is a punctuation character; otherwise, returns zero.
Remarks
A character is considered to be a punctuation character if it is a printable character which is neither a space nor an
alphanumeric character. Punctuation characters consist of the following:
!"#$%&'();<=>?@[\]*+,-./:^_{|}~
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = '&';
if (ispunct(ch))
printf("& is a punctuation character\n");
else
printf("& is NOT a punctuation character\n");
ch = '\t';
if (ispunct(ch))
printf("a tab is a punctuation character\n");
else
printf("a tab is NOT a punctuation character\n");
}
Example Output
Return Value
Returns a non-zero integer value if the character, c, is a white-space character; otherwise, returns zero.
Remarks
A character is considered to be a white-space character if it is one of the following: space (' '), form feed ('\f'), newline
('\n'), carriage return ('\r'), horizontal tab ('\t'), or vertical tab ('\v').
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = '&';
if (isspace(ch))
printf("& is a white-space character\n");
else
printf("& is NOT a white-space character\n");
ch = '\t';
if (isspace(ch))
printf("a tab is a white-space character\n");
else
printf("a tab is NOT a white-space character\n");
}
Example Output
Return Value
Returns a non-zero integer value if the character, c, is an uppercase alphabetic character; otherwise, returns zero.
Remarks
A character is considered to be an uppercase alphabetic character if it is in the range of ‘A’-‘Z’.
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = 'B';
if (isupper(ch))
printf("B is uppercase\n");
else
printf("B is NOT uppercase\n");
ch = 'b';
if (isupper(ch))
printf("b is uppercase\n");
else
printf("b is NOT uppercase\n");
}
Example Output
B is uppercase
b is NOT uppercase
Return Value
Returns a non-zero integer value if the character, c, is a hexadecimal digit; otherwise, returns zero.
Remarks
A character is considered to be a hexadecimal digit character if it is in the range of ‘0’-‘9’, ‘A’-‘F’, or ‘a’-‘f’.
Note: The list does not include the leading 0x because 0x is the prefix for a hexadecimal number but is not an actual
hexadecimal digit.
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = 'B';
if (isxdigit(ch))
printf("B is a hexadecimal digit\n");
else
printf("B is NOT a hexadecimal digit\n");
ch = 't';
if (isxdigit(ch))
printf("t is a hexadecimal digit\n");
else
printf("t is NOT a hexadecimal digit\n");
}
Example Output
B is a hexadecimal digit
t is NOT a hexadecimal digit
Return Value
Returns the corresponding lowercase alphabetical character if the argument, c, was originally uppercase; otherwise,
returns the original character.
Remarks
Only uppercase alphabetical characters may be converted to lowercase.
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = 'B';
ch = 'b';
printf("b remains lowercase %c\n",
tolower(ch));
ch = '@';
printf("@ has no lowercase, ");
printf("so %c is returned\n", tolower(ch));
}
Example Output
B changes to lowercase b
b remains lowercase b
@ has no lowercase, so @ is returned
Return Value
Returns the corresponding uppercase alphabetical character if the argument, c, was originally lowercase; otherwise,
returns the original character.
Remarks
Only lowercase alphabetical characters may be converted to uppercase.
Example
#include <ctype.h>
#include <stdio.h>
int main(void)
{
int ch;
ch = 'b';
printf("b changes to uppercase %c\n",
toupper(ch));
ch = 'B';
printf("B remains uppercase %c\n",
toupper(ch));
ch = '@';
printf("@ has no uppercase, ");
printf("so %c is returned\n", toupper(ch));
}
Example Output
b changes to uppercase B
B remains uppercase B
@ has no uppercase, so @ is returned
Value
The value 38 by default, 308 if the switch -fno-short-double is used.
Remarks
By default, a double type is the same size as a float type (32-bit representation). The -fno-short-double
switch allows the IEEE 64-bit representation to be used for a double precision floating-point value.
By default, a double type is the same size as a float type (32-bit representation). The -fno-short-double
switch allows the IEEE 64-bit representation to be used for a double precision floating-point value.
-1 indeterminable
0 toward zero
1 to nearest representable value
2 toward positive infinity
3 toward negative infinity
Include
<float.h>
Value
1
Remarks
• LC_COLLATE
• LC_CTYPE
• LC_MONETARY
• LC_NUMERIC
• LC_TIME
• localeconv
• setlocale
Related Links
2.2 Multiple Header Types and Macros
jmp_buf Type
A type that is an array used by setjmp and longjmp to save and restore the program environment.
Include
<setjmp.h>
Prototype
typedef int jmp_buf[_NSETJMP];
Remarks
_NSETJMP is defined as 16 + 2 that represents 16 registers and a 32-bit return address.
Remarks
The value parameter, val, should be non-zero. If longjmp is invoked from a nested signal handler (that is, invoked
as a result of a signal raised during the handling of another signal), the behavior is undefined.
Example
#include <stdio.h>
#include <setjmp.h>
#include <stdlib.h>
jmp_buf jb;
longjmp(jb, 5);
}
int main (void)
{
int i;
if(i = setjmp(jb)) {
printf("setjmp returned %d\n" i);
exit(0);
}
printf("setjmp returned 0 - good\n");
printf("calling inner...\n");
inner();
printf("inner returned - bad!\n");
}
Example Output
Return Value
If the return is from a direct call, setjmp returns zero. If the return is from a call to longjmp, setjmp returns a non-
zero value.
Note: If the argument val from longjmp is 0, setjmp returns 1.
Example
See longjmp.
SIG_DFL - Used as the second argument and/or the return value for signal to specify that the default handler should
be used for a specific signal.
SIG_ERR - Used as the return value for signal when it cannot complete a request due to an error.
SIG_IGN - Used as the second argument and/or the return value for signal to specify that the signal should be
ignored.
int main(void)
{
raise(SIGABRT);
printf("Program never reaches here.");
}
Example Output
ABRT
int main(void)
{
raise(SIGFPE);
Example Output
FPE
int main(void)
{
raise(SIGILL);
printf("Program never reaches here");
}
Example Output
ILL
int main(void)
{
raise(SIGINT);
Example Output
INT
int main(void)
{
raise(SIGSEGV);
printf("Program never reaches here.");
}
Example Output
SEGV
int main(void)
{
raise(SIGTERM);
Example Output
TERM
Return Value
Returns a 0 if successful; otherwise, returns a non-zero value.
Remarks
raise sends the signal identified by sig to the executing program.
Example
signal(SIGILL, illegalinsn);
x = 7;
y = 0;
z = div(x, y);
printf("Program never reaches here");
}
Example Output
Example Explanation
This example requires the linker script, p30f6014.gld. There are three parts to this example.
First, an interrupt handler is written for the interrupt vector, _MathError, to handle a math error by sending an illegal
instruction, signal (SIGILL), to the executing program. The last statement in the interrupt handler clears the
exception flag.
Second, the function illegalinsn will print an error message and call exit.
Third, in main, signal (SIGILL, illegalinsn) sets the handler for SIGILL to the function illegalinsn.
When a math error occurs due to a divide by zero, the _MathError interrupt vector is called, which in turn will raise
a signal that will call the handler function for SIGILL: the function illegalinsn. Thus, error messages are printed
and the program is terminated.
Return Value
Returns the previous value of func.
Example
int main(void)
{
/* Override default with user defined function */
signal(SIGINT, mysigint);
raise(SIGINT);
Example Output
SIGINT received
SIGILL was ignored
FPE
Example Explanation
The function mysigint is the user-defined signal handler for SIGINT. Inside the main program, the function signal is
called to set up the signal handler (mysigint) for the signal SIGINT that will override the default actions. The
function raise is called to report the signal SIGINT. This causes the signal handler for SIGINT to use the user-
defined function (mysigint) as the signal handler so it prints the “SIGINT received” message.
Next, the function signal is called to set up the signal handler SIG_IGN for the signal SIGILL. The constant
SIG_IGN is used to indicate the signal should be ignored. The function raise is called to report the signal SIGILL
that is ignored.
The function raise is called again to report the signal SIGFPE. Since there is no user-defined function for SIGFPE, the
default signal handler is used so the message “FPE” is printed (which stands for “arithmetic error - terminating”).
Then, the calling program is terminated. The printf statement is never reached.
Return Value
Returns the current argument
Remarks
va_start must be called before va_arg.
Example
#include <stdio.h>
#include <stdarg.h>
va_start(ap, fmt);
while (*fmt)
{
switch (*fmt)
{
case '%':
fmt++;
if (*fmt == 'd')
{
int d = va_arg(ap, int);
printf("<%d> is an integer\n",d);
}
else if (*fmt == 's')
{
char *s = va_arg(ap, char*);
printf("<%s> is a string\n", s);
}
else
{
printf("%%%c is an unknown format\n",
*fmt);
}
fmt++;
break;
default:
printf("%c is unknown\n", *fmt);
fmt++;
break;
}
}
va_end(ap);
}
int main(void)
{
tprint("%d%s.%c", 83, "This is text.", 'a');
}
Example Output
<83> is an integer
<This is text.> is a string
. is unknown
%c is an unknown format
Remarks
After a call to va_end, the argument list pointer, ap, is considered to be invalid. Further calls to va_arg should not
be made until the next va_start. In the 16-bit compiler, va_end does nothing, so this call is not necessary but
should be used for readability and portability.
Example
See va_arg.
Include
<stdarg.h>
Prototype
#define va_start(va_list ap, last_arg)
Arguments
Example
See va_arg.
minus the support for floating-point output. -msmart-io=0 disables this feature and no conversion will take
place. -msmart-io=1 or -msmart-io (the default) will convert a function call if it can be proven that an I/O
function will never be presented with a floating-point conversion. -msmart-io=2 is more optimistic than the
default and will assume that non-constant format strings or otherwise unknown format strings will not contain a
floating-point format. In the event that -msmart-io=2 is used with a floating-point format, the format letter will
appear as literal text and its corresponding argument will not be consumed.
• -fno-short-double will cause the compiler to generate calls to formatted I/O routines that support double as
if it were a long double type.
Mixing modules compiled with these options may result in a larger executable size or incorrect execution if large and
small double-sized data is shared across modules.
Customizing STDIO
The standard I/O relies on helper functions described in “Standard C Libraries - Support Functions.” These functions
include read(), write(), open(), and close() which are called to read, write, open or close handles that are
associated with standard I/O FILE pointers. The sources for these libraries are provided for you to customize as you
wish.
It is recommended that these customized functions be allocated in a named section that begins with .lib. This will
cause the linker to allocate them near to the rest of the library functions, which is where they ought to be.
The simplest way to redirect standard I/O to the peripheral of your choice is to select one of the default handles
already in use. Also, you could open files with a specific name via fopen() by rewriting open() to return a new
handle to be recognized by read() or write() as appropriate.
If only a specific peripheral is required, then you could associate handle 1 == stdout or 2 == stderr to another
peripheral by writing the correct code to talk to the interested peripheral.
A complete generic solution might be:
Single letters were used in this example because they are faster to check and use less memory. However, if memory
is not an issue, you could use strcmp to compare full names.
In write(), you would write:
int __attribute__((__section__(".libc.write")))
write(int handle, void *buffer, unsigned int len) {
int i;
volatile UxMODEBITS *umode = &U1MODEbits;
volatile UxSTABITS *ustatus = &U1STAbits;
volatile unsigned int *txreg = &U1TXREG;
volatile unsigned int *brg = &U1BRG;
switch (handle)
{
default:
case 0:
case 1:
case 2:
if ((_ ___C30_UART != 1) && (&U2BRG)) {
umode = &U2MODEbits;
ustatus = &U2STAbits;
txreg = &U2TXREG;
brg = &U2BRG;
}
if ((umode->UARTEN) == 0)
{
*brg = 0;
umode->UARTEN = 1;
}
if ((ustatus->UTXEN) == 0)
{
ustatus->UTXEN = 1;
}
for (i = len; i; --i)
{
while ((ustatus->TRMT) ==0);
*txreg = *(char*)buffer++;
}
break;
case handle_can1: /* code to support can1 */
break;
case handle_can2: /* code to support can2 */
break;
case handle_spi1: /* code to support spi1 */
break;
case handle_spi2: /* code to support spi2 */
break;
}
return(len);
}
where you would fill in the appropriate code as specified in the comments.
Now you can use the generic C STDIO features to write to another port:
Related Links
2.2 Multiple Header Types and Macros
2.14.2.1 stderr
File pointer to the standard error stream.
Include
<stdio.h>
2.14.2.2 stdin
File pointer to the standard input stream.
Include
<stdio.h>
2.14.2.3 stdout
File pointer to the standard output stream.
Include
<stdio.h>
Remarks
The function clears the end-of-file and error indicators for the given stream (i.e., feof and ferror will return false
after the function clearerr is called).
Example
fclose(myfile);
}
}
Example Output
Error
Error indicator reset
Return Value
Returns 0 if successful; otherwise, returns EOF if any errors were detected.
Remarks
fclose writes any buffered output to the file. This function requires a heap.
Example
int main(void)
{
FILE *myfile1, *myfile2;
int y;
y = fclose(myfile1);
if (y == EOF)
printf("afile1 was not closed\n");
else
printf("afile1 was closed\n");
}
}
Example Output
Prototype
FILE *fdopen(int fildes, const char *mode);
Arguments
Return Value
Returns a pointer to the open stream. If the function fails, a null pointer is returned.
Remarks
The following are types of file access:
w+b or wb+ Opens an empty binary file for reading and writing.
Example Output
Example Explanation
afile1 must exist before it can be opened for reading (r) or the fdopen function will fail.
If the fdopen function opens a file for writing (w+) that doesn’t exist, it will be created and then opened. If the file
does exist, it cannot be overwritten and will be appended.
If the fdopen function attempts to append a file (a+) that doesn’t exist, it will NOT be created and fdopen will fail. If
the file does exist, it will be opened for appending.
Return Value
Returns non-zero if stream is at the end-of-file; otherwise, returns zero.
Example
int main(void)
{
FILE *myfile;
int y = 0;
}
}
Example Input
Contents of afile.txt (used as input):
This is a sentence.
Example Output
This is a sentence.
Return Value
Returns a non-zero value if error indicator is set; otherwise, returns a zero.
Example
fclose(myfile);
}
}
Example Output
Error
Error indicator reset
Return Value
Returns EOF if a write error occurs; otherwise, returns zero for success.
Remarks
If stream is a null pointer, all output buffers are written to files. fflush has no effect on an unbuffered stream.
Return Value
Returns the character read or EOF if a read error occurs or end-of-file is reached.
Remarks
The function reads the next character from the input stream, advances the file-position indicator and returns the
character as an unsigned char converted to an int.
Example
int main(void)
{
FILE *buf;
char y;
printf("%c|", y);
y = fgetc(buf);
}
fclose(buf);
}
}
Example Input
Contents of afile.txt (used as input):
Short
Longer string
Example Output
S|h|o|r|t|
|L|o|n|g|e|r| |s|t|r|i|n|g|
|
Return Value
Returns 0 if successful; otherwise, returns a non-zero value.
Remarks
The function stores the file-position indicator for the given stream in *pos if successful, otherwise, fgetpos sets
errno.
Example
int main(void)
{
FILE *myfile;
fpos_t pos;
char buf[25];
if (fsetpos(myfile, &pos) != 0)
perror("fsetpos error");
Example Output
Return Value
Returns a pointer to the string s if successful; otherwise, returns a null pointer.
Remarks
The function reads characters from the input stream and stores them into the string pointed to by s until it has read
n-1 characters, stores a newline character or sets the end-of-file or error indicators. If any characters were stored, a
null character is stored immediately after the last read character in the next element of the array. If fgets sets the
error indicator, the array contents are indeterminate.
Example
int main(void)
{
FILE *buf;
char s[MAX];
Example Input
Contents of afile.txt (used as input):
Short
Longer string
Example Output
Short
|Longer string
|
Return Value
Returns a pointer to the open stream. If the function fails, a null pointer is returned.
Remarks
The following are types of file access:
w+b or wb+ Opens an empty binary file for reading and writing (an existing file is overwritten.)
a+b or ab+ Opens a binary file for reading and appending (a file is created if it doesn’t exist).
Example Output
Example Explanation
afile1 must exist before it can be opened for reading (r) or the fopen function will fail.
If the fopen function opens a file for writing (w+) it does not have to exist, it will be created and then opened. If the
file does exist, it cannot be overwritten and will be appended.
Return Value
Returns number of characters generated or a negative number if an error occurs.
Remarks
The format argument has the same syntax and use that it has in printf.
Example
int main(void)
{
FILE *myfile;
int y;
char s[]="Print this string";
int x = 1;
char a = '\n';
if ((myfile = fopen("afile", "w")) == NULL)
printf("Cannot open afile\n");
else
{
y = fprintf(myfile, "%s %d time%c", s, x, a);
fclose(myfile);
}
}
Example Output
Number of characters printed to file = 25
Contents of afile:
Print this string 1 time
Related Links
2.14.28 printf Function
c character to be written
stream pointer to the open stream
Return Value
Returns the character written or EOF if a write error occurs.
Remarks
The function writes the character to the output stream, advances the file-position indicator and returns the character
as an unsigned char converted to an int.
Example
int main(void)
{
char *y;
char buf[] = "This is text\n";
int x;
x = 0;
Example Output
s string to be written
stream pointer to the open stream
Return Value
Returns a non-negative value if successful; otherwise, returns EOF.
Remarks
The function writes characters to the output stream up to but not including the null character.
Example
int main(void)
{
char buf[] = "This is text\n";
fputs(buf,stdout);
fputs("|",stdout);
}
Example Output
This is text
|
Return Value
Returns the number of complete elements read up to nelem whose size is specified by size.
Remarks
The function reads characters from a given stream into the buffer pointed to by ptr until the function stores size *
nelem characters or sets the end-of-file or error indicator. fread returns n/size where n is the number of characters
it read. If n is not a multiple of size, the value of the last element is indeterminate. If the function sets the error
indicator, the file-position indicator is indeterminate.
Example
#include <stdio.h> /* for fread, fwrite, printf, fopen, fclose, sizeof, FILE, NULL */
int main(void) {
FILE *buf;
int x, numwrote, numread;
double nums[10], readnums[10];
Example Output
10.0/1 = 10.000000
10.0/2 = 5.000000
10.0/3 = 3.333333
10.0/4 = 2.500000
10.0/5 = 2.000000
10.0/6 = 1.666667
10.0/7 = 1.428571
10.0/8 = 1.250000
10.0/9 = 1.111111
10.0/10 = 1.000000
Wrote 10 numbers
Read 10 numbers
1 * 10.000000 = 10.000000
2 * 5.000000 = 10.000000
3 * 3.333333 = 10.000000
4 * 2.500000 = 10.000000
5 * 2.000000 = 10.000000
6 * 1.666667 = 10.000000
7 * 1.428571 = 10.000000
8 * 1.250000 = 10.000000
9 * 1.111111 = 10.000000
10 * 1.000000 = 10.000000
Example Explanation
This program uses fwrite to save 10 numbers to a file in binary form. This allows the numbers to be saved in the
same pattern of bits as the program is using, which provides more accuracy and consistency. Using fprintf would
save the numbers as text strings, which could cause the numbers to be truncated. Each number is divided into 10 to
produce a variety of numbers. Retrieving the numbers with fread to a new array and multiplying them by the original
number shows the numbers were not truncated in the save process.
Return Value
Returns a pointer to the new open file. If the function fails a null pointer is returned.
Remarks
The function closes the file associated with the stream as though fclose was called. Then it opens the new file as
though fopen was called. freopen will fail if the specified stream is not open. See fopen for the possible types of
file access.
This function requires a heap.
Example
int main(void)
{
FILE *myfile1, *myfile2;
int y;
Example Output
Example Explanation
This program uses myfile2 to point to the stream when freopen is called, so if an error occurs, myfile1 will still
point to the stream and can be closed properly. If the freopen call is successful, myfile2 can be used to close the
stream properly.
Return Value
Returns the number of items successfully converted and assigned. If no items are assigned, a 0 is returned. EOF is
returned if end-of-file is encountered before the first conversion or if an error occurs.
Remarks
The format argument has the same syntax and use that it has in scanf.
Example
int main(void)
{
FILE *myfile;
char s[30];
int x;
char a;
if ((myfile = fopen("afile", "w+")) == NULL)
printf("Cannot open afile\n");
else
{
fprintf(myfile, "%s %d times%c",
"Print this string", 100, '\n');
fclose(myfile);
}
}
Example Input
Contents of afile:
Example Output
Print
this
string
100
times
Return Value
Returns 0 if successful; otherwise, returns a non-zero value and set errno.
Remarks
mode can be one of the following:
int main(void)
{
FILE *myfile;
char s[70];
int y;
myfile = fopen("afile.out", "w+");
if (myfile == NULL)
printf("Cannot open afile.out\n");
else
{
fprintf(myfile, "This is the beginning, "
"this is the middle and "
"this is the end.");
Example Output
Example Explanation
The file afile.out is created with the text, “This is the beginning, this is the middle and this is the end.”
The function fseek uses an offset of zero and SEEK_SET to set the file pointer to the beginning of the file. fgets
then reads 22 characters which are “This is the beginning,” and adds a null character to the string.
Next, fseek uses an offset of two and SEEK_CURRENT to set the file pointer to the current position plus two (skipping
the comma and space). fgets then reads up to the next 70 characters. The first 39 characters are “this is the middle
and this is the end.” It stops when it reads EOF and adds a null character to the string.
Finally, fseek uses an offset of negative 16 characters and SEEK_END to set the file pointer to 16 characters from
the end of the file. fgets then reads up to 70 characters. It stops at the EOF after reading 16 characters “this is the
end,” and adds a null character to the string.
2.14.20.1 SEEK_CUR
Indicates that fseek should seek from the current position of the file pointer.
Include
<stdio.h>
Example
See example for fseek.
2.14.20.2 SEEK_END
Indicates that fseek should seek from the end of the file.
Include
<stdio.h>
Example
See example for fseek.
2.14.20.3 SEEK_SET
Indicates that fseek should seek from the beginning of the file.
Include
<stdio.h>
Example
See example for fseek.
Return Value
Returns 0 if successful; otherwise, returns a non-zero value.
Remarks
The function sets the file-position indicator for the given stream in *pos if successful; otherwise, fsetpos sets
errno.
Example
int main(void)
{
FILE *myfile;
fpos_t pos;
char buf[25];
if ((myfile = fopen("sampfgetpos.c", "rb")) ==
NULL)
printf("Cannot open file\n");
else
{
fread(buf, sizeof(char), 8, myfile);
if (fgetpos(myfile, &pos) != 0)
perror("fgetpos error");
else
{
fread(buf, sizeof(char), 21, myfile);
printf("Bytes read: %.21s\n", buf);
fread(buf, sizeof(char), 18, myfile);
printf("Bytes read: %.18s\n", buf);
}
if (fsetpos(myfile, &pos) != 0)
perror("fsetpos error");
Example Output
Return Value
Returns the position of the file pointer if successful; otherwise, returns -1.
Example
int main(void)
{
FILE *myfile;
char s[75];
long y;
myfile = fopen("afile.out", "w+");
if (myfile == NULL)
printf("Cannot open afile.out\n");
else
{
fprintf(myfile,"This is a very long sentence "
"for input into the file named "
"afile.out for testing.");
fclose(myfile);
if ((myfile = fopen("afile.out", "rb")) != NULL)
{
printf("Read some characters:\n");
fread(s, sizeof(char), 29, myfile);
printf("\t\"%s\"\n", s);
y = ftell(myfile);
printf("The current position of the "
"file pointer is %ld\n", y);
fclose(myfile);
}
}
}
Example Output
Return Value
Returns the number of complete elements successfully written, which will be less than nelem only if a write error is
encountered.
Remarks
The function writes characters to a given stream from a buffer pointed to by ptr up to nelem elements whose size is
specified by size. The file position indicator is advanced by the number of characters successfully written. If the
function sets the error indicator, the file-position indicator is indeterminate.
Example
int main(void)
{
FILE *buf;
int x, numwrote, numread;
double nums[10], readnums[10];
if ((buf = fopen("afile.out", "w+")) != NULL)
{
for (x = 0; x < 10; x++)
{
nums[x] = 10.0/(x + 1);
printf("10.0/%d = %f\n", x+1, nums[x]);
}
}
else
printf("Cannot open afile.out\n");
if ((buf = fopen("afile.out", "r+")) != NULL)
{
numread = fread(readnums, sizeof(double),
10, buf);
printf("Read %d numbers\n", numread);
for (x = 0; x < 10; x++)
{
printf("%d * %f = %f\n", x+1, readnums[x],
(x + 1) * readnums[x]);
}
fclose(buf);
}
else
printf("Cannot open afile.out\n");
}
Example Output
10.0/1 = 10.000000
10.0/2 = 5.000000
10.0/3 = 3.333333
10.0/4 = 2.500000
10.0/5 = 2.000000
10.0/6 = 1.666667
10.0/7 = 1.428571
10.0/8 = 1.250000
10.0/9 = 1.111111
10.0/10 = 1.000000
Wrote 10 numbers
Read 10 numbers
1 * 10.000000 = 10.000000
2 * 5.000000 = 10.000000
3 * 3.333333 = 10.000000
4 * 2.500000 = 10.000000
5 * 2.000000 = 10.000000
6 * 1.666667 = 10.000000
7 * 1.428571 = 10.000000
8 * 1.250000 = 10.000000
9 * 1.111111 = 10.000000
10 * 1.000000 = 10.000000
Example Explanation
This program uses fwrite to save 10 numbers to a file in binary form. This allows the numbers to be saved in the
same pattern of bits as the program is using which provides more accuracy and consistency. Using fprintf would
save the numbers as text strings, which could cause the numbers to be truncated. Each number is divided into 10 to
produce a variety of numbers. Retrieving the numbers with fread to a new array and multiplying them by the original
number shows the numbers were not truncated in the save process.
Return Value
Returns the character read or EOF if a read error occurs or end-of-file is reached.
Remarks
getc is the same as the function fgetc.
Example
int main(void)
{
FILE *buf;
char y;
Example Input
Contents of afile.txt (used as input):
Short
Longer string
Example Output
S|h|o|r|t|
|L|o|n|g|e|r| |s|t|r|i|n|g|
|
#include <stdio.h>
int main(void)
{
char y;
y = getchar();
printf("%c|", y);
y = getchar();
printf("%c|", y);
y = getchar();
printf("%c|", y);
y = getchar();
printf("%c|", y);
y = getchar();
printf("%c|", y);
}
Example Input
Contents of UartIn.txt (used as stdin input for simulator):
Short
Longer string
Example Output
S|h|o|r|t|
Return Value
Returns a pointer to the string s if successful; otherwise, returns a null pointer.
Remarks
The function reads characters from the stream stdin and stores them into the string pointed to by s until it reads a
newline character (which is not stored) or sets the end-of-file or error indicators. If any characters were read, a null
character is stored immediately after the last read character in the next element of the array. If gets sets the error
indicator, the array contents are indeterminate.
Example
#include <stdio.h>
int main(void)
{
char y[50];
gets(y) ;
printf("Text: %s\n", y);
}
Example Input
Contents of UartIn.txt (used as stdin input for simulator):
Short
Longer string
Example Output
Text: Short
s string to print
Return Value
None.
Remarks
The string s is printed followed by a colon and a space. Then, an error message based on errno is printed followed
by an newline.
Example
#include <stdio.h>
int main(void)
{
FILE *myfile;
fclose(myfile);
}
Example Output
Return Value
Returns number of characters generated or a negative number if an error occurs.
Remarks
There must be exactly the same number of arguments as there are format specifiers. If the are less arguments than
match the format specifiers, the output is undefined. If there are more arguments than match the format specifiers,
the remaining arguments are discarded. Each format specifier begins with a percent sign followed by optional fields
and a required type as shown here:
%[flags][width][.precision][size]type
flags
width
Specify the number of characters to generate for the conversion. If the asterisk (*) is used instead of a decimal
number, the next argument (which must be of type int) will be used for the field width. If the result is less than the field
width, pad characters will be used on the left to fill the field. If the result is greater than the field width, the field is
expanded to accommodate the value without padding.
precision
The field width can be followed with dot (.) and a decimal integer representing the precision that specifies one of the
following:
• minimum number of digits to generate on an integer conversion
• number of fraction digits to generate on an e, E, or f conversion
• maximum number of significant digits to generate on a g or G conversion
• maximum number of characters to generate from a C string on an s conversion
If the period appears without the integer, the integer is assumed to be zero. If the asterisk (*) is used instead of a
decimal number, the next argument (which must be of type int) will be used for the precision.
size
h modifier Used with type d, i, o, u, x, X; converts the value to a short int or unsigned short int.
h modifier Used with n; specifies that the pointer points to a short int.
l modifier Used with type d, i, o, u, x, X; converts the value to a long int or unsigned long int.
l modifier Used with n; specifies that the pointer points to a long int.
ll modifier Used with type d, i, o, u, x, X; converts the value to a long long int or unsigned long
long int.
ll modifier Used with n; specifies that the pointer points to a long long int.
ll modifier Used with s; specifies that the string pointer is an __eds__ pointer. This is an MPLAB XC16
extension.
L modifier Used with e, E, f, g, G; converts the value to a long double.
type
d, i signed int.
s string.
p value of a pointer.
n The associated argument shall be an integer pointer into which is placed the number of
characters written so far. No characters are printed.
% A % character is printed.
#include <stdio.h>
int main(void)
{
/* print a character right justified in a 3 */
/* character space. */
printf("%3c\n", 'a');
Example Output
a
-4 | 4
0012
0x1c
1.100000E+20
-3.35
0.02
c character to be written
stream pointer to FILE structure
Return Value
Returns the character or EOF if an error occurs or end-of-file is reached.
Remarks
putc is the same as the function fputc.
Example
#include <stdio.h>
int main(void)
{
char *y;
char buf[] = "This is text\n";
int x;
x = 0;
putc('|', stdout);
}
}
Example Output
c character to be written
Return Value
Returns the character or EOF if an error occurs or end-of-file is reached.
Remarks
Same effect as fputc with stdout as an argument.
Example
#include <stdio.h>
int main(void)
{
char *y;
char buf[] = "This is text\n";
int x;
x = 0;
for (y = buf; (x != EOF) && (*y != '\0'); y++)
x = putchar(*y);
}
Example Output
This is text
s string to be written
Return Value
Returns a non-negative value if successful; otherwise, returns EOF.
Remarks
The function writes characters to the stream stdout. A newline character is appended. The terminating null
character is not written to the stream.
Example
#include <stdio.h>
int main(void)
{
char buf[] = "This is text\n";
puts(buf);
puts("|");
}
Example Output
This is text
Return Value
Returns 0 if successful; otherwise, returns -1.
Remarks
If filename does not exist or is open, remove will fail.
Example
int main(void)
{
if (remove("myfile.txt") != 0)
printf("Cannot remove file");
else
printf("File removed");
}
Example Output
File removed
Prototype
int rename(const char *old, const char *new);
Arguments
Return Value
Return 0 if successful; otherwise, returns a non-zero value.
Remarks
The old name must exist in the current working directory. The new name must not already exist in the current
working directory.
Example
int main(void)
{
if (rename("myfile.txt","newfile.txt") != 0)
printf("Cannot rename file");
else
printf("File renamed");
}
Example Output
File renamed
Remarks
The function calls fseek(stream, 0L, SEEK_SET) and then clears the error indicator for the given stream.
Example
int main(void)
{
FILE *myfile;
char s[] = "cookies";
int x = 10;
fclose(myfile);
}
}
Example Output
I have 10 cookies.
I ate 10 cookies.
Return Value
Returns the number of items successfully converted and assigned. If no items are assigned, a 0 is returned. EOF is
returned if an input failure is encountered before the first.
Remarks
Each format specifier begins with a percent sign followed by optional fields and a required type as shown here:
%[*][width][modifier]type
*
Indicates assignment suppression. This will cause the input field to be skipped and no assignment made.
width
Specify the maximum number of input characters to match for the conversion, not including white space that can be
skipped.
modifier
h modifier Used with type d, i, o, u, x, X; converts the value to a short int or unsigned short int.
h modifier Used with n; specifies that the pointer points to a short int.
l modifier Used with type d, i, o, u, x, X; converts the value to a long int or unsigned long int.
l modifier Used with n; specifies that the pointer points to a long int.
ll modifier Used with type d, i, o, u, x, X; converts the value to a long long int or unsigned long
long int.
ll modifier Used with n; specifies that the pointer points to a long long int.
type
d, i signed int.
s string.
p value of a pointer.
n The associated argument shall be an integer pointer into which is placed the number of
characters written so far. No characters are printed.
% A % character is printed.
Example
For MPLAB X Simulator:
#include <stdio.h>
#include <libpic30.h>
int main(void)
{
int number, items;
char letter;
char color[30], string[30];
float salary;
__attach_input_file("UartIn.txt");
printf("Enter your favorite number, "
"favorite letter, ");
printf("favorite color desired salary "
"and SSN:\n");
items = scanf("%d %c %[A-Za-z] %f %s", &number,
&letter, &color, &salary, &string);
Example Input
Example Output
Remarks
setbuf must be called after fopen but before any other function calls that operate on the stream. If buf is a null
pointer, setbuf calls the function setvbuf(stream, 0, _IONBF, BUFSIZ) for no buffering; otherwise setbuf
calls setvbuf(stream, buf, _IOFBF, BUFSIZ) for full buffering with a buffer of size BUFSIZ. See setvbuf.
This function requires a heap.
Example
int main(void)
{
FILE *myfile1, *myfile2;
char buf[BUFSIZ];
Example Output
2.14.36.1 BUFSIZ
Defines the size of the buffer used.
Include
<stdio.h>
Value
512
Return Value
Returns 0 if successful
Remarks
setvbuf must be called after fopen but before any other function calls that operate on the stream. For mode, use
one of the following:
_IOFBF – for full buffering
_IOLBF – for line buffering
_IONBF – for no buffering
This function requires a heap.
Example
int main(void)
{
FILE *myfile1, *myfile2;
char buf[256];
if ((myfile1 = fopen("afile1", "w+")) != NULL)
{
if (setvbuf(myfile1, NULL, _IONBF, 0) == 0)
printf("myfile1 has no buffering\n");
else
printf("Unable to define buffer stream "
"and/or size\n");
}
fclose(myfile1);
if ((myfile2 = fopen("afile2", "w+")) != NULL)
{
if (setvbuf(myfile2, buf, _IOFBF, sizeof(buf)) ==
0)
printf("myfile2 has a buffer of %d "
"characters\n", sizeof(buf));
else
printf("Unable to define buffer stream "
"and/or size\n");
}
fclose(myfile2);
}
Example Output
2.14.38.1 _IOFBF
Indicates full buffering.
Include
<stdio.h>
2.14.38.2 _IOLBF
Indicates line buffering.
Include
<stdio.h>
2.14.38.3 _IONBF
Indicates no buffering.
Include
<stdio.h>
Return Value
Returns the number of characters stored in s excluding the terminating null character.
Remarks
The format argument has the same syntax and use that it has in printf.
Example
#include <stdio.h>
int main(void)
{
char sbuf[100], s[]="Print this string";
int x = 1, y;
char a = '\n';
Example Output
Related Links
2.14.28 printf Function
Return Value
Returns the number of items successfully converted and assigned. If no items are assigned, a 0 is returned. EOF is
returned if an input error is encountered before the first conversion.
Remarks
The format argument has the same syntax and use that it has in scanf.
Example
#include <stdio.h>
int main(void)
{
char s[] = "5 T green 3000000.00";
int number, items;
char letter;
char color[10];
float salary;
Example Output
int main(void)
{
FILE *mytempfile;
Example Output
Return Value
Returns a pointer to the filename generated and stores the filename in s. If it can not generate a filename, the NULL
pointer is returned.
Remarks
The created filename will not conflict with an existing file name. Use L_tmpnam to define the size of array the
argument of tmpnam points to.
Example
int main(void)
{
char *myfilename;
char mybuf[L_tmpnam];
char *myptr = (char *) &mybuf;
Example Output
2.14.43.1 L_tmpnam
Defines the number of characters for the longest temporary file name created by the function tmpnam.
Include
<stdio.h>
Value
16
Remarks
L_tmpnam is used to define the size of the array used by tmpnam.
2.14.43.2 TMP_MAX
The maximum number of unique file names the function tmpnam can generate.
Include
<stdio.h>
Value
32
Return Value
Returns the pushed character if successful; otherwise, returns EOF.
Remarks
The pushed back character will be returned by a subsequent read on the stream. If more than one character is
pushed back, they will be returned in the reverse order of their pushing. A successful call to a file positioning function
(fseek, fsetpos or rewind) cancels any pushed back characters. Only one character of push back is guaranteed.
Multiple calls to ungetc without an intervening read or file positioning operation may cause a failure.
Example
int main(void)
{
FILE *buf;
char y, c;
Example Input
Contents of afile.txt (used as input):
Short
Longer string
Example Output
Sho2rt
Longe2r st2ring
Return Value
Returns number of characters stored in s excluding the terminating null character.
Remarks
The format argument has the same syntax and use that it has in printf.
To access the variable length argument list, the ap variable must be initialized by the macro va_start and may be
reinitialized by additional calls to va_arg. This must be done before the vasprintf function is called. Invoke
va_end after the function returns. For more details, see stdarg.h.
This function requires a heap.
Example
va_start(ap, fmt);
vasprintf(bufptr, fmt, ap);
va_end(ap);
printf("Error: %s", buf);
}
int main(void)
{
int num = 3;
Example Output
Related Links
2.14.28 printf Function
Return Value
Returns number of characters generated or a negative number if an error occurs.
Remarks
The format argument has the same syntax and use that it has in printf.
To access the variable length argument list, the ap variable must be initialized by the macro va_start and may be
reinitialized by additional calls to va_arg. This must be done before the vfprintf function is called. Invoke va_end
after the function returns. For more details, see stdarg.h.
This function requires a heap.
Example
FILE *myfile;
va_start(ap, fmt);
vfprintf(myfile, fmt, ap);
va_end(ap);
}
int main(void)
{
int num = 3;
Example Output
Contents of afile.txt:
Related Links
2.14.28 printf Function
Return Value
Returns number of characters generated or a negative number if an error occurs.
Remarks
The format argument has the same syntax and use that it has in printf.
To access the variable length argument list, the ap variable must be initialized by the macro va_start and may be
reinitialized by additional calls to va_arg. This must be done before the vprintf function is called. Invoke va_end
after the function returns. For more details, see stdarg.h
Example
va_start(ap, fmt);
printf("Error: ");
vprintf(fmt, ap);
va_end(ap);
}
int main(void)
{
int num = 3;
Example Output
Related Links
2.14.28 printf Function
<stdio.h>
<stdarg.h>
Prototype
int vsprintf(char *s, const char *format, va_list ap);
Arguments
Return Value
Returns number of characters stored in s excluding the terminating null character.
Remarks
The format argument has the same syntax and use that it has in printf.
To access the variable length argument list, the ap variable must be initialized by the macro va_start and may be
reinitialized by additional calls to va_arg. This must be done before the vsprintf function is called. Invoke va_end
after the function returns. For more details, see stdarg.h.
This function requires a heap.
Example
va_start(ap, fmt);
vsprintf(buf, fmt, ap);
va_end(ap);
printf("Error: %s", buf);
}
int main(void)
{
int num = 3;
Example Output
Related Links
2.14.28 printf Function
2.15.1.1 div_t
A type that holds a quotient and remainder of a signed integer division with operands of type int.
Prototype
typedef struct { int quot, rem; } div_t;
Remarks
This is the structure type returned by the function, div.
2.15.1.2 ldiv_t
A type that holds a quotient and remainder of a signed integer division with operands of type long.
Prototype
typedef struct { long quot, rem; } ldiv_t;
Remarks
This is the structure type returned by the function, ldiv.
2.15.1.3 wchar_t
A type that holds a wide character value. In stdlib.h and stddef.h.
2.15.1.4 EXIT_FAILURE
Reports unsuccessful termination.
Remarks
EXIT_FAILURE is a value for the exit function to return an unsuccessful termination status.
Example
See exit for example of use.
2.15.1.5 EXIT_SUCCESS
Reports successful termination.
Remarks
EXIT_SUCCESS is a value for the exit function to return a successful termination status.
Example
See exit for example of use.
2.15.1.6 MB_CUR_MAX
Maximum number of characters in a multibyte character.
Value
2.15.1.7 RAND_MAX
Maximum value capable of being returned by the rand function.
Value
32767
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *myfile;
fclose(myfile);
}
Example Output
i integer value
Return Value
Returns the absolute value of i.
Remarks
A negative number is returned as positive; a positive number is unchanged.
Example
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
i = 12;
printf("The absolute value of %d is %d\n", i, abs(i));
i = -2;
printf("The absolute value of %d is %d\n", i, abs(i));
i = 0;
printf("The absolute value of %d is %d\n", i, abs(i));
}
Example Output
Return Value
Returns a zero if successful; otherwise, returns a non-zero value.
Remarks
For the registered functions to be called, the program must terminate with the exit function call.
Example
#include <stdio.h>
#include <stdlib.h>
void good_msg(void);
void bad_msg(void);
void end_msg(void);
int main(void)
{
int number;
atexit(end_msg);
printf("Enter your favorite number:");
scanf("%d", &number);
printf(" %d\n", number);
if (number == 5)
{
printf("Good Choice\n");
atexit(good_msg);
exit(0);
}
else
{
printf("%d!?\n", number);
atexit(bad_msg);
exit(0);
}
}
void good_msg(void)
{
printf("That's an excellent number\n");
}
void bad_msg(void)
{
printf("That's an awful number\n");
}
void end_msg(void)
{
printf("Now go count something\n");
}
Example Input 1
With contents of UartIn.txt (used as stdin input for simulator):
Example Output 1
Example Input 2
With contents of UartIn.txt (used as stdin input for simulator):
42
Example Output 2
Return Value
Returns the converted value if successful; otherwise, returns 0.
Remarks
The number may consist of the following:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char a[] = " 1.28";
char b[] = "27.835e2";
char c[] = "Number1";
double x;
x = atof(a);
printf("String = \"%s\" float = %f\n", a, x);
x = atof(b);
printf("String = \"%s\" float = %f\n", b, x);
x = atof(c);
printf("String = \"%s\" float = %f\n", c, x);
}
Example Output
s string to be converted
Return Value
Returns the converted integer if successful; otherwise, returns 0.
Remarks
The number may consist of the following:
[whitespace] [sign] digits
Optional whitespace followed by an optional sign, then a sequence of one or more digits. The conversion stops when
the first unrecognized character is reached. The conversion is equivalent to (int) strtol(s,0,10), except it
does no error checking so errno will not be set.
Example
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
x = atoi(a);
printf("String = \"%s\"\tint = %d\n", a, x);
x = atoi(b);
printf("String = \"%s\"\tint = %d\n", b, x);
}
Example Output
s string to be converted
Return Value
Returns the converted long integer if successful; otherwise, returns 0.
Remarks
The number may consist of the following:
[whitespace] [sign] digits
Optional whitespace followed by an optional sign, then a sequence of one or more digits. The conversion stops when
the first unrecognized character is reached. The conversion is equivalent to (int) strtol(s,0,10), except it
does no error checking so errno will not be set.
Example
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char a[] = " -123456";
char b[] = "2Number";
long x;
x = atol(a);
printf("String = \"%s\" int = %ld\n", a, x);
x = atol(b);
printf("String = \"%s\" int = %ld\n", b, x);
}
Example Output
Return Value
Returns a pointer to the object being searched for if found; otherwise, returns NULL.
Remarks
The value returned by the compare function is <0 if ck is less than ce, 0 if ck is equal to ce or >0 if ck is greater
than ce.
In the following example, qsort is used to sort the list before bsearch is called. bsearch requires the list to be
sorted according to the comparison function. This comp uses ascending order.
Example
#include <stdlib.h>
#include <stdio.h>
#define NUM 7
int main(void)
{
int list[NUM] = {35, 47, 63, 25, 93, 16, 52};
int x, y;
int *r;
y = 25;
r = bsearch(&y, list, NUM, sizeof(int), comp);
if (r)
printf("\nThe value %d was found\n", y);
else
printf("\nThe value %d was not found\n", y);
y = 75;
r = bsearch(&y, list, NUM, sizeof(int), comp);
if (r)
Example Output
Sorted List: 16 25 35 47 52 63 93
The value 25 was found
Return Value
Returns a pointer to the allocated space if successful; otherwise, returns a null pointer.
Remarks
Memory returned by calloc is aligned correctly for any size data element and is initialized to zero. This function
requires a heap.
Example
int main(void)
{
int x;
long *i;
}
else
printf("Cannot allocate memory\n");
}
Example Output
i[0] = 0
i[1] = 0
i[2] = 0
i[3] = 0
i[4] = 0
numer numerator
denom denominator
Return Value
Returns the quotient and the remainder.
Remarks
The returned quotient will have the same sign as the numerator divided by the denominator. The sign for the
remainder will be such that the quotient times the denominator plus the remainder will equal the numerator (quot *
denom + rem = numer). Division by zero will invoke the math exception error, which, by default, will cause a
Reset. Write a math error handler to do something else.
Example
#include <stdlib.h>
#include <stdio.h>
void __attribute__((__interrupt__))
_MathError(void)
{
printf("Illegal instruction executed\n");
abort();
}
int main(void)
{
int x, y;
div_t z;
x = 7;
y = 3;
printf("For div(%d, %d)\n", x, y);
z = div(x, y);
printf("The quotient is %d and the "
"remainder is %d\n\n", z.quot, z.rem);
x = 7;
y = -3;
printf("For div(%d, %d)\n", x, y);
z = div(x, y);
printf("The quotient is %d and the "
"remainder is %d\n\n", z.quot, z.rem);
x = -5;
y = 3;
printf("For div(%d, %d)\n", x, y);
z = div(x, y);
printf("The quotient is %d and the "
"remainder is %d\n\n", z.quot, z.rem);
x = 7;
y = 7;
printf("For div(%d, %d)\n", x, y);
z = div(x, y);
printf("The quotient is %d and the "
"remainder is %d\n\n", z.quot, z.rem);
x = 7;
y = 0;
printf("For div(%d, %d)\n", x, y);
z = div(x, y);
printf("The quotient is %d and the "
"remainder is %d\n\n", z.quot, z.rem);
}
Example Output
For div(7, 3)
The quotient is 2 and the remainder is 1
For div(-5, 3)
The quotient is -1 and the remainder is -2
For div(7, 7)
The quotient is 1 and the remainder is 0
For div(7, 0)
Illegal instruction executed
ABRT
Remarks
exit calls any functions registered by atexit in reverse order of registration, flushes buffers, closes stream, closes
any temporary files created with tmpfile and resets the processor. This function is customizable. See pic30-
libs.
Example
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *myfile;
Example Output
Remarks
Frees memory previously allocated with calloc, malloc or realloc. If free is used on space that has already
been deallocated (by a previous call to free or by realloc) or on space not allocated with calloc, malloc or
realloc, the behavior is undefined. This function requires a heap.
Example
int main(void)
{
long *i;
Example Output
Memory allocated
Memory freed
Prototype
char *getenv(const char *name);
Argument
Return Value
Returns a pointer to the value of the environment variable if successful; otherwise, returns a null pointer.
Remarks
This function must be customized to be used as described (see pic30-libs). By default, there are no entries in the
environment list for getenv to find.
Example
int main(void)
{
char *incvar;
incvar = getenv("INCLUDE");
if (incvar != NULL)
printf("INCLUDE environment variable = %s\n",
incvar);
else
printf("Cannot find environment variable "
"INCLUDE ");
}
Example Output
Return Value
Returns the absolute value of i.
Remarks
A negative number is returned as positive; a positive number is unchanged.
Example
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
long i;
i = 123456;
printf("The absolute value of %7ld is %6ld\n",
i, labs(i));
i = -246834;
printf("The absolute value of %7ld is %6ld\n",
i, labs(i));
i = 0;
printf("The absolute value of %7ld is %6ld\n",
i, labs(i));
}
Example Output
numer numerator
denom denominator
Return Value
Returns the quotient and the remainder.
Remarks
The returned quotient will have the same sign as the numerator divided by the denominator. The sign for the
remainder will be such that the quotient times the denominator plus the remainder will equal the numerator (quot *
denom + rem = numer). If the denominator is zero, the behavior is undefined.
Example
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
long x,y;
ldiv_t z;
x = 7;
y = 3;
printf("For ldiv(%ld, %ld)\n", x, y);
z = ldiv(x, y);
printf("The quotient is %ld and the "
"remainder is %ld\n\n", z.quot, z.rem);
x = 7;
y = -3;
printf("For ldiv(%ld, %ld)\n", x, y);
z = ldiv(x, y);
printf("The quotient is %ld and the "
"remainder is %ld\n\n", z.quot, z.rem);
x = -5;
y = 3;
x = 7;
y = 7;
printf("For ldiv(%ld, %ld)\n", x, y);
z = ldiv(x, y);
printf("The quotient is %ld and the "
"remainder is %ld\n\n", z.quot, z.rem);
x = 7;
y = 0;
printf("For ldiv(%ld, %ld)\n", x, y);
z = ldiv(x, y);
printf("The quotient is %ld and the "
"remainder is %ld\n\n",
z.quot, z.rem);
}
Example Output
For ldiv(7, 3)
The quotient is 2 and the remainder is 1
For ldiv(-5, 3)
The quotient is -1 and the remainder is -2
For ldiv(7, 7)
The quotient is 1 and the remainder is 0
For ldiv(7, 0)
The quotient is -1 and the remainder is 7
Example Explanation
In the last example (ldiv(7,0)) the denominator is zero, the behavior is undefined.
Return Value
Returns a pointer to the allocated space if successful; otherwise, returns a null pointer.
Remarks
malloc does not initialize memory it returns. This function requires a heap.
Example
int main(void)
{
long *i;
Example Output
Memory allocated
Memory freed
s command to be executed
Default Behavior
As distributed, this function acts as a stub or placeholder for your function. If s is not NULL, an error message is
written to stdout and the program will reset; otherwise, a value of -1 is returned.
Return Value
Returns the number of characters stored excluding the null character.
Remarks
wcstombs converts n number of multibyte characters unless it encounters a null character first. The 16-bit compiler
does not support multibyte characters with length greater than 1 character.
Return Value
Returns zero if s points to a null character; otherwise, returns 1.
Remarks
The resulting multibyte character is stored at s. The 16-bit compiler does not support multibyte characters with length
greater than 1 character.
Return Value
Returns a pointer to the location of the match if successful; otherwise, returns NULL.
Remarks
memchr stops when it finds the first occurrence of c, or after searching n number of characters.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "What time is it?";
char ch1 = 'i', ch2 = 'y';
char *ptr;
int res;
printf("buf1 : %s\n\n", buf1);
Example Output
i found at position 7
y not found
s1 first buffer
s2 second buffer
Return Value
Returns a positive number if s1 is greater than s2, zero if s1 is equal to s2 or a negative number if s1 is less than
s2.
Remarks
This function compares the first n characters in s1 to the first n characters in s2 and returns a value indicating
whether the buffers are less than, equal to or greater than each other.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "Where is the time?";
char buf2[50] = "Where did they go?";
char buf3[50] = "Why?";
int res;
Example Output
Return Value
Returns dst.
Remarks
memcpy copies n characters from the source buffer src to the destination buffer dst. If the buffers overlap, the
behavior is undefined.
For memcpy_eds, memcpy_packed, memcpy_p2d16 or memcpy_p2d1624, see “Functions for Specialized Copying
and Initialization.”
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "";
char buf2[50] = "Where is the time?";
char buf3[50] = "Why?";
printf("\n");
Example Output
buf1 :
buf2 : Where is the time?
buf3 : Why?
Include
<string.h>
Prototype
void *memmove(void *s1, const void *s2, size_t n);
Arguments
Return Value
Returns a pointer to the destination buffer.
Remarks
If the buffers overlap, the effect is as if the characters are read first from s2, then written to s1, so the buffer is not
corrupted.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "When time marches on";
char buf2[50] = "Where is the time?";
char buf3[50] = "Why?";
printf("\n");
Example Output
Arguments
s buffer
c character to put in buffer
n number of times
Return Value
Returns the buffer with characters written to it.
Remarks
The character c is written to the buffer n times.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[20] = "What time is it?";
char buf2[20] = "";
char ch1 = '?', ch2 = 'y';
char *ptr;
int res;
printf("\n");
printf("memset(\"%s\", \'%c\',10);\n", buf2, ch2);
memset(buf2, ch2, 10);
printf("buf2 after memset: %s\n", buf2);
}
Example Output
memset("", 'y',10);
buf2 after memset: yyyyyyyyyy
Return Value
Returns a pointer to the destination string.
Remarks
This function appends the source string (including the terminating null character) to the end of the destination string.
The initial character of the source string overwrites the null character at the end of the destination string. If the buffers
overlap, the behavior is undefined.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "We're here";
char buf2[50] = "Where is the time?";
strcat(buf1, buf2);
printf("buf1 after strcat of buf2: \n\t%s\n",
buf1);
printf("\t(%d characters)\n", strlen(buf1));
printf("\n");
strcat(buf1, "Why?");
printf("buf1 after strcat of \"Why?\": \n\t%s\n",
buf1);
printf("\t(%d characters)\n", strlen(buf1));
}
Example Output
Return Value
Returns a pointer to the location of the match if successful; otherwise, returns a null pointer.
Remarks
This function searches the string s to find the first occurrence of the character, c.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "What time is it?";
char ch1 = 'm', ch2 = 'y';
char *ptr;
int res;
Example Output
m found at position 8
y not found
s1 first string
s2 second string
Return Value
Returns a positive number if s1 is greater than s2, zero if s1 is equal to s2 or a negative number if s1 is less than
s2.
Remarks
This function compares successive characters from s1 and s2 until they are not equal or the null terminator is
reached.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "Where is the time?";
char buf2[50] = "Where did they go?";
char buf3[50] = "Why?";
int res;
Example Output
s1 first string
s2 second string
Return Value
Using the locale-dependent rules, it returns a positive number if s1 is greater than s2, zero if s1 is equal to s2 or a
negative number if s1 is less than s2.
Remarks
Since alternate locales are not supported, this function is equivalent to strcmp.
Return Value
Returns a pointer to the destination string.
Remarks
All characters of s2 are copied, including the null terminating character. If the strings overlap, the behavior is
undefined.
For strcpy_eds or strcpy_packed, see “Functions for Specialized Copying and Initialization.”
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "We're here";
char buf2[50] = "Where is the time?";
char buf3[50] = "Why?";
strcpy(buf1, buf2);
printf("buf1 after strcpy of buf2: \n\t%s\n\n",
buf1);
strcpy(buf1, buf3);
printf("buf1 after strcpy of buf3: \n\t%s\n",
buf1);
}
Example Output
Return Value
Returns the length of the segment in s1 not containing characters found in s2.
Remarks
This function will determine the number of consecutive characters from the beginning of s1 that are not contained in
s2.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char str1[20] = "hello";
char str2[20] = "aeiou";
char str3[20] = "animal";
char str4[20] = "xyz";
int res;
Example Output
strcspn("hello", "aeiou") = 1
strcspn("animal", "aeiou") = 0
strcspn("animal", "xyz") = 6
Example Explanation
In the first result, e is in s2 so it stops counting after h.
In the second result, a is in s2.
In the third result, none of the characters of s1 are in s2 so all characters are counted.
<string.h>
Prototype
char *strerror(int errcode);
Argument
Return Value
Returns a pointer to an internal error message string corresponding to the specified error code errcode.
Remarks
The array pointed to by strerror may be overwritten by a subsequent call to this function.
Example
#include <stdio.h>
#include <string.h>
#include <errno.h>
int main(void)
{
FILE *myfile;
Example Output
s the string
Return Value
Returns the length of a string.
Remarks
This function determines the length of the string, not including the terminating null character.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char str1[20] = "We are here";
char str2[20] = "";
Example Output
str2 :
(string length = 0 characters)
Return Value
Returns a pointer to the destination string.
Remarks
This function appends up to n characters (a null character and characters that follow it are not appended) from the
source string to the end of the destination string. If a null character is not encountered, then a terminating null
character is appended to the result. If the strings overlap, the behavior is undefined.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "We're here";
char buf2[50] = "Where is the time?";
char buf3[50] = "Why?";
Example Output
buf3 : Why?
(4 characters)
s1 first string
s2 second string
n number of characters to compare
Return Value
Returns a positive number if s1 is greater than s2, zero if s1 is equal to s2 or a negative number if s1 is less than
s2.
Remarks
strncmp returns a value based on the first character that differs between s1 and s2. Characters that follow a null
character are not compared.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "Where is the time?";
char buf2[50] = "Where did they go?";
char buf3[50] = "Why?";
int res;
Example Output
Return Value
Returns a pointer to the destination string.
Remarks
Copies n characters from the source string to the destination string. If the source string is less than n characters, the
destination is filled with null characters to total n characters. If n characters were copied and no null character was
found, then the destination string will not be null-terminated. If the strings overlap, the behavior is undefined.
For strncpy_eds, strncpy_packed, strncpy_p2d16 or strncpy_p2d24, see “Functions for Specialized
Copying and Initialization.”
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "We're here";
char buf2[50] = "Where is the time?";
char buf3[50] = "Why?";
char buf4[7] = "Where?";
Example Output
Why?
( 4 characters)
Example Explanation
Each buffer contains the string shown, followed by null characters for a length of 50. Using strlen will find the
length of the string up to, but not including, the first null character.
In the first example, 6 characters of buf2 (“Where “) replace the first 6 characters of buf1 ("We’re ") and the rest of
buf1 remains the same ("here" plus null characters).
In the second example, 18 characters replace the first 18 characters of buf1 and the rest remain null characters.
In the third example, 5 characters of buf3 ("Why?" plus a null terminating character) replace the first 5 characters of
buf1. buf1 now actually contains ("Why?", 1 null character, " is the time?", 32 null characters). strlen shows 4
characters because it stops when it reaches the first null character.
In the fourth example, since buf4 is only 7 characters, strncpy uses 2 additional null characters to replace the first
9 characters of buf1. The result of buf1 is 6 characters ("Where?") followed by 3 null characters, followed by 9
characters ("the time?"), followed by 32 null characters.
Return Value
Returns a pointer to the matched character in s1 if found; otherwise, returns a null pointer.
Remarks
This function will search s1 for the first occurrence of a character contained in s2.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char str1[20] = "What time is it?";
char str2[20] = "xyz";
char str3[20] = "eou?";
char *ptr;
int res;
printf("\n");
Example Output
Return Value
Returns a pointer to the location of the match if successful; otherwise, returns a null pointer.
Remarks
This function searches the string s to find the last occurrence of the character, c.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char buf1[50] = "What time is it?";
char ch1 = 'm', ch2 = 'y';
char *ptr;
int res;
Example Output
m found at position 8
y not found
Return Value
Returns the number of consecutive characters from the beginning of s1 that are contained in s2.
Remarks
This function stops searching when a character from s1 is not in s2.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char str1[20] = "animal";
char str2[20] = "aeiounm";
char str3[20] = "aimnl";
char str4[20] = "xyz";
int res;
Example Output
strspn("animal", "aeiounm") = 5
strspn("animal", "aimnl") = 6
strspn("animal", "xyz") = 0
Example Explanation
In the first result, l is not in s2.
In the second result, the terminating null is not in s2.
In the third result, a is not in s2 , so the comparison stops.
Return Value
Returns the address of the first element that matches the substring if found; otherwise, returns a null pointer.
Remarks
This function will find the first occurrence of the string s2 (excluding the null terminator) within the string s1. If s2
points to a zero length string, s1 is returned.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char str1[20] = "What time is it?";
char str2[20] = "is";
char str3[20] = "xyz";
char *ptr;
int res;
Example Output
Return Value
Returns a pointer to the first character of a token (the first character in s1 that does not appear in the set of
characters of s2). If no token is found, the null pointer is returned.
Remarks
A sequence of calls to this function can be used to split up a string into substrings (or tokens) by replacing specified
characters with null characters. The first time this function is invoked on a particular string, that string should be
passed in s1. After the first time, this function can continue parsing the string from the last delimiter by invoking it with
a null value passed in s1.
It skips all leading characters that appear in the string s2 (delimiters), then skips all characters not appearing in s2
(this segment of characters is the token), and then overwrites the next character with a null character, terminating the
current token. The function, strtok, then saves a pointer to the character that follows, from which the next search
will start. If strtok finds the end of the string before it finds a delimiter, the current token extends to the end of the
string pointed to by s1. If this is the first call to strtok, it does not modify the string (no null characters are written to
s1). The set of characters that is passed in s2 need not be the same for each call to strtok.
If strtok is called with a non-null parameter for s1 after the initial call, the string becomes the new string to search.
The old string previously searched will be lost.
Example
#include <string.h>
#include <stdio.h>
int main(void)
{
char str1[30] = "Here, on top of the world!";
char delim[5] = ", .";
char *word;
int x;
}
}
Example Output
s1 destination string
s2 source string to be transformed
n number of characters to transform
Return Value
Returns the length of the transformed string not including the terminating null character. If n is zero, the string is not
transformed (s1 may be a point null in this case) and the length of s2 is returned.
Remarks
If the return value is greater than or equal to n, the content of s1 is indeterminate. Since the 16-bit compiler does not
support alternate locales, the transformation is equivalent to strcpy, except that the length of the destination string
is bounded by n-1.
clock_t
Stores processor time values.
Prototype
typedef long clock_t
struct tm
Structure used to hold the time and date (calendar time).
Prototype
struct tm {
int tm_sec; /*seconds after the minute ( 0 to 61 )*/
/*allows for up to two leap seconds*/
int tm_min; /*minutes after the hour ( 0 to 59 )*/
int tm_hour; /*hours since midnight ( 0 to 23 )*/
Remarks
If tm_isdst is a positive value, Daylight Savings is in effect. If it is zero, Daylight Saving Time is not in effect. If it is a
negative value, the status of Daylight Saving Time is not known.
time_t
Represents calendar time values.
Prototype
typedef long time_t
CLOCKS_PER_SEC
Number of processor clocks per second.
Prototype
#define CLOCKS_PER_SEC
Value
1
Remarks
The compiler returns clock ticks (instruction cycles) not actual time.
Related Links
2.2 Multiple Header Types and Macros
Return Value
Returns a pointer to a character string of the following format:
DDD MMM dd hh:mm:ss YYYY
DDD is day of the week
MMM is month of the year
dd is day of the month
hh is hour
mm is minute
ss is second
YYYY is year
Example
volatile int i;
int main(void)
{
struct tm when;
time_t whattime;
when.tm_sec = 30;
when.tm_min = 30;
when.tm_hour = 2;
when.tm_mday = 1;
when.tm_mon = 1;
when.tm_year = 103;
whattime = mktime(&when);
printf("Day and time is %s\n", asctime(&when));
}
Example Output
volatile int i;
int main(void)
{
clock_t start, stop;
int ct;
start = clock();
for (i = 0; i < 10; i++)
stop = clock();
printf("start = %ld\n", start);
printf("stop = %ld\n", stop);
}
Example Output
start = 0
stop = 317
Return Value
Returns the address of a string that represents the local time of the parameter passed.
Remarks
This function is equivalent to asctime(localtime(tod)).
Example
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t whattime;
struct tm nowtime;
nowtime.tm_sec = 30;
nowtime.tm_min = 30;
nowtime.tm_hour = 2;
nowtime.tm_mday = 1;
nowtime.tm_mon = 1;
nowtime.tm_year = 103;
whattime = mktime(&nowtime);
printf("Day and time %s\n", ctime(&whattime));
}
Example Output
t1 ending time
t0 beginning time
Return Value
Returns the number of seconds between t1 and t0.
Remarks
By default, the 16-bit compiler returns the time as instruction cycles so difftime returns the number of ticks
between t1 and t0.
Example
#include <time.h>
#include <stdio.h>
volatile int i;
int main(void)
{
clock_t start, stop;
double elapsed;
start = clock();
for (i = 0; i < 10; i++)
stop = clock();
printf("start = %ld\n", start);
printf("stop = %ld\n", stop);
elapsed = difftime(stop, start);
printf("Elapsed time = %.0f\n", elapsed);
}
Example Output
start = 0
stop = 317
Elapsed time = 317
Return Value
Returns the address of the time structure.
Remarks
This function breaks down the tod value into the time structure of type tm. By default, the compiler returns the time
as instruction cycles. With this default, gmtime and localtime will be equivalent, except gmtime will return
tm_isdst (Daylight Savings Time flag) as zero to indicate that Daylight Savings Time is not in effect.
Example
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t timer;
struct tm *newtime;
newtime = gmtime(&timer);
printf("UTC time = %s\n", asctime(newtime));
}
Example Output
Return Value
Returns the address of the time structure.
Remarks
By default, the 16-bit compiler returns the time as instruction cycles. With this default, localtime and gmtime will
be equivalent, except localtime will return tm_isdst (Daylight Savings Time flag) as -1 to indicate that the status
of Daylight Savings Time is not known.
Example
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t timer;
struct tm *newtime;
newtime = localtime(&timer);
printf("Local time = %s\n", asctime(newtime));
}
Example Output
Return Value
Returns the calendar time encoded as a value of time_t.
Remarks
If the calendar time cannot be represented, the function returns -1 cast as a time_t (i.e. (time_t) -1).
Example
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t timer, whattime;
struct tm *newtime;
whattime = mktime(newtime);
printf("Calendar time as time_t = %ld\n",
whattime);
}
Example Output
s output string
n maximum length of string
format format-control string
tptr pointer to tm data structure
Return Value
Returns the number of characters placed in the array, s, if the total, including the terminating null, is not greater than
n. Otherwise, the function returns 0 and the contents of array s are indeterminate.
Remarks
The format parameters follow:
Example
#include <time.h>
#include <stdio.h>
int main(void)
{
time_t timer, whattime;
struct tm *newtime;
char buf[128];
timer = 1066668182; /* Mon Oct 20 16:43:02 2003 */
/* localtime allocates space for structure */
newtime = localtime(&timer);
strftime(buf, 128, "It was a %A, %d days into the "
"month of %B in the year %Y.\n", newtime);
printf(buf);
strftime(buf, 128, "It was %W weeks into the year "
"or %j days into the year.\n", newtime);
printf(buf);
}
Example Output
It was a Monday, 20 days into the month of October in the year 2003.
It was 42 weeks into the year or 293 days into the year.
Return Value
Returns the calendar time encoded as a value of time_t.
Remarks
If the target environment cannot determine the time, the function returns -1 cast as a time_t. By default, the
compiler returns the time as instruction cycles. This function is customizable (see pic30-libs).
Example
#include <time.h>
#include <stdio.h>
volatile int i;
int main(void)
{
time_t ticks;
Example Output
Time = 256
3. Math Libraries
Standard ANSI C library math functions are contained in the file libm-omf.a, where omf will be elf or coff
depending upon the selected object module format.
For details on using the standard C libraries, see “Standard C Libraries.”
3.1.1 HUGE_VAL
HUGE_VAL is returned by a function on a range error (e.g., the function tries to return a value too large to be
represented in the target precision).
Include
<math.h>
Remarks
HUGE_VAL is returned if a function result is negative and is too large (in magnitude) to be represented in the target
precision. When the printed result is +/- HUGE_VAL, it will be represented by +/- inf.
Return Value
Returns the arc cosine in radians in the range of 0 to pi (inclusive).
Remarks
A domain error occurs if x is less than -1 or greater than 1.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x,y;
errno = 0;
x = -2.0;
y = acos (x);
if (errno)
perror("Error");
printf("The arccosine of %f is %f\n", x, y);
errno = 0;
x = 0.10;
y = acos (x);
if (errno)
perror("Error");
printf("The arccosine of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the arc cosine in radians in the range of 0 to pi (inclusive).
Remarks
A domain error occurs if x is less than -1 or greater than 1.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y;
errno = 0;
x = 2.0F;
y = acosf (x);
if (errno)
perror("Error");
printf("The arccosine of %f is %f\n", x, y);
errno = 0;
x = 0.0F;
y = acosf (x);
if (errno)
perror("Error");
Example Output
Return Value
Returns the arc sine in radians in the range of -pi/2 to +pi/2 (inclusive).
Remarks
A domain error occurs if x is less than -1 or greater than 1.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x, y;
errno = 0;
x = 2.0;
y = asin (x);
if (errno)
perror("Error");
printf("The arcsine of %f is %f\n", x, y);
errno = 0;
x = 0.0;
y = asin (x);
if (errno)
perror("Error");
printf("The arcsine of %f is %f\n", x, y);
}
Example Output
Prototype
float asinf (float x);
Argument
Return Value
Returns the arc sine in radians in the range of -pi/2 to +pi/2 (inclusive).
Remarks
A domain error occurs if x is less than -1 or greater than 1.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y;
errno = 0;
x = 2.0F;
y = asinf(x);
if (errno)
perror("Error");
printf("The arcsine of %f is %f\n", x, y);
errno = 0;
x = 0.0F;
y = asinf(x);
if (errno)
perror("Error");
printf("The arcsine of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the arc tangent in radians in the range of -pi/2 to +pi/2 (inclusive).
Remarks
No domain or range error will occur.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
double x, y;
x = 2.0;
y = atan (x);
printf("The arctangent of %f is %f\n", x, y);
x = -1.0;
y = atan (x);
printf("The arctangent of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the arc tangent in radians in the range of -pi/2 to +pi/2 (inclusive).
Remarks
No domain or range error will occur.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
float x, y;
x = 2.0F;
y = atanf (x);
printf("The arctangent of %f is %f\n", x, y);
x = -1.0F;
y = atanf (x);
printf("The arctangent of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the arc tangent in radians in the range of -pi to pi (inclusive) with the quadrant determined by the signs of
both parameters.
Remarks
A domain error occurs if both x and y are zero or both x and y are +/- infinity.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x, y, z;
errno = 0;
x = 0.0;
y = 2.0;
z = atan2(y, x);
if (errno)
perror("Error");
printf("The arctangent of %f/%f is %f\n", y, x, z);
errno = 0;
x = -1.0;
y = 0.0;
z = atan2(y, x);
if (errno)
perror("Error");
printf("The arctangent of %f/%f is %f\n", y, x, z);
errno = 0;
x = 0.0;
y = 0.0;
z = atan2(y, x);
if (errno)
perror("Error");
printf("The arctangent of %f/%f is %f\n", y, x, z);
}
Example Output
<math.h>
Prototype
float atan2f (float y, float x);
Arguments
Return Value
Returns the arc tangent in radians in the range of -pi to pi with the quadrant determined by the signs of both
parameters.
Remarks
A domain error occurs if both x and y are zero or both x and y are +/- infinity.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y, z;
errno = 0;
x = 2.0F;
y = 0.0F;
z = atan2f (y, x);
if (errno)
perror("Error");
printf("The arctangent of %f/%f is %f\n", y, x, z);
errno = 0;
x = 0.0F;
y = -1.0F;
z = atan2f (y, x);
if (errno)
perror("Error");
printf("The arctangent of %f/%f is %f\n", y, x, z);
errno = 0;
x = 0.0F;
y = 0.0F;
z = atan2f (y, x);
if (errno)
perror("Error");
printf("The arctangent of %f/%f is %f\n", y, x, z);
}
Example Output
Return Value
Returns the smallest integer value greater than or equal to x.
Remarks
No domain or range error will occur. See floor.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
double x[8] = {2.0, 1.75, 1.5, 1.25, -2.0, -1.75, -1.5, -1.25};
double y;
int i;
Example Output
Return Value
Returns the smallest integer value greater than or equal to x.
Remarks
No domain or range error will occur. See floorf.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
float x[8] = {2.0F, 1.75F, 1.5F, 1.25F, -2.0F, -1.75F, -1.5F, -1.25F};
float y;
int i;
Example Output
Return Value
Returns the cosine of x in radians in the ranges of -1 to 1 inclusive.
Remarks
A domain error will occur if x is a NaN or infinity.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x,y;
errno = 0;
x = -1.0;
y = cos (x);
if (errno)
perror("Error");
printf("The cosine of %f is %f\n", x, y);
errno = 0;
x = 0.0;
y = cos (x);
if (errno)
perror("Error");
printf("The cosine of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the cosine of x in radians in the ranges of -1 to 1 inclusive.
Remarks
A domain error will occur if x is a NaN or infinity.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y;
errno = 0;
x = -1.0F;
y = cosf (x);
if (errno)
perror("Error");
printf("The cosine of %f is %f\n", x, y);
errno = 0;
x = 0.0F;
y = cosf (x);
if (errno)
perror("Error");
printf("The cosine of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the hyperbolic cosine of x.
Remarks
A range error will occur if the magnitude of x is too large.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x, y;
errno = 0;
x = -1.5;
y = cosh (x);
if (errno)
perror("Error");
printf("The hyperbolic cosine of %f is %f\n", x, y);
errno = 0;
x = 0.0;
y = cosh (x);
if (errno)
perror("Error");
printf("The hyperbolic cosine of %f is %f\n", x, y);
errno = 0;
x = 720.0;
y = cosh (x);
if (errno)
perror("Error");
printf("The hyperbolic cosine of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the hyperbolic cosine of x.
Remarks
A range error will occur if the magnitude of x is too large.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y;
errno = 0;
x = -1.0F;
y = coshf (x);
if (errno)
perror("Error");
printf("The hyperbolic cosine of %f is %f\n", x, y);
errno = 0;
x = 0.0F;
y = coshf (x);
if (errno)
perror("Error");
printf("The hyperbolic cosine of %f is %f\n", x, y);
errno = 0;
x = 720.0F;
y = coshf (x);
if (errno)
perror("Error");
printf("The hyperbolic cosine of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the exponential of x. On an overflow, exp returns inf and on an underflow exp returns 0.
Remarks
A range error occurs if the magnitude of x is too large.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x, y;
errno = 0;
x = 1.0;
y = exp(x);
if (errno)
perror("Error");
printf("The exponential of %f is %f\n", x, y);
errno = 0;
x = 1E3;
y = exp(x);
if (errno)
perror("Error");
printf("The exponential of %f is %f\n", x, y);
errno = 0;
x = -1E3;
y = exp(x);
if (errno)
perror("Error");
printf("The exponential of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the exponential of x. On an overflow, expf returns inf and on an underflow expf returns 0.
Remarks
A range error occurs if the magnitude of x is too large.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y;
errno = 0;
x = 1.0F;
y = expf(x);
if (errno)
perror("Error");
printf("The exponential of %f is %f\n", x, y);
errno = 0;
x = 1.0E3F;
y = expf(x);
if (errno)
perror("Error");
printf("The exponential of %f is %f\n", x, y);
errno = 0;
x = -1.0E3F;
y = expf(x);
if (errno)
perror("Error");
printf("The exponential of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the absolute value of x. A negative number is returned as positive; a positive number is unchanged.
Remarks
No domain or range error will occur.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
double x, y;
x = 1.75;
y = fabs (x);
printf("The absolute value of %f is %f\n", x, y);
x = -1.5;
y = fabs (x);
printf("The absolute value of %f is %f\n", x, y);
}
Example Output
<math.h>
Prototype
float fabsf(float x);
Argument
Return Value
Returns the absolute value of x. A negative number is returned as positive; a positive number is unchanged.
Remarks
No domain or range error will occur.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
float x,y;
x = 1.75F;
y = fabsf (x);
printf("The absolute value of %f is %f\n", x, y);
x = -1.5F;
y = fabsf (x);
printf("The absolute value of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the largest integer value less than or equal to x.
Remarks
No domain or range error will occur. See ceil.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
double x[8] = {2.0, 1.75, 1.5, 1.25, -2.0,
Example Output
Return Value
Returns the largest integer value less than or equal to x.
Remarks
No domain or range error will occur. See ceilf.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
float x[8] = {2.0F, 1.75F, 1.5F, 1.25F,
-2.0F, -1.75F, -1.5F, -1.25F};
float y;
int i;
Example Output
Return Value
Returns the remainder of x divided by y.
Remarks
If y = 0, a domain error occurs. If y is non-zero, the result will have the same sign as x and the magnitude of the
result will be less than the magnitude of y.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x,y,z;
errno = 0;
x = 7.0;
y = 3.0;
z = fmod(x, y);
if (errno)
perror("Error");
printf("For fmod(%f, %f) the remainder is %f\n", x, y, z);
errno = 0;
x = 7.0;
y = 7.0;
z = fmod(x, y);
if (errno)
perror("Error");
printf("For fmod(%f, %f) the remainder is %f\n", x, y, z);
errno = 0;
x = -5.0;
y = 3.0;
z = fmod(x, y);
if (errno)
perror("Error");
printf("For fmod(%f, %f) the remainder is %f\n", x, y, z);
errno = 0;
x = 5.0;
y = -3.0;
z = fmod(x, y);
if (errno)
perror("Error");
printf("For fmod(%f, %f) the remainder is %f\n", x, y, z);
errno = 0;
x = -5.0;
y = -5.0;
z = fmod(x, y);
if (errno)
perror("Error");
printf("For fmod(%f, %f) the remainder is %f\n", x, y, z);
errno = 0;
x = 7.0;
y = 0.0;
z = fmod(x, y);
if (errno)
perror("Error");
printf("For fmod(%f, %f) the remainder is %f\n", x, y, z);
}
Example Output
Return Value
Returns the remainder of x divided by y.
Remarks
If y = 0, a domain error occurs. If y is non-zero, the result will have the same sign as x and the magnitude of the
result will be less than the magnitude of y.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x,y,z;
errno = 0;
x = 7.0F;
y = 3.0F;
z = fmodf(x, y);
if(errno)
perror("Error");
printf("For fmodf(%f, %f) the remainder is"
" %f\n\n", x, y, z);
errno = 0;
x = -5.0F;
y = 3.0F;
z = fmodf(x, y);
if(errno)
perror("Error");
printf("For fmodf(%f, %f) the remainder is %f\n", x, y, z);
errno = 0;
x = 5.0F;
y = -3.0F;
z = fmodf(x, y);
if(errno)
perror("Error");
printf("For fmodf(%f, %f) the remainder is %f\n", x, y, z);
errno = 0;
x = 5.0F;
y = -5.0F;
z = fmodf (x, y);
if(errno)
perror("Error");
printf("For fmodf (%f, %f) the remainder is %f\n", x, y, z);
errno = 0;
x = 7.0F;
y = 0.0F;
z = fmodf(x, y);
if(errno)
perror("Error");
printf("For fmodf(%f, %f) the remainder is %f\n", x, y, z);
errno = 0;
x = 7.0F;
y = 7.0F;
z = fmodf(x, y);
if(errno)
perror("Error");
printf("For fmodf(%f, %f) the remainder is %f\n", x, y, z);
}
Example Output
Return Value
Returns the fraction, exp points to the exponent. If x is 0, the function returns 0 for both the fraction and exponent.
Remarks
The absolute value of the fraction is in the range of 1/2 (inclusive) to 1 (exclusive). No domain or range error will
occur.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
double x,y;
int n;
x = 50.0;
y = frexp (x, &n);
printf("For frexp of %f\n the fraction is %f\n ", x, y);
printf(" and the exponent is %d\n\n", n);
x = -2.5;
y = frexp (x, &n);
printf("For frexp of %f\n the fraction is %f\n ", x, y);
printf(" and the exponent is %d\n\n", n);
x = 0.0;
y = frexp (x, &n);
printf("For frexp of %f\n the fraction is %f\n ", x, y);
printf(" and the exponent is %d\n\n", n);
}
Example Output
Return Value
Returns the fraction, exp points to the exponent. If x is 0, the function returns 0 for both the fraction and exponent.
Remarks
The absolute value of the fraction is in the range of 1/2 (inclusive) to 1 (exclusive). No domain or range error will
occur.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
float x,y;
int n;
x = 0.15F;
y = frexpf (x, &n);
printf("For frexpf of %f\n the fraction is %f\n ", x, y);
printf(" and the exponent is %d\n\n", n);
x = -2.5F;
y = frexpf (x, &n);
printf("For frexpf of %f\n the fraction is %f\n ", x, y);
printf(" and the exponent is %d\n\n", n);
x = 0.0F;
y = frexpf (x, &n);
printf("For frexpf of %f\n the fraction is %f\n ", x, y);
printf(" and the exponent is %d\n\n", n);
}
Example Output
x floating-point value
ex integer exponent
Return Value
Returns x * 2^ex. On an overflow, ldexp returns inf and on an underflow, ldexp returns 0.
Remarks
A range error will occur on overflow or underflow.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x,y;
int n;
errno = 0;
x = -0.625;
n = 2;
y = ldexp (x, n);
if (errno)
perror("Error");
printf("For a number = %f and an exponent = %d\n",
x, n);
printf(" ldexp(%f, %d) = %f\n\n",
x, n, y);
errno = 0;
x = 2.5;
n = 3;
y = ldexp (x, n);
if (errno)
perror("Error");
printf("For a number = %f and an exponent = %d\n",
x, n);
printf(" ldexp(%f, %d) = %f\n\n",
x, n, y);
errno = 0;
x = 15.0;
n = 10000;
y = ldexp (x, n);
if (errno)
perror("Error");
printf("For a number = %f and an exponent = %d\n",
x, n);
printf(" ldexp(%f, %d) = %f\n\n",
x, n, y);
}
Example Output
x floating-point value
ex integer exponent
Return Value
Returns x * 2^ex. On an overflow, ldexp returns inf and on an underflow, ldexpf returns 0.
Remarks
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x,y;
int n;
errno = 0;
x = -0.625F;
n = 2;
y = ldexpf (x, n);
if (errno)
perror("Error");
printf("For a number = %f and an exponent = %d\n", x, n);
printf(" ldexpf(%f, %d) = %f\n\n", x, n, y);
errno = 0;
x = 2.5F;
n = 3;
y = ldexpf (x, n);
if (errno)
perror("Error");
printf("For a number = %f and an exponent = %d\n", x, n);
printf(" ldexpf(%f, %d) = %f\n\n", x, n, y);
errno = 0;
x = 15.0F;
n = 10000;
y = ldexpf (x, n);
if (errno)
perror("Error");
printf("For a number = %f and an exponent = %d\n", x, n);
printf(" ldexpf(%f, %d) = %f\n\n", x, n, y);
}
Example Output
Return Value
Returns the natural logarithm of x. -inf is returned if x is 0 and NaN is returned if x is a negative number.
Remarks
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x, y;
errno = 0;
x = 2.0;
y = log(x);
if (errno)
perror("Error");
printf("The natural logarithm of %f is %f\n",
x, y);
errno = 0;
x = 0.0;
y = log(x);
if (errno)
perror("Error");
printf("The natural logarithm of %f is %f\n",
x, y);
errno = 0;
x = -2.0;
y = log(x);
if (errno)
perror("Error");
printf("The natural logarithm of %f is %f\n",
x, y);
}
Example Output
Return Value
Returns the natural logarithm of x. -inf is returned if x is 0 and NaN is returned if x is a negative number.
Remarks
A domain error occurs if x ≤ 0.
Example Output
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y;
errno = 0;
x = 2.0F;
y = logf(x);
if (errno)
perror("Error");
printf("The natural logarithm of %f is %f\n",
x, y);
errno = 0;
x = 0.0F;
y = logf(x);
if (errno)
perror("Error");
printf("The natural logarithm of %f is %f\n",
x, y);
errno = 0;
x = -2.0F;
y = logf(x);
if (errno)
perror("Error");
printf("The natural logarithm of %f is %f\n",
x, y);
}
Example Output
Return Value
Returns the base-10 logarithm of x. -inf is returned if x is 0 and NaN is returned if x is a negative number.
Remarks
A domain error occurs if x ≤ 0.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x, y;
errno = 0;
x = 2.0;
y = log10 (x);
if (errno)
perror("Error");
printf("The base-10 logarithm of %f is %f\n", x, y);
errno = 0;
x = 0.0;
y = log10 (x);
if (errno)
perror("Error");
printf("The base-10 logarithm of %f is %f\n", x, y);
errno = 0;
x = -2.0;
y = log10 (x);
if (errno)
perror("Error");
printf("The base-10 logarithm of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the base-10 logarithm of x. -inf is returned if x is 0 and NaN is returned if x is a negative number.
Remarks
A domain error occurs if x ≤ 0.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y;
errno = 0;
x = 2.0F;
y = log10f(x);
if (errno)
perror("Error");
printf("The base-10 logarithm of %f is %f\n", x, y);
errno = 0;
x = 0.0F;
y = log10f(x);
if (errno)
perror("Error");
printf("The base-10 logarithm of %f is %f\n", x, y);
errno = 0;
x = -2.0F;
y = log10f(x);
if (errno)
perror("Error");
printf("The base-10 logarithm of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the signed fractional part and pint points to the integer part.
Remarks
The absolute value of the fractional part is in the range of 0 (inclusive) to 1 (exclusive). No domain or range error will
occur.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
double x, y, n;
x = 0.707;
y = modf(x, &n);
printf("For %f the fraction is %f\n ", x, y);
printf(" and the integer is %0.f\n\n", n);
x = -15.2121;
y = modf(x, &n);
printf("For %f the fraction is %f\n ", x, y);
printf(" and the integer is %0.f\n\n", n);
}
Example Output
Return Value
Returns the signed fractional part and pint points to the integer part.
Remarks
The absolute value of the fractional part is in the range of 0 (inclusive) to 1 (exclusive). No domain or range error will
occur.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
float x, y, n;
x = 0.707F;
y = modff(x, &n);
printf("For %f the fraction is %f\n ", x, y);
printf(" and the integer is %0.f\n\n", n);
x = -15.2121F;
y = modff(x, &n);
printf("For %f the fraction is %f\n ", x, y);
printf(" and the integer is %0.f\n\n", n);
}
Example Output
x the base
y the exponent
Return Value
Returns x raised to the power y (xy).
Remarks
If y is 0, pow returns 1. If x is 0.0 and y is less than 0, pow returns inf and a domain error occurs. If the result
overflows or underflows, a range error occurs.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x,y,z;
errno = 0;
x = -2.0;
y = 3.0;
z = pow(x, y);
if (errno)
perror("Error");
printf("%f raised to %f is %f\n ", x, y, z);
errno = 0;
x = 3.0;
y = -0.5;
z = pow(x, y);
if (errno)
perror("Error");
printf("%f raised to %f is %f\n ", x, y, z);
errno = 0;
x = 4.0;
y = 0.0;
z = pow(x, y);
if (errno)
perror("Error");
printf("%f raised to %f is %f\n ", x, y, z);
errno = 0;
x = 0.0;
y = -3.0;
z = pow(x, y);
if (errno)
perror("Error");
printf("%f raised to %f is %f\n ", x, y, z);
}
Example Output
x the base
y the exponent
Return Value
Returns x raised to the power y (x^y).
Remarks
If y is 0, pow returns 1. If x is 0.0 and y is less than 0, pow returns inf and a domain error occurs. If the result
overflows or underflows, a range error occurs.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x,y,z;
errno = 0;
x = -2.0F;
y = 3.0F;
z = powf (x, y);
if (errno)
perror("Error");
printf("%f raised to %f is %f\n ", x, y, z);
errno = 0;
x = 3.0F;
y = -0.5F;
z = powf (x, y);
if (errno)
perror("Error");
printf("%f raised to %f is %f\n ", x, y, z);
errno = 0;
x = 0.0F;
y = -3.0F;
z = powf (x, y);
if (errno)
perror("Error");
printf("%f raised to %f is %f\n ", x, y, z);
}
Example Output
Return Value
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x, y;
errno = 0;
x = -1.0;
y = sin (x);
if (errno)
perror("Error");
printf("The sine of %f is %f\n", x, y);
errno = 0;
x = 0.0;
y = sin (x);
if (errno)
perror("Error");
printf("The sine of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the sine of x in radians in the ranges of -1 to 1 inclusive.
Remarks
A domain error will occur if x is a NaN or infinity.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y;
errno = 0;
x = -1.0F;
y = sinf (x);
if (errno)
perror("Error");
printf("The sine of %f is %f\n", x, y);
errno = 0;
x = 0.0F;
y = sinf (x);
if (errno)
perror("Error");
printf("The sine of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the hyperbolic sine of x.
Remarks
A range error will occur if the magnitude of x is too large.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x, y;
errno = 0;
x = -1.5;
y = sinh (x);
if (errno)
perror("Error");
printf("The hyperbolic sine of %f is %f\n",
x, y);
errno = 0;
x = 0.0;
y = sinh (x);
if (errno)
perror("Error");
printf("The hyperbolic sine of %f is %f\n",
x, y);
errno = 0;
x = 720.0;
y = sinh (x);
if (errno)
perror("Error");
printf("The hyperbolic sine of %f is %f\n",
x, y);
}
Example Output
Return Value
Returns the hyperbolic sine of x.
Remarks
A range error will occur if the magnitude of x is too large.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y;
errno = 0;
x = -1.0F;
y = sinhf (x);
if (errno)
perror("Error");
printf("The hyperbolic sine of %f is %f\n", x, y);
errno = 0;
x = 0.0F;
y = sinhf (x);
if (errno)
perror("Error");
printf("The hyperbolic sine of %f is %f\n", x, y);
}
Example Output
Prototype
double sqrt(double x);
Argument
Return Value
Returns the non-negative square root of x.
Remarks
If x is negative, a domain error occurs.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x, y;
errno = 0;
x = 0.0;
y = sqrt(x);
if (errno)
perror("Error");
printf("The square root of %f is %f\n", x, y);
errno = 0;
x = 9.5;
y = sqrt(x);
if (errno)
perror("Error");
printf("The square root of %f is %f\n", x, y);
errno = 0;
x = -25.0;
y = sqrt(x);
if (errno)
perror("Error");
printf("The square root of %f is %f\n", x, y);
}
Example Output
Return Value
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x;
errno = 0;
x = sqrtf (0.0F);
if (errno)
perror("Error");
printf("The square root of 0.0F is %f\n", x);
errno = 0;
x = sqrtf (9.5F);
if (errno)
perror("Error");
printf("The square root of 9.5F is %f\n", x);
errno = 0;
x = sqrtf (-25.0F);
if (errno)
perror("Error");
printf("The square root of -25F is %f\n", x);
}
Example Output
Return Value
Returns the tangent of x in radians.
Remarks
A domain error will occur if x is a NaN or infinity.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
double x, y;
errno = 0;
x = -1.0;
y = tan (x);
if (errno)
perror("Error");
printf("The tangent of %f is %f\n", x, y);
errno = 0;
x = 0.0;
y = tan (x);
if (errno)
perror("Error");
printf("The tangent of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the tangent of x in radians.
Remarks
A domain error will occur if x is a NaN or infinity.
Example
#include <math.h>
#include <stdio.h>
#include <errno.h>
int main(void)
{
float x, y;
errno = 0;
x = -1.0F;
y = tanf (x);
if (errno)
perror("Error");
printf("The tangent of %f is %f\n", x, y);
errno = 0;
x = 0.0F;
y = tanf (x);
if (errno)
perror("Error");
printf("The tangent of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the hyperbolic tangent of x in the ranges of -1 to 1 inclusive.
Remarks
No domain or range error will occur.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
double x, y;
x = -1.0;
y = tanh(x);
printf("The hyperbolic tangent of %f is %f\n", x, y);
x = 2.0;
y = tanh(x);
printf("The hyperbolic tangent of %f is %f\n", x, y);
}
Example Output
Return Value
Returns the hyperbolic tangent of x in the ranges of -1 to 1 inclusive.
Remarks
No domain or range error will occur.
Example
#include <math.h>
#include <stdio.h>
int main(void)
{
float x, y;
x = -1.0F;
y = tanhf(x);
printf("The hyperbolic tangent of %f is %f\n", x, y);
x = 0.0F;
y = tanhf(x);
printf("The hyperbolic tangent of %f is %f\n", x, y);
}
Example Output
4. Support Libraries
Support libraries contain support functions that either must be customized for correct operation of the Standard C
Library in your target environment or are already customized for a Microchip target environment. The default behavior
section describes what the function does, as it is distributed. The description and remarks describe what it typically
should do. The corresponding object modules are distributed in the libpic30-omf.a archive and the source code
(for the compiler) is available in the src\pic30 folder.
For details on using the standard C libraries, see “Standard C Libraries.”
File
_dump_heap_info.c
Remarks
This is a helper function called by the exit() Standard C Library function.
Default Behavior
As distributed, this function flushes stdout and terminates. The parameter status is the same as that passed to the
exit() standard C library function.
File
_exit.c
Return Value
Returns ‘0’ if successful; otherwise, returns ‘-1’.
Remarks
brk() is used to dynamically change the amount of space allocated for the calling process’s data segment. The
change is made by resetting the process’s break value and allocating the appropriate amount of space. The break
value is the address of the first location beyond the end of the data segment. The amount of allocated space
increases as the break value increases.
Newly allocated space is uninitialized.
This helper function is used by the Standard C Library function malloc().
Default Behavior
If the argument endds is zero, the function sets the global variable __curbrk to the address of the start of the heap
and returns zero.
If the argument endds is non-zero and has a value less than the address of the end of the heap, the function sets the
global variable __curbrk to the value of endds and returns zero.
Otherwise, the global variable __curbrk is unchanged and the function returns -1.
The argument endds must be within the heap range (see data space memory map below).
Since the stack is located immediately above the heap, using brk() or sbrk() has little effect on the size of the
dynamic memory pool. The brk() and sbrk() functions are primarily intended for use in run-time environments
where the stack grows downward and the heap grows upward.
The linker allocates a block of memory for the heap if the -Wl,--heap=n option is specified, where n is the desired
heap size in characters. The starting and ending addresses of the heap are reported in variables: _heap and
_eheap, respectively.
For the 16-bit compiler, using the linker’s heap size option is the standard way of controlling heap size, rather than
relying on brk() and sbrk().
File
brk.c
Return Value
Returns ‘0’ if the file is successfully closed. A return value of ‘-1’ indicates an error.
Remarks
This helper function is called by the fclose() Standard C Library function.
Default Behavior
As distributed, this function passes the file handle to the simulator, which issues a close in the host file system.
File
close.c
Return Value
Returns the offset, in characters, of the new position from the beginning of the file. A return value of ‘-1L’ indicates an
error.
Remarks
This helper function is called by the Standard C Library functions fgetpos(), ftell(), fseek(), fsetpos and
rewind().
Default Behavior
As distributed, the parameters are passed to the host file system through the simulator. The return value is the value
returned by the host file system.
File
lseek.c
Return Value
If successful, the function returns a file handle: a small positive integer. This handle is then used on subsequent low-
level file I/O operations. A return value of ‘-1’ indicates an error.
Remarks
The access flag is a union of one of the following access methods and zero or more access qualifiers:
• 0 – Open a file for reading.
• 1 – Open a file for writing.
• 2 – Open a file for both reading and writing.
The following access qualifiers must be supported:
• 0x0008 – Move file pointer to end-of-file before every write operation.
• 0x0100 – Create and open a new file for writing.
• 0x0200 – Open the file and truncate it to zero length.
• 0x4000 – Open the file in text (translated) mode.
• 0x8000 – Open the file in binary (untranslated) mode.
The mode parameter may be one of the following:
• 0x0100 – Reading only permitted.
• 0x0080 – Writing permitted (implies reading permitted).
This helper function is called by the Standard C Library functions fopen() and freopen().
Default Behavior
As distributed, the parameters are passed to the host file system through the simulator. The return value is the value
returned by the host file system. If the host system returns a value of ‘-1’, the global variable errno is set to the value
of the symbolic constant, EFOPEN, defined in <errno.h>.
File
open.c
Return Value
Returns the number of characters read, which may be less than len if there are fewer than len characters left in the
file or if the file was opened in text mode, in which case, each carriage return-linefeed (CR-LF) pair is replaced with a
single linefeed character. Only the single linefeed character is counted in the return value. The replacement does not
affect the file pointer. If the function tries to read at end-of-file, it returns ‘0’. If the handle is invalid, or the file is not
open for reading or the file is locked, the function returns ‘-1’.
Remarks
This helper function is called by the Standard C Library functions fgetc(), fgets(), fread() and gets().
Default Behavior
As distributed, the parameters are passed to the host file system through the simulator. The return value is the value
returned by the host file system.
File
read.c
Return Value
Return the start of the new space allocated or ‘-1’ for errors.
Remarks
sbrk() adds incr characters to the break value and changes the allocated space accordingly. incr can be
negative, in which case the amount of allocated space is decreased.
sbrk() is used to dynamically change the amount of space allocated for the calling process’s data segment. The
change is made by resetting the process’s break value and allocating the appropriate amount of space. The break
value is the address of the first location beyond the end of the data segment. The amount of allocated space
increases as the break value increases.
This is a helper function called by the Standard C Library function malloc().
Default Behavior
If the global variable __curbrk is zero, the function calls brk() to initialize the break value. If brk() returns -1, so
does this function.
If the incr is zero, the current value of the global variable __curbrk is returned.
If the incr is non-zero, the function checks that the address (__curbrk + incr) is less than the end address of the
heap. If it is less, the global variable __curbrk is updated to that value and the function returns the unsigned value
of __curbrk.
Otherwise, the function returns -1.
See the of brk().
File
sbrk.c
Return Value
If successful, write returns the number of characters actually written. A return value of ‘-1’ indicates an error.
Remarks
If the actual space remaining on the disk is less than the size of the buffer, the function trying to write to the disk write
fails and does not flush any of the buffer’s contents to the disk. If the file is opened in text mode, each linefeed
character is replaced with a carriage return – linefeed pair in the output. The replacement does not affect the return
value.
This is a helper function called by the Standard C Library function fflush().
Default Behavior
As distributed, the parameters are passed to the host file system through the simulator. The return value is the value
returned by the host file system.
File
write.c
#ifndef __dsPIC33F__
#error this is a 33F demo for the explorer 16(tm) board
#endif
#inlcude <p33Fxxxx.h>
_FOSCSEL(FNOSC_PRI );
_FOSC(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMD_XT);
_FWDT(FWDTEN_OFF);
main() {
ODCA = 0;
TRISAbits.TRISA6 = 0;
__C30_UART=2;
U2BRG = 38;
U2MODEbits.UARTEN = 1;
while (1) {
__builtin_btg(&LATA,6);
printf("Hello world %d\n",U2BRG);
}
}
p pointer to file
Remarks
This function differs from the MPLAB X IDE mechanism of providing an input file because it provides “on-demand”
access to the file. That is, data will only be read from the file upon request and the asynchronous nature of the UART
is not simulated. This function may be called more than once; any opened file will be closed. It is only appropriate to
call this function in a simulated environment.
Default Behavior
Allows the programmer to attach a hosted file to the standard input stream, stdin.
The function will return 0 to indicate failure. If the file cannot be opened for whatever reason, standard in will remain
connected (or be re-connected) to the simulated UART.
File
attach.c
Argument
Remarks
None.
Default Behavior
This function will effect a delay of the requested number of cycles. The minimum supported delay is 12 cycles (an
argument of less than or equal to 12 will result in 12 cycles). The delay includes the call and return statements, but
not any cycles required to set up the argument (typically this would be two for a literal value).
File
delay32.s
Remarks
This function is implemented as a macro.
Default Behavior
This function relies on a user-supplied definition of FCY to represent the instruction clock frequency. FCY must be
defined before header file libpic30.h is included. The specified delay is converted to the equivalent number of
instruction cycles and passed to __delay32(). If FCY is not defined, then __delay_ms() is declared external,
causing the link to fail unless the user provides a function with that name.
File
delay32.s
Remarks
This function is implemented as a macro. The minimum delay is equivalent to 12 instruction cycles.
Default Behavior
This function relies on a user-supplied definition of FCY to represent the instruction clock frequency. FCY must be
defined before header file libpic30.h is included. The specified delay is converted to the equivalent number of
instruction cycles and passed to __delay32(). If FCY is not defined, then __delay_ms() is declared external,
causing the link to fail unless the user provides a function with that name.
File
delay32.s
#include "libpic30.h"
#include "p30fxxxx.h"
int main() {
char i,source[_EE_ROW];
_prog_addressT p;
int main() {
_prog_addressT p;
_init_prog_address(p, dat); /* get address in program space */
_erase_eedata(p, _EE_4WORDS); /* erase the dat[] array */
_wait_eedata(); /* wait to complete */
_write_eedata_word(p, 0x1234); /* write a word to dat[0] */
_wait_eedata();
p += 2;
_write_eedata_word(p, 0x5678); /* write a word to dat[1] */
_wait_eedata();
}
Return Value
None.
Remarks
None.
Default Behavior
Erase EEDATA memory as specified by parameters.
File
eedata_helper.c
None.
Default Behavior
Wait for an erase or write operation to complete.
File
eedata_helper.c
Return Value
None.
Remarks
None.
Default Behavior
Write specified bytes of EEDATA memory.
File
eedata_helper.c
Return Value
None.
Remarks
None.
Default Behavior
Write one word of EEDATA memory for dsPIC30F devices.
File
eedata_helper.c
#include "libpic30.h"
#include "p24Fxxxx.h"
int main() {
int i;
int source1[_FLASH_ROW];
long source2[_FLASH_ROW];
_prog_addressT p;
int main() {
int i;
int source1[_FLASH_ROW];
long source2[_FLASH_ROW];
_prog_addressT p;
<libpic30.h>
Prototype
void _erase_flash(_prog_addressT dst);
Argument
Return Value
None.
Remarks
None.
Default Behavior
Erase a page of Flash memory.
File
flash_helper.s
Return Value
None.
Remarks
None.
Default Behavior
Erase rows of Flash memory.
File
flash_helper2.s
Return Value
None.
Remarks
None.
Default Behavior
Write a row of Flash memory with 16-bit data.
File
flash_helper.c
Return Value
None.
Remarks
None.
Default Behavior
Write a row of Flash memory with 24-bit data.
File
flash_helper.c
Return Value
None.
Remarks
None.
Default Behavior
Write a word of Flash memory with 16-bit data for most PIC24 devices.
File
flash_helper.c
Return Value
None.
Remarks
None.
Default Behavior
Write a word of Flash memory with 24-bit data for most PIC24 devices.
File
flash_helper.c
Return Value
None.
Remarks
None.
Default Behavior
Write two words of Flash memory with 16-bit data for most dsPIC33E/PIC24E devices.
File
flash_helper.c
Return Value
None.
Remarks
None.
Default Behavior
Write two words of Flash memory with 48-bit data for most dsPIC33E/PIC24E devices.
File
flash_helper.c
#include "stdio.h"
#include "libpic30.h"
int main() {
int i;
_prog_addressT p;
/* method 1 */
_init_prog_address(p, dat);
(void) _memcpy_p2d16(buf, p, 10);
display_mem(buf,10);
/* method 2 */
_init_prog_address(p, dat);
p = _memcpy_p2d16(buf, p, 4);
p = _memcpy_p2d16(&buf[4], p, 6);
display_mem(buf,10);
}
Return Value
N/A
Remarks
None.
Default Behavior
Initialize variable to specified value.
File
libpic30.c
Return Value
The memory block pointed to by dest.
Remarks
__eds__ pointers are superset of unqualified data pointers; therefore these functions can be used to copy between
__eds__ and unqualified data memory.
Default Behavior
Copy len bytes of data from each address pointed to by the src pointer to the destination pointed to by the dest
pointer.
File
memcpy_helper.s
Return Value
The next unused source address.
Remarks
None.
Default Behavior
Copy 16 bits of data from each of the len bytes of addresses of src to the destination pointed to by the dest
pointer.
File
memcpy_helper.s
Return Value
The next unused source address.
Remarks
None.
Default Behavior
Copy 24 bits of data from each of the len bytes of addresses of src to the destination pointed to by the dest
pointer.
File
memcpy_helper.s
Return Value
The memory block pointed to by dest.
Remarks
None.
Default Behavior
Copy len bytes of data from each address pointed to by the src pointer to the destination pointed to by the dest
pointer.
File
memcpy_helper.s
Return Value
The string pointed to by dest.
Remarks
__eds__ pointers are superset of unqualified data pointers; therefore these functions can be used to copy between
__eds__ and unqualified data memory.
Default Behavior
Copy the string src pointer to the destination pointed to by the dest pointer.
File
memcpy_helper.s
Return Value
The string pointed to by dest.
Remarks
None.
Default Behavior
Copy the string pointed to by src to the RAM buffer pointed to by dest. Unlike the standard strncpy function, this
function does not zero fill the remaining space in the destination string.
File
memcpy_helper.s
Copies at most len bytes string from one __eds__ buffer to another __eds__ buffer.
Include
<libpic30.h>
Prototype
__eds__ char* _strncpy_eds(__eds__ char *dest, __eds__ char *src, unsigned int len);
Arguments
Return Value
The string pointed to by dest.
Remarks
__eds__ pointers are superset of unqualified data pointers; therefore these functions can be used to copy between
__eds__ and unqualified data memory.
Default Behavior
Copy len bytes of the string pointed to by the src pointer to the destination pointed to by the dest pointer. Unlike
the standard strncpy function, this function does not zero fill the remaining space in the destination string.
File
memcpy_helper.s
Return Value
The next unused source address.
Remarks
None.
Default Behavior
Copy 16 bits of data from each of the len bytes of addresses of src to the destination pointed to by the dest
pointer.
File
memcpy_helper.s
Prototype
_prog_addressT _strncpy_p2d24(char *dest, _prog_addressT src, unsigned int len);
Arguments
Return Value
The next unused source address.
Remarks
None.
Default Behavior
Copy 24 bits of data from each of the len bytes of addresses of src to the destination pointed to by the dest
pointer.
File
memcpy_helper.s
Return Value
The string pointed to by dest.
Remarks
None.
Default Behavior
Copy at most len bytes of the string pointed to by src to the RAM buffer pointed to by dest. Unlike the standard
strncpy function, this function does not zero fill the remaining space in the destination string.
File
memcpy_helper.s
#include <libpic30.h>
//_program_slave(core#, verify, &slave_image)
if (_program_slave(l, 0, &slave_image) == 0)
{
/* now verify */
if (_program_slave(l, 1, &slave_image) == ESLV_VERIFY_FAIL)
{
asm(“reset”) ; // try again
}
}
#include <libpic30.h>
int main()
{
// Master intialization code
_start_slave(); // Start Slave core
while(1);
}
slave_number Identifier of the slave core to be programmed. The identifier for the first slave core is 1.
verify A 0 will load the entire Slave image to the PRAM. A 1 will verify the entire Slave image in the
PRAM.
*image Pointer to the Slave image to be programmed into PRAM.
Slave PRAM images not following the Microchip language tool format will require a custom
routine.
Return Value
• ESLV_INVALID - returned if an invalid slave number is given
• ESLV_BAD_IMAGE - returned if there is an error decoding the slave image
• ESLV_VERIFY_FAIL - returned if 'verify' fails
Remarks
None.
Default Behavior
The __program_slave routine uses the verify parameter as a switch to either load or verify the Slave image
using the LDSLV or VFSLV instructions.
File
program_slave.c
slave_number Identifier of the slave core to be programmed. The identifier for the first slave core is 1.
verify A 0 will load the entire Slave image to the PRAM. A 1 will verify the entire Slave image in the
PRAM.
*image Pointer to the Slave image to be programmed into PRAM.
Slave PRAM images not following the Microchip language tool format will require a custom
routine.
Return Value
• ESLV_INVALID - returned if an invalid slave number is given
• ESLV_BAD_IMAGE - returned if there is an error decoding the slave image
• ESLV_VERIFY_FAIL - returned if 'verify' fails
Remarks
The Master loads the PRAM Inactive Partition while the Slave is running and then the Slave executes the BOOTSWP
instruction to swap partitions.
Default Behavior
The __program_inactive_slave routine uses the verify parameter as a switch to either load or verify the
Slave image using the LDSLV or VFSLV instructions.
File
program_inactive_slave.c
Return Value
None.
Remarks
None.
Default Behavior
The __start_slave and __stop_slave routines perform the MSl1KEY unlock sequence and set or clear the
SLVEN bit (MSl1CON[15]) respectively.
File
start_slave.c
Bit Position
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
-20 2-1 2-2 2-3 2-4 2-5 2-6 2-7 2-8 2-9 2-10 2-11 2-12 2-13 2-14 2-15
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
or 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768
-1
Implied Radix (Decimal) Point
Bit Value
The following table shows the conversion of a two’s complement 16-bit integer +24576 to Q15 value +0.75.
Table 5-1. Table1: Conversion of a Two’s Complement 16-Bit Integer to Q15
= Radix Point
Q15.16 Format
In the Q15.16 format, the Most Significant bit is defined as a sign bit followed by 16 bits of the integral part. The radix
point is implied to lie just after the integral part, followed by 16 bits of the fractional value. This format is commonly
referred to as Q15.16 format. The range of Q15.16 numbers is from -32768.0 (0x8000 0000) to
+32767.9999847412109375 (0x7FFF FFFF) and has a precision of 2-16.
Figure 4: Fractional Format (32 bits)
The following table shows the conversion of a two’s complement 32-bit integer, -715827882 to Q15.16 value
-10922.6666564941.
Table 5-2. Table 2: Conversion of a Two’s Complement 32-Bit Integer to Q15.16
0 0 x 215 0 0 x 2-1 0
1 1x 214 16384 1x 2-2 0.25
0 0 x 213 0 0 x 2-3 0
1 0 x 212 4096 1 x 2-4 0.0625
0 0x 211 0 0x 2-5 0
1 1 x 210 1024 1 x 2-6 0.015625
...........continued
Binary Dec Q15.16
0 0 x 29 0 0 x 2-7 0
1 1x 28 256 1x 2-8 0.00390625
0 0 x 27 0 0 x 2-9 0
1 1 x 26 64 1 x 2-10 0.000976563
0 0x 25 0 0x 2-11 0
1 1 x 24 16 1 x 2-12 0.000244141
0 0 x 23 0 0 x 2-13 0
1 1x 22 4 1x 2-14 6.10352E-05
1 1 x 21 2 1 x 2-15 3.01576E-05
0 0x 20 0 0x 2-16 0
SUM -715827882 SUM -10922.6666564941
= Radix Point
A similar relationship exists between the 32-bit integer format and the Q15.16 format, where the integer range
[-2147483648, +2147483647] is mapped to the Q15.16 range [-32768.0, +32767.9999847412109375].
Figure 6: Mapping between 32-bit integer format and Q15.16 format
The standard headers can be included in any order. Do not include a standard header within a declaration. Do not
define macros that have the same names as keywords before including a standard header.
A standard header never includes another standard header.
Library Files
The archived library files contain all the individual object files for each library function.
When linking an application, the library file (libq-omf.a or libq-dsp-omf.a) must be provided as an input to the
linker (using the --library or -l linker option), such that the functions used by the application may be linked into
the application. Also, linker options -lq and -lq-dsp must be used when linking the respective libraries.
A typical C application will require three library files: libc-omf.a, libm-omf.a and libpic30-omf.a (see
“OMF-Specific Libraries and Startup Modules” for more on OMF-specific libraries). These libraries will be included
automatically if linking is performed using the compiler.
Function Naming Conventions
Signed fixed-point types are defined as follows:
Qn_m
where:
• n is the number of data bits to the left of the radix point
• m is the number of data bits to the right of the radix point
Note: A sign bit is implied.
For convenience, short names are also defined:
In this document, the terms Q15.16 and Q16 are used interchangeably; however, both imply Q15.16 format.
Functions in the library are prefixed with the type of the return value. For example, _Q15acos returns a Q15 value
equal to the arc cosine of its argument.
Argument types do not always match the return type. Refer to the function prototype for a specification of its
arguments. In cases where the return value is not a fixed-point type, the argument type is appended to the function
name. For example, function _itoaQ15 accepts a type Q15 argument.
In cases where two versions of a function are provided with the same return type but different argument types, the
argument type is appended to the function name. For example:
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the absolute value of x in Q15 format. The value ranges from 0 to 32767.
Note: _Q15abs(-32768) = 32767.
Also abs (smallest negative number) = largest positive number.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. Therefore the value of this
argument ranges from 17705 to 32767.
Return Value
This function returns the arc cosine of x in Q15 format. The value ranges from 256 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the arc cosine of x, divided by PI, in Q15 format. The value ranges from 82 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
y a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the sum of x and y in Q15 format. The value ranges from -32768 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -27573 to 27573.
Return Value
This function returns the arc sine of x in Q15 format. The value ranges from -32768 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the arc sine of x, divided by PI, in Q15 format. The value ranges from -16384 to 16303.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the arc tangent of x in Q15 format. The value ranges from -25736 to 25735.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the arc tangent of x, divided by PI, in Q15 format. The value ranges from -8192 to 8192.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
y a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the arc tangent of y divided by x in Q15 format. The value ranges from -25736 to 25735.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
y a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the arc tangent of y divided by x, divided by PI, in Q15 format. The value ranges from -8192 to
8192.
Return Value
This function returns the integer equivalent of s in Q15 format, which range is from -32768 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the cosine of x in Q15 format. The value ranges from 17705 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the cosine of PI times x in Q15 format. The value ranges from -32768 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 0.
Return Value
This function returns the exponent value of x in Q15 format. The value ranges from 12055 to 32767.
Return Value
This function returns a fixed-point number in Q15 format. The value ranges from -32768 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this
argument ranges from -32768 to 32767.
s a buffer holding values in ASCII, at least 8 characters long.
Return Value
None.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns a floating-point equivalent number. The corresponding floating-point range is -1 to 0.99996.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from 12055 to 32767.
Return Value
This function returns the natural log of x in Q15 format. The value ranges from -32768 to -1.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from 3277 to 32767.
Return Value
This function returns the log of x in Q15 format. The value ranges from -32768 to 0.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns -x in Q15 format. The value ranges from -32768 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the square root of x in Q15 format. The value ranges from 16384 to -32767 for a positive
number and -32768 to -16384 for a negative number.
x a fixed-point number in Q15 format, which ranges from 1 to 215-1. The value of this argument
ranges from 1 to 32767.
y a fixed-point number in Q15 format, which ranges from 1 to 215-1. The value of this argument
ranges from 1 to 32767.
Return Value
This function returns x to the power of y in Q15 format. The value ranges from 1 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
num an integer number, which ranges from -15 to 15.
Return Value
This function returns the shifted value of x in Q15 format. The value ranges from -32768 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
num an integer number, which ranges from -15 to 15.
Return Value
This function returns the shifted value of x in Q15 format. The value ranges from -32768 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
num an integer number, which ranges from -15 to 15.
Return Value
This function returns the shifted value of x in Q15 format. The value ranges from -32768 to 32767.
This function shifts a Q15 value by num bits, to the right if num is positive or to the left if num is negative. The function
sets the _Q15shrSatFlag variable in case of underflow or overflow but does not take care of saturation.
Include
<libq.h>
Prototype
_Q15 _Q15shrNoSat(_Q15 x, short num);
Arguments
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
num an integer number, which ranges from -15 to 15.
Return Value
This function returns the shifted value of x in Q15 format. The value ranges from -32768 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the sine of x in Q15 format. The value ranges from -27573 to 27573.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the sine of PI times x in Q15 format. The value ranges from -32768 to 32767.
f a fixed-point number in Q15 format, which ranges from 0 to (231-1). The valid range of values for this
argument is from -16384 to 16384. The argument represents the Normalizing frequency.
start a fixed-point number in Q16 format, which ranges from 0 to (231-1). The valid range of values for this
argument is from 1 to 32767. This argument represents the Starting Sample number in the Sine Series.
num a fixed-point number in Q16 format, which ranges from 0 to (231-1). The valid range of values for this
argument is from 1 to 32767. This argument represents the Number of Sine Samples the function is called
to generate. Note: num should not be more than 16383 for dsPIC and 32767 for PIC devices.
buf a pointer to the buffer where the generated sine samples would get copied into.
Return Value
This function returns num, the number of generated sine samples.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from 1 to 32767.
Return Value
This function returns the square root of x in Q15 format. The value ranges from 1 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
y a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns x minus y in Q15 format. The value ranges from -32768 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -25736 to 25735.
Return Value
This function returns the tangent of x in Q15 format. The value ranges from -32768 to 32767.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the tangent of PI times x in Q15 format. The value ranges from -32768 to 32767.
x a fixed-point number in Q16 format. The value of this argument ranges from -65536 to 65535.
Return Value
This function returns the arc cosine of x in Q16 format. The value ranges from 205887 to 363 respectively.
x a fixed-point number in Q16 format. The value of this argument ranges from -65536 to 65535.
Return Value
This function returns the arc cosine of x, divided by PI, in Q16 format. The value ranges from 65536 to 115
respectively.
x a fixed-point number in Q16 format. The value of this argument ranges from -65536 to 65535.
Return Value
This function returns the arc sine of x in Q16 format. The value ranges from -102944 to 102579 respectively.
Argument
x a fixed-point number in Q16 format. The value of this argument ranges from -65536 to 65535.
Return Value
This function returns the arc sine of x, divided by PI, in Q16 format. The value ranges from -32768 to 32653
respectively.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the arc tangent of x in Q16 format. The value ranges from -2147483648 to 2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the arc tangent of x, divided by PI, in Q16 format. The value ranges from -2147483648 to
2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
y a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the arc tangent of y divided by x in Q16 format. The value ranges from -2147483648 to
2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
y a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the arc tangent of y divided by x, divided by PI, in Q16 format. The value ranges from
-2147483648 to 2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the cosine of x in Q16 format. The value ranges from -65536 to 65535.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the cosine of PI times x in Q16 format. The value ranges from -65536 to 65535.
dividend a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
divisor a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
Return Value
This function returns the quotient of its arguments. The value ranges from 0 to 2147483647.
dividend a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
divisor a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
remainder a pointer to an object large enough to hold the remainder. This must be provided by the caller.
Return Value
This function returns the quotient and remainder of its arguments. The values range from 0 to 2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -772244 to 681391.
Return Value
This function returns the exponent value of x in Q16 format. The value ranges from 0 to 2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -32768 to 32768.
Return Value
This function returns a fixed-point number in Q16 format. The value ranges from -2147483648 to 2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns a floating-point equivalent number. The corresponding floating-point range is -32768 to 32768.
x a fixed-point number in Q16 format. The value of this argument ranges from 1 to 2147483647.
Return Value
This function returns the natural log of x in Q16 format. The value ranges from -726817 to 681391.
Argument
x a fixed-point number in Q16 format. The value of this argument ranges from 1 to 2147483647.
Return Value
This function returns the log of x in Q16 format. The value ranges from -315653 to 295925.
x a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
y a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
prod a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
Return Value
This function returns the multiplied and accumulated value prod in Q16 format. The value ranges from 0 to
2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
y a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
prod a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
Return Value
This function returns the multiplied and accumulated value prod in Q16 format. The value ranges from 0 to
2147483647.
Arguments
a a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
b a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
Return Value
This function returns the product of its arguments. The value ranges from 0 to 2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns -x in Q16 format. The value ranges from -2147483648 to 2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the square root of x in Q16 format. The value ranges from 16384 to -32767 for a positive
number and -2147483648 to -1073741824 for a negative number.
x a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
y a fixed-point number in Q16 format. The value of this argument ranges from 0 to 2147483647.
Return Value
This function returns x to the power of y in Q16 format. The value ranges from 0 to 2147483647.
x a fixed-point number in Q15 format, which ranges from -215 to 215-1. The value of this argument
ranges from -32768 to 32767.
Return Value
This function returns the reciprocal of x in Q16 format. The value ranges from -2147483648 to 2147418112.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the reciprocal of x in Q16 format. The value of this output ranges from -2147483648 to
2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
y an integer number, which ranges from -32 to 32.
Return Value
This function returns the shifted value of x in Q16 format. The value ranges from -2147483648 to 2147483647.
x a fixed-point number in Q16 format, which ranges from -216 to 216-1. The value of this
argument ranges from -2147483648 to 2147483647.
Y an integer number, which ranges from -32 to 32.
Return Value
This function returns the shifted value of x in Q16 format. The value ranges from -2147483648 to 2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
y an integer number, which ranges from -32 to 32.
Return Value
This function returns the shifted value of x in Q16 format. The value ranges from -2147483648 to 2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
y an integer number, which ranges from -32 to 32.
Return Value
This function returns the shifted value of x in Q16 format. The value ranges from -2147483648 to 2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the sine of x in Q16 format. The value ranges from -65566 to 65565.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the sine of PI times x in Q16 format. The value ranges from -65536 to 65535.
f a fixed-point number in Q16 format, which ranges from 0 to (231-1). The valid range of values for this
argument is from -32768 to 32768. The argument represents the Normalizing frequency.
start a fixed-point number in Q16 format, which ranges from 0 to (231-1). The valid range of values for this
argument is from 1 to 32767. This argument represents the Starting Sample number in the Sine Series.
num a fixed-point number in Q16 format, which ranges from 0 to (231-1). The valid range of values for this
argument is from 1 to 32767. This argument represents the Number of Sine Samples the function is called
to generate. Note: num should not be more than 16383 for dsPIC and 32767 for PIC devices.
buf a pointer to the buffer where the generated sine samples would get copied into.
Return Value
This function returns num, the number of generated sine samples.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the tangent of x in Q16 format. The value ranges from -2147483648 to 2147483647.
x a fixed-point number in Q16 format. The value of this argument ranges from -2147483648 to
2147483647.
Return Value
This function returns the tangent of PI times x in Q16 format. The value ranges from -2147483648 to 2147483647.
6. Revision History
The following is a list of changes by version to this document.
Note: Some revision letters are not used - the letters I and O - as they can be confused for numbers in some fonts.
• USHRT_MAX
Section 2.8 “<setjmp.h> Non-Local Jumps” - the following function descriptions have been updated:
• jmp_buf
• longjmp
• SIGILL
• SIGSEGV
• raise
• signal
Divided Chapter 3. “Standard C Libraries - Math Functions” into Chapter 4. “Standard C Libraries - Support
Functions” and Chapter 5. “Fixed-Point Math Functions”
Minor typographical changes.
– clearerr
– feof
– fgetpos
– fopen
– fprintf
– fread
• Minor typographical changes
– _builtin_mulss
– _builtin_mulsu
– _builtin_mulus
– _builtin_muluu
– _builtin_nop
– _builtin_psvpage
• Minor typographical changes
Customer Support
Users of Microchip products can receive assistance through several channels:
• Distributor or Representative
• Local Sales Office
• Embedded Solutions Engineer (ESE)
• Technical Support
Customers should contact their distributor, representative or ESE for support. Local sales offices are also available to
help customers. A listing of sales offices and locations is included in this document.
Technical support is available through the website at: www.microchip.com/support
Legal Notice
Information contained in this publication regarding device applications and the like is provided only for your
convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with
Trademarks
The Microchip name and logo, the Microchip logo, Adaptec, AnyRate, AVR, AVR logo, AVR Freaks, BesTime,
BitCloud, chipKIT, chipKIT logo, CryptoMemory, CryptoRF, dsPIC, FlashFlex, flexPWR, HELDO, IGLOO, JukeBlox,
KeeLoq, Kleer, LANCheck, LinkMD, maXStylus, maXTouch, MediaLB, megaAVR, Microsemi, Microsemi logo, MOST,
MOST logo, MPLAB, OptoLyzer, PackeTime, PIC, picoPower, PICSTART, PIC32 logo, PolarFire, Prochip Designer,
QTouch, SAM-BA, SenGenuity, SpyNIC, SST, SST Logo, SuperFlash, Symmetricom, SyncServer, Tachyon,
TempTrackr, TimeSource, tinyAVR, UNI/O, Vectron, and XMEGA are registered trademarks of Microchip Technology
Incorporated in the U.S.A. and other countries.
APT, ClockWorks, The Embedded Control Solutions Company, EtherSynch, FlashTec, Hyper Speed Control,
HyperLight Load, IntelliMOS, Libero, motorBench, mTouch, Powermite 3, Precision Edge, ProASIC, ProASIC Plus,
ProASIC Plus logo, Quiet-Wire, SmartFusion, SyncWorld, Temux, TimeCesium, TimeHub, TimePictra, TimeProvider,
Vite, WinPath, and ZL are registered trademarks of Microchip Technology Incorporated in the U.S.A.
Adjacent Key Suppression, AKS, Analog-for-the-Digital Age, Any Capacitor, AnyIn, AnyOut, BlueSky, BodyCom,
CodeGuard, CryptoAuthentication, CryptoAutomotive, CryptoCompanion, CryptoController, dsPICDEM,
dsPICDEM.net, Dynamic Average Matching, DAM, ECAN, EtherGREEN, In-Circuit Serial Programming, ICSP,
INICnet, Inter-Chip Connectivity, JitterBlocker, KleerNet, KleerNet logo, memBrain, Mindi, MiWi, MPASM, MPF,
MPLAB Certified logo, MPLIB, MPLINK, MultiTRAK, NetDetach, Omniscient Code Generation, PICDEM,
PICDEM.net, PICkit, PICtail, PowerSmart, PureSilicon, QMatrix, REAL ICE, Ripple Blocker, SAM-ICE, Serial Quad
I/O, SMART-I.S., SQI, SuperSwitcher, SuperSwitcher II, Total Endurance, TSHARC, USBCheck, VariSense,
ViewSpan, WiperLock, Wireless DNA, and ZENA are trademarks of Microchip Technology Incorporated in the U.S.A.
and other countries.
SQTP is a service mark of Microchip Technology Incorporated in the U.S.A.
The Adaptec logo, Frequency on Demand, Silicon Storage Technology, and Symmcom are registered trademarks of
Microchip Technology Inc. in other countries.
GestIC is a registered trademark of Microchip Technology Germany II GmbH & Co. KG, a subsidiary of Microchip
Technology Inc., in other countries.
All other trademarks mentioned herein are property of their respective companies.
© 2019, Microchip Technology Incorporated, Printed in the U.S.A., All Rights Reserved.
ISBN: 978-1-5224-6445-7