SW006013 Microchip Technology, SW006013 Datasheet

MPLAB C Compiler For DsPIC DSCs

SW006013

Manufacturer Part Number
SW006013
Description
MPLAB C Compiler For DsPIC DSCs
Manufacturer
Microchip Technology
Series
DsPIC30F/33Fr
Type
MPLAB® C Compilerr
Datasheet

Specifications of SW006013

Supported Families
DsPIC30F, DsPIC33F
Core Architecture
DsPIC
Kit Contents
Software And Docs
Mcu Supported Families
DsPIC30F And DsPIC33F
Tool Function
Compiler
Tool Type
Compiler
Processor Series
dsPIC
Lead Free Status / RoHS Status
na
For Use With/related Products
dSPIC DSCs
Lead Free Status / Rohs Status
Lead free / RoHS Compliant
16-BIT LANGUAGE TOOLS
LIBRARIES
© 2008 Microchip Technology Inc.
DS51456E

Related parts for SW006013

SW006013 Summary of contents

Page 1

... LANGUAGE TOOLS © 2008 Microchip Technology Inc. LIBRARIES DS51456E ...

Page 2

... PowerMate, PowerTool, REAL ICE, rfLAB, Select Mode, Total Endurance, UNI/O, WiperLock 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. All other trademarks mentioned herein are property of their respective companies. ...

Page 3

... Chapter 3. Standard C Libraries - Math Functions 3.1 Introduction ................................................................................................. 145 3.2 Using the Standard C Libraries .................................................................. 145 3.3 <math.h> mathematical functions .............................................................. 147 © 2008 Microchip Technology Inc. 16-BIT LANGUAGE TOOLS Table of Contents LIBRARIES DS51456E-page iii ...

Page 4

... Functions for Specialized Copying and Initialization .................................. 203 Chapter 5. Fixed Point Math Functions 5.1 Introduction ................................................................................................. 207 5.2 Using the Fixed Point Libraries .................................................................. 207 5.3 <libq.h> mathematical functions ................................................................. 209 Appendix A. ASCII Character Set .............................................................................227 Index ...........................................................................................................................229 Worldwide Sales and Service ...................................................................................242 DS51456E-page iv © 2008 Microchip Technology Inc. ...

Page 5

... Chapter 3: Standard C Libraries - Math Functions – lists the math functions for standard C operation. • Chapter 4: Standard C Libraries - Support Functions – lists standard C library helper functions. • Appendix A: ASCII Character Set © 2008 Microchip Technology Inc. 16-BIT LANGUAGE TOOLS Preface NOTICE TO CUSTOMERS LIBRARIES ® ...

Page 6

... Optional arguments mpasmwin [options] file [options] Choice of mutually exclusive errorlevel {0|1} arguments selection Replaces repeated text var_name [, var_name...] Represents code supplied by void main (void) user { ... } © 2008 Microchip Technology Inc. Examples ®® IDE User’s Guide ...

Page 7

... Indianapolis, Indiana 46268. Plauger, P.J., The Standard C Library, Prentice-Hall, Englewood Cliffs, N.J. 07632. Van Sickle, Ted., Programming Microcontrollers in C, First Edition. LLH Technology Publishing, Eagle Rock, Virginia 24085. © 2008 Microchip Technology Inc. ® DSCs ® DSCs User’s Guide (DS51284) ...

Page 8

... MPLAB SIM simulator, as well as general editing and debugging features. • Programmers – The latest information on Microchip programmers. These include the MPLAB PM3 and PRO MATE Plus and PICkit 1 and 2 development programmers. DS51456E-page 4 ® C compilers; all MPLAB ® II device programmers and the PICSTART © 2008 Microchip Technology Inc. ® ® ...

Page 9

... Customers should contact their distributor, representative or field application engineer (FAE) for support. Local sales offices are also available to help customers. A listing of sales offices and locations is included in the back of this document. Technical support is available through the web site at: http://support.microchip.com © 2008 Microchip Technology Inc. DS51456E-page 5 ...

Page 10

... NOTES: DS51456E-page 6 © 2008 Microchip Technology Inc. ...

Page 11

... DSP Library • 16-Bit Peripheral Libraries • Standard C Libraries with Math and Support Functions • Fixed Point Math Functions • Compiler Built-in Functions © 2008 Microchip Technology Inc. 16-BIT LANGUAGE TOOLS LIBRARIES ® DSCs (formerly MPLAB C30). subdirectory of the MPLAB C lib ® ...

Page 12

... Device is the 16-bit device number (e.g., Device-omf .a for the dsPIC30F6014 device) and omf is either for all tools. omf or -omf= (either or crt0.eo DSCs User’s Guide” (DS51284 coff elf © 2008 Microchip Technology Inc. ...

Page 13

... The MPLAB C Compiler for PIC24 MCUs and dsPIC that, to the developer, work like library functions. These functions are listed in the ® “MPLAB C Compiler for PIC24 MCUs and dsPIC © 2008 Microchip Technology Inc. (written by Dinkumware, an industry leader) and omf .a omf libm- ...

Page 14

... NOTES: DS51456E-page 10 © 2008 Microchip Technology Inc. ...

Page 15

... Microchip Technology Inc. 16-BIT LANGUAGE TOOLS depending upon the selected object module format. c:\Program Files\Microchip\MPLAB C30 – header files for libraries , which contains diagrams of resources ResourceGraphs ...

Page 16

... Section 1.2 “OMF-Specific Libraries/Start-up Modules” for .a ® Assembler, Linker and Utilities for PIC24 MCUs and dsPIC ® DSCs User’s Guide” (DS51284) for more information and omf omf libc- .a libm- .a ® ® C Compiler for PIC24 © 2008 Microchip Technology Inc. ...

Page 17

... Description: Include: Prototype: Argument: Remarks: Example: © 2008 Microchip Technology Inc. consists of a single macro that is useful for debugging logic assert the macro <assert.h> NDEBUG If the expression is false, an assertion message is printed to stderr and the program is aborted. <assert.h> ...

Page 18

... NOT an alphanumeric\n"); } Output alphanumeric # is NOT an alphanumeric Test for an alphabetic character. <ctype.h> int isalpha(int c); The character to test. c Returns a non-zero integer value if the character is alphabetic; otherwise, returns zero. Alphabetic characters are included within the ranges A-Z or a-z. © 2008 Microchip Technology Inc. ...

Page 19

... Example: iscntrl Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. #include <ctype.h> /* for isalpha */ #include <stdio.h> /* for printf */ int main(void) { int 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"); ...

Page 20

... Returns a non-zero integer value if the character is a graphical charac- ter; otherwise, returns zero. A character is considered graphical character any print- able character except a space. #include <ctype.h> /* for isgraph */ #include <stdio.h> /* for printf */ int main(void) { int ch; © 2008 Microchip Technology Inc. ...

Page 21

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc '3'; if (isgraph(ch)) printf(" graphical character\n"); else printf("3 is NOT a graphical character\n"); ch = '#'; if (isgraph(ch)) printf(" graphical character\n"); else printf("# is NOT a graphical character\n"); (isgraph(ch)) printf(" ...

Page 22

... Returns a non-zero integer value if the character is a punctuation char- acter; otherwise, returns zero. A character is considered punctuation character print- able character which is neither a space nor an alphanumeric character. Punctuation characters consist of the following: ! " & < = > © 2008 Microchip Technology Inc. ...

Page 23

... Example: isspace Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. #include <ctype.h> /* for ispunct */ #include <stdio.h> /* for printf */ int main(void) { int ch '&'; if (ispunct(ch)) printf("& punctuation character\n"); else printf("& is NOT a punctuation character\n"); ...

Page 24

... A character is considered hexadecimal digit character 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. © 2008 Microchip Technology Inc. ...

Page 25

... Example: tolower Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. #include <ctype.h> /* for isxdigit */ #include <stdio.h> /* for printf */ int main(void) { int ch 'B'; if (isxdigit(ch)) printf(" hexadecimal digit\n"); else printf("B is NOT a hexadecimal digit\n"); ch = 't'; ...

Page 26

... Output: b changes to upper case B B remains upper case B @ has no upper case returned © 2008 Microchip Technology Inc. ...

Page 27

... ERANGE Description: Include: Remarks: errno Description: Include: Remarks: © 2008 Microchip Technology Inc. consists of macros that provide error codes that are reported errno.h immediately before calling the library func- errno is zero. Library functions will never set errno Represents a domain error. <errno.h> ...

Page 28

... Maximum finite double precision floating-point value <float.h> 3.402823e+38 by default, 1.797693e+308 if the switch -fno-short-double is used By default, a double type is the same size as a float type (32-bit repre- sentation). The -fno-short-double switch allows the IEEE 64-bit representation to be used for a double precision floating-point value. © 2008 Microchip Technology Inc. ...

Page 29

... Remarks: DBL_MIN_10_EXP Description: Include: Value: Remarks: © 2008 Microchip Technology Inc. Maximum integer value for a double precision floating-point exponent in base 10 <float.h> default, 308 if the switch -fno-short-double is used By default, a double type is the same size as a float type (32-bit repre- sentation). The -fno-short-double switch allows the IEEE 64-bit representation to be used for a double precision floating-point value ...

Page 30

... The difference between 1.0 and the next larger representable single precision floating-point value <float.h> 1.192093e-07 Number of base-FLT_RADIX digits in a single precision floating-point significand <float.h> 24 Maximum finite single precision floating-point value <float.h> 3.402823e+38 Maximum integer value for a single precision floating-point exponent in base 10 <float.h> 38 © 2008 Microchip Technology Inc. ...

Page 31

... FLT_ROUNDS Description: Include: Value: Remarks: LDBL_DIG Description: Include: Value: © 2008 Microchip Technology Inc. Maximum integer value for a single precision floating-point exponent in base FLT_RADIX <float.h> 128 Minimum single precision floating-point value <float.h> 1.175494e-38 Minimum negative integer value for a single precision floating-point exponent in base 10 < ...

Page 32

... Maximum integer value for a long double precision floating-point expo- nent in base FLT_RADIX <float.h> 1024 Minimum long double precision floating-point value <float.h> 2.225074e-308 Minimum negative integer value for a long double precision float- ing-point exponent in base 10 <float.h> -307 © 2008 Microchip Technology Inc. ...

Page 33

... Description: Include: Value: LLONG_MAX Description: Include: Value: © 2008 Microchip Technology Inc. Minimum negative integer value for a long double precision float- ing-point exponent in base FLT_RADIX <float.h> -1021 consists of macros that define the minimum and maximum limits.h Number of bits to represent type char < ...

Page 34

... Maximum number of bytes in a multibyte character <limits.h> 1 Maximum value of a signed char <limits.h> 127 Minimum value of a signed char <limits.h> -128 Maximum value of a short int <limits.h> 32767 Minimum value of a short int <limits.h> -32768 © 2008 Microchip Technology Inc. ...

Page 35

... LC_ALL • LC_COLLATE • LC_CTYPE • LC_MONETARY • LC_NUMERIC • LC_TIME • localeconv • setlocale © 2008 Microchip Technology Inc. Maximum value of an unsigned char <limits.h> 255 Maximum value of an unsigned int <limits.h> 65535 Maximum value of a long long unsigned int < ...

Page 36

... 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. © 2008 Microchip Technology Inc. ...

Page 37

... Description: Include: SIG_ERR Description: Include: SIG_IGN Description: Include: © 2008 Microchip Technology Inc. consists of a type, several macros and two functions that signal.h function. raise ); the signal is treated as a fatal error and execution SIG_DFL ); the signal is ignored and control is returned to the SIG_IGN is an integer type that the program access atomically ...

Page 38

... See signal for an exam- ple of a user defined function. #include <signal.h> /* for raise, SIGFPE */ #include <stdio.h> /* for printf */ int main(void) { raise(SIGFPE); printf("Program never reaches here"); } Output: FPE Explanation: FPE stands for “floating-point error”. © 2008 Microchip Technology Inc. ...

Page 39

... SIGINT Description: Include: Prototype: Remarks: Example: © 2008 Microchip Technology Inc. Signals illegal instruction. <signal.h> #define SIGILL SIGILL is used as an argument for raise and/or signal. When used, the default behavior is to print an invalid executable code mes- sage and terminate the calling program. This may be overridden by a user function that defines the signal handler actions ...

Page 40

... See signal for an example of a user defined function. #include <signal.h> /* for raise, SIGTERM */ #include <stdio.h> /* for printf */ int main(void) { raise(SIGTERM); printf("Program never reaches here."); } Output: TERM Explanation: TERM stands for “termination request”. © 2008 Microchip Technology Inc. ...

Page 41

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Reports a synchronous signal. <signal.h> int raise(int sig); sig signal name Returns successful; otherwise, returns a non-zero value. raise sends the signal identified by sig to the executing program. #include <signal.h> ...

Page 42

... 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. © 2008 Microchip Technology Inc. ...

Page 43

... Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. supports functions with variable argument lists. This allows stdarg.h must be declared inside the function to hold the va_list will initialize the variable to an argument list, will end the use of the argument. ...

Page 44

... Output: <83> integer <This is text.> string . is unknown % unknown format © 2008 Microchip Technology Inc. ...

Page 45

... Include: wchar_t Description: Include: NULL Description: Include: © 2008 Microchip Technology Inc. Ends the use of ap. <stdarg.h> #define va_end(va_list ap) pointer to list of arguments ap 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 ...

Page 46

... This program shows the offset in bytes of each structure member from the start of the structure. Although item1 is only 5 bytes (char item1[5]), padding is added so the address of item2 falls on an even boundary. The same occurs with item3 byte (char item3) with 1 byte of padding. © 2008 Microchip Technology Inc. ...

Page 47

... In the event that used with a floating-point format, the format letter will appear as literal text and its corresponding argument will not be consumed. © 2008 Microchip Technology Inc. consists of types, macros and functions that provide support stdio.h ...

Page 48

... The sources for these libraries are provided for you to FILE , by rewriting to return a new handle to be recognized by open appropriate another peripheral by writing the correct code to talk to the type read() write() open stdout to compare full strcmp © 2008 Microchip Technology Inc. ...

Page 49

... STDIO Functions FILE Description: Include: fpos_t Description: Include: © 2008 Microchip Technology Inc. /* code to support can1 */ /* code to support can2 */ /* code to support spi1 */ /* code to support spi2 */ Stores information for a file stream. <stdio.h> Type of a variable used to store a file position. <stdio.h> ...

Page 50

... Error condi- tions include write errors and input or read errors. Maximum number of characters in a filename including the null termi- nator. <stdio.h> 260 Defines the maximum number of files that can be simultaneously open © 2008 Microchip Technology Inc. ...

Page 51

... Remarks: NULL Description: Include: SEEK_CUR Description: Include: Example: © 2008 Microchip Technology Inc. <stdio.h> 8 stderr, stdin and stdout are included in the FOPEN_MAX count. Defines the number of characters for the longest temporary filename created by the function tmpnam. <stdio.h> 16 L_tmpnam is used to define the size of the array used by tmpnam. ...

Page 52

... See example for fseek. File pointer to the standard error stream. <stdio.h> File pointer to the standard input stream. <stdio.h> File pointer to the standard output stream. <stdio.h> The maximum number of unique filenames the function tmpnam can generate. <stdio.h> 32 © 2008 Microchip Technology Inc. ...

Page 53

... Description: Include: Prototype: Argument: Remarks: Example: © 2008 Microchip Technology Inc. Resets the error indictor for the stream <stdio.h> void clearerr(FILE *stream); stream to reset error indicators stream 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) ...

Page 54

... NULL) printf("Cannot open afile1\n"); else { printf("afile1 was opened\n"); y = fclose(myfile1 EOF) printf("afile1 was not closed\n"); else printf("afile1 was closed\n"); } } Output: afile1 was opened afile1 was closed */ © 2008 Microchip Technology Inc. ...

Page 55

... Description: Include: Prototype: Argument: Return Value: Example: © 2008 Microchip Technology Inc. Tests for end-of-file <stdio.h> int feof(FILE *stream); stream to check for end-of-file stream Returns non-zero if stream is at the end-of-file; otherwise, returns zero. #include <stdio.h> /* for feof, fgetc, fputc fopen, fclose, FILE, ...

Page 56

... Error\n"); else printf("Error indicator reset\n"); fclose(myfile Output: Error Error indicator reset */ */ */ */ */ */ */ */ © 2008 Microchip Technology Inc. ...

Page 57

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Flushes the buffer in the specified stream. <stdio.h> int fflush(FILE *stream); pointer to the stream to flush. stream Returns EOF if a write error occurs; otherwise, returns zero for suc- cess. ...

Page 58

... Output: Bytes read: program opens a file Bytes read: and reads bytes at Bytes read: program opens a file */ */ */ */ */ */ */ */ */ © 2008 Microchip Technology Inc. ...

Page 59

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Get a string from a stream <stdio.h> char *fgets(char *s, int n, FILE *stream); pointer to the storage string s maximum number of characters to read n pointer to the open stream. stream Returns a pointer to the string s if successful; otherwise, returns a null ...

Page 60

... FILE, /* NULL, EOF int main(void) { FILE *myfile1, *myfile2; int © 2008 Microchip Technology Inc. ...

Page 61

... Microchip Technology Inc. if ((myfile1 = fopen("afile1", "r")) == NULL) printf("Cannot open afile1\n"); else { printf("afile1 was opened\n"); y = fclose(myfile1 EOF) printf("afile1 was not closed\n"); else printf("afile1 was closed\n"); } if ((myfile1 = fopen("afile1", "w+")) == NULL) printf("Second try, cannot open afile1\n"); ...

Page 62

... NULL) printf("Cannot open afile\n"); else { y = fprintf(myfile, "%s %d time%c" a); printf("Number of characters printed " "to file = %d",y); fclose(myfile Output: Number of characters printed to file = 25 Contents of afile: Print this string 1 time */ */ © 2008 Microchip Technology Inc. ...

Page 63

... Example: fputs Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Puts a character to the stream. <stdio.h> int fputc(int c, FILE *stream); character to be written c pointer to the open stream stream Returns the character written or EOF if a write error occurs. ...

Page 64

... NULL) { for ( < 10; x++) { nums[x] = 10.0/(x + 1); printf("10.0/%d = %f\n", x+1, nums[x]); } numwrote = fwrite(nums, sizeof(double), 10, buf); printf("Wrote %d numbers\n\n", numwrote); fclose(buf); } else printf("Cannot open afile.out\n"); */ */ © 2008 Microchip Technology Inc. ...

Page 65

... Microchip Technology Inc. if ((buf = fopen("afile.out", "r+")) != NULL) { numread = fread(readnums, sizeof(double), 10, buf); printf("Read %d numbers\n", numread); for ( < 10; x++) { printf("% %f\n", x+1, readnums[x readnums[x]); } fclose(buf); } else printf("Cannot open afile.out\n"); } Output: 10.0/1 = 10.000000 10.0/2 = 5.000000 10.0/3 = 3.333333 10 ...

Page 66

... This program uses myfile2 to point to the stream when freopen is called 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. Scans formatted text from a stream. <stdio.h> © 2008 Microchip Technology Inc. ...

Page 67

... Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. int fscanf(FILE *stream, const char *format, ...); pointer to the open stream from which to read data stream format control string format optional arguments ... Returns the number of items successfully converted and assigned items are assigned returned ...

Page 68

... SEEK_SET); if (y) perror("Fseek failed"); else { fgets(s, 22, myfile); printf("\"%s\"\n\n", s fseek(myfile, 2L, SEEK_CUR); if (y) perror("Fseek failed"); else { fgets(s, 70, myfile); printf("\"%s\"\n\n", s © 2008 Microchip Technology Inc. ...

Page 69

... Description: Include: Prototype: Arguments: Return Value: Remarks: © 2008 Microchip Technology Inc fseek(myfile, -16L, SEEK_END); if (y) perror("Fseek failed"); else { fgets(s, 70, myfile); printf("\"%s\"\n", s); } fclose(myfile Output: "This is the beginning" "this is the middle and this is the end." ...

Page 70

... Output: Bytes read: program opens a file Bytes read: and reads bytes at Bytes read: program opens a file */ */ */ */ */ */ */ */ */ © 2008 Microchip Technology Inc. ...

Page 71

... Description: Include: Prototype: Argument: Return Value: Example: © 2008 Microchip Technology Inc. Gets the current position of a file pointer. <stdio.h> long ftell(FILE *stream); stream in which to get the current file position stream Returns the position of the file pointer if successful; otherwise, returns -1. #include < ...

Page 72

... NULL) { for ( < 10; x++) { nums[x] = 10.0/(x + 1); printf("10.0/%d = %f\n", x+1, nums[x]); } numwrote = fwrite(nums, sizeof(double), 10, buf); printf("Wrote %d numbers\n\n", numwrote); fclose(buf); } else printf("Cannot open afile.out\n"); */ */ © 2008 Microchip Technology Inc. ...

Page 73

... Microchip Technology Inc. if ((buf = fopen("afile.out", "r+")) != NULL) { numread = fread(readnums, sizeof(double), 10, buf); printf("Read %d numbers\n", numread); for ( < 10; x++) { printf("% %f\n", x+1, readnums[x readnums[x]); } fclose(buf); } else printf("Cannot open afile.out\n"); } Output: 10.0/1 = 10.000000 10.0/2 = 5.000000 10.0/3 = 3.333333 10 ...

Page 74

... FILE *buf; char y; if ((buf = fopen("afile.txt", "r")) == NULL) printf("Cannot open afile.txt\n"); else { y = getc(buf); while (y != EOF) { printf("%c|", y getc(buf); } fclose(buf Input: Contents of afile.txt (used as input): Short Longer string Output: S|h|o|r|t| |L|o|n|g|e|r| |s|t|r|i|n| © 2008 Microchip Technology Inc. ...

Page 75

... Description: Include: Prototype: Argument: Return Value: Remarks: © 2008 Microchip Technology Inc. Get a character from stdin. <stdio.h> int getchar(void); Returns the character read or EOF if a read error occurs or end-of-file is reached. Same effect as fgetc with the argument stdin. #include <stdio.h> /* for getchar, printf */ ...

Page 76

... FILE, NULL int main(void) { FILE *myfile; if ((myfile = fopen("samp.fil", "r+")) == NULL) perror("Cannot open samp.fil"); else printf("Success opening samp.fil\n"); fclose(myfile); } Output: Cannot open samp.fil: file open error */ */ © 2008 Microchip Technology Inc. ...

Page 77

... Description: Include: Prototype: Arguments: Return Value: Remarks: © 2008 Microchip Technology Inc. Prints formatted text to stdout. <stdio.h> int printf(const char *format, ...); format control string format optional arguments ... Returns number of characters generated or a negative number if an error occurs. There must be exactly the same number of arguments as there are for- mat specifiers ...

Page 78

... Print a /* second integer that is right justified character space using the pipe (|) separator between the integers. printf("%-4d|%4d\n", -4, 4); /* print a number converted to octal digits. printf("%.4o\n", 10 © 2008 Microchip Technology Inc. ...

Page 79

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. /* print a number converted to hexadecimal /* format with a 0x prefix. printf("%#x\n", 28); /* print a float in scientific notation printf("%E\n", 1.1e20); /* print a float with 2 fraction digits printf("%.2f\n", -3.346); ...

Page 80

... The function writes characters to the stream stdout. A newline char- acter is appended. The terminating null character is not written to the stream. #include <stdio.h> /* for puts */ int main(void) { char buf[] = "This is text\n"; puts(buf); puts("|"); } Output: This is text | */ © 2008 Microchip Technology Inc. ...

Page 81

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Deletes the specified file. <stdio.h> int remove(const char *filename); name of file to be deleted. filename Returns 0 if successful not. If filename does not exist or is open, remove will fail. ...

Page 82

... Output: I have 10 cookies. I ate 10 cookies © 2008 Microchip Technology Inc. ...

Page 83

... Description: Include: Prototype: Argument: Return Value: Remarks: © 2008 Microchip Technology Inc. Scans formatted text from stdin. <stdio.h> int scanf(const char *format, ...); format control string format optional arguments ... Returns the number of items successfully converted and assigned items are assigned returned. EOF is returned if an input fail- ure is encountered before the first ...

Page 84

... Contents of UartIn.txt (used as stdin input for simulator Green 300000 123-45-6789 Output: Enter your favorite number, favorite letter, favorite color, desired salary and SSN: Number of items scanned = 5 Favorite number = 5, Favorite letter = T Favorite color = Green, Desired salary = $300000.00 Social Security Number = 123-45-6789 © 2008 Microchip Technology Inc. ...

Page 85

... Description: Include: Prototype: Arguments: Remarks: Example: © 2008 Microchip Technology Inc. Defines how a stream is buffered. <stdio.h> void setbuf(FILE *stream, char *buf); pointer to the open stream stream user allocated buffer buf 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 ...

Page 86

... NULL (setvbuf(myfile2, buf, _IOFBF, sizeof(buf printf("myfile2 has a buffer of %d " "characters\n", sizeof(buf)); else printf("Unable to define buffer stream " "and/or size\n"); } fclose(myfile2); } Output: myfile1 has no buffering myfile2 has a buffer of 256 characters */ © 2008 Microchip Technology Inc. ...

Page 87

... Return Value: Remarks: Example: sscanf Description: Include: Prototype: Arguments: Return Value: Remarks: © 2008 Microchip Technology Inc. Prints formatted text to a string <stdio.h> int sprintf(char *s, const char *format, ...); storage string for output s format control string format ... optional arguments Returns the number of characters stored in s excluding the terminating null character ...

Page 88

... FILE, NULL int main(void) { FILE *mytempfile; if ((mytempfile = tmpfile()) == NULL) printf("Cannot create temporary file"); else printf("Temporary file was created"); } Output: Temporary file was created */ © 2008 Microchip Technology Inc. ...

Page 89

... Description: Include: Prototype: Argument: Return Value: Remarks: © 2008 Microchip Technology Inc. Creates a unique temporary filename <stdio.h> char *tmpnam(char *s); pointer to the temporary name s Returns a pointer to the filename generated and stores the filename can not generate a filename, the NULL pointer is returned. ...

Page 90

... NULL) printf("Cannot open afile.txt\n"); else { y = fgetc(buf); while (y != EOF 'r ungetc(y, buf EOF) { printf("2"); y = fgetc(buf printf("%c", y fgetc(buf); } fclose(buf Input: Contents of afile.txt (used as input): Short Longer string Output: Sho2rt Longe2r st2ring */ */ © 2008 Microchip Technology Inc. ...

Page 91

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Prints formatted data to a stream using a variable length argument list. <stdio.h> <stdarg.h> int vfprintf(FILE *stream, const char *format, va_list ap); pointer to the open stream stream format control string ...

Page 92

... Output: Error: The letter 'a' is not an integer value. Error: Requires 3 or more characters © 2008 Microchip Technology Inc. ...

Page 93

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Prints formatted text to a string using a variable length argument list <stdio.h> <stdarg.h> int vsprintf(char *s, const char *format, va_list ap); storage string for output s format control string format pointer to a list of arguments ...

Page 94

... EXIT_FAILURE is a value for the exit function to return an unsuccess- ful termination status See exit for example of use. Reports successful termination <stdlib.h> EXIT_SUCCESS is a value for the exit function to return a successful termination status. See exit for example of use. © 2008 Microchip Technology Inc. ...

Page 95

... Description: Include: Value: abort Description: Include: Prototype: Remarks: Example: © 2008 Microchip Technology Inc. Maximum number of characters in a multibyte character <stdlib.h> 1 The value of a null pointer constant <stdlib.h> Maximum value capable of being returned by the rand function <stdlib.h> 32767 Aborts the current process. ...

Page 96

... Returns a zero if successful; otherwise, returns a non-zero value. For the registered functions to be called, the program must terminate with the exit function call. #include <stdio.h> /* for scanf, printf */ #include <stdlib.h> /* for atexit, exit void good_msg(void); void bad_msg(void); void end_msg(void %d\n" %d\n" %d\n" © 2008 Microchip Technology Inc. ...

Page 97

... Microchip Technology Inc. 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(" ...

Page 98

... Output: String = "1.28" float = 1.280000 String = "27.835:e2" float = 2783.500000 String = "Number1" float = 0.000000 */ © 2008 Microchip Technology Inc. ...

Page 99

... Example: atol Description: Include: Prototype: Argument: Return Value: Remarks: © 2008 Microchip Technology Inc. Converts a string to an integer. <stdlib.h> int atoi(const char *s); string to be converted s Returns the converted integer if successful; otherwise, returns 0. 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 ...

Page 100

... The value returned by the compare function is < less than ce equal to ce, or > 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 com- parison function. This comp uses ascending order. */ © 2008 Microchip Technology Inc. ...

Page 101

... Example: © 2008 Microchip Technology Inc. #include <stdlib.h> /* for bsearch, qsort */ #include <stdio.h> /* for printf, sizeof */ #define NUM 7 int comp(const void *e1, const void *e2); int main(void) { int list[NUM] = {35, 47, 63, 25, 93, 16, 52}; int x, y; int *r; qsort(list, NUM, sizeof(int), comp); printf("Sorted List: " ...

Page 102

... 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 © 2008 Microchip Technology Inc. ...

Page 103

... Example: © 2008 Microchip Technology Inc. #include <stdlib.h> /* for div, div_t */ #include <stdio.h> /* for printf void __attribute__((__interrupt__)) _MathError(void) { printf("Illegal instruction executed\n"); abort(); } int main(void) { int x, y; div_t printf("For div(%d, %d)\n" div(x, y); printf("The quotient is %d and the " ...

Page 104

... FILE, NULL #include <stdlib.h> /* for exit int main(void) { FILE *myfile; if ((myfile = fopen("samp.fil", "r" NULL) { printf("Cannot open samp.fil\n"); exit(EXIT_FAILURE); } else { printf("Success opening samp.fil\n"); exit(EXIT_SUCCESS); } printf("This will not be printed"); } Output: Cannot open samp.fil */ */ © 2008 Microchip Technology Inc. ...

Page 105

... Description: Include: Prototype: Argument: Return Value: Remarks: © 2008 Microchip Technology Inc. Frees memory. <stdlib.h> void free(void *ptr); points to memory to be freed ptr Frees memory previously allocated with calloc, malloc, or real- loc. If free is used on space that has already been deallocated (by a previous call to free or by realloc space not allocated with calloc, malloc, or realloc, the behavior is undefined ...

Page 106

... Output: The absolute value of 123456 is 123456 The absolute value of -246834 is 246834 The absolute value © 2008 Microchip Technology Inc. ...

Page 107

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the quotient and remainder of two long integers. <stdlib.h> ldiv_t ldiv(long numer, long denom); numerator numer denominator denom Returns the quotient and the remainder. The returned quotient will have the same sign as the numerator divided by the denominator ...

Page 108

... NULL #include <stdlib.h> /* for malloc, free int main(void) { long *i; if ((i = (long *)malloc(50 * sizeof(long))) == NULL) printf("Cannot allocate memory\n"); else { printf("Memory allocated\n"); free(i); printf("Memory freed\n"); } } Output: Memory allocated Memory freed */ */ © 2008 Microchip Technology Inc. ...

Page 109

... Remarks: mbtowc Description: Include: Prototype: Arguments: Return Value: Remarks: © 2008 Microchip Technology Inc. Gets the length of a multibyte character. (See Remarks.) <stdlib.h> int mblen(const char *s, size_t n); points to the multibyte character s number of bytes to check n Returns zero if s points to a null character; otherwise, returns 1. ...

Page 110

... List: "); for ( < NUM; x++) printf("%d ", list[x]); } int comp(const void *e1, const void *e2) { const int * a1 = e1; const int * a2 = e2; if (*a1 < *a2) return -1; else if (*a1 == *a2) return 0; else return 1; } Output: Unsorted List Sorted List © 2008 Microchip Technology Inc. ...

Page 111

... Description: Include: Prototype: Arguments: Return Value: Remarks: © 2008 Microchip Technology Inc. Generates a pseudo-random integer. <stdlib.h> int rand(void); Returns an integer between 0 and RAND_MAX. Calls to this function return pseudo-random integer values in the range [0,RAND_MAX]. To use this function effectively, you must seed the ran- dom number generator using the srand function ...

Page 112

... NULL) printf("Cannot allocate memory\n"); else { printf("Memory allocated\n"); /* Temp pointer in case realloc() fails */ ((i = (long *)realloc( sizeof(long))) == NULL) { printf("Cannot reallocate memory\n"); /* j pointed to allocated memory */ free(j); } else { printf("Memory reallocated\n"); free(i Output: Memory allocated Memory reallocated © 2008 Microchip Technology Inc. ...

Page 113

... Description: Include: Prototype: Arguments: Return Value: Remarks: © 2008 Microchip Technology Inc. Set the starting seed for the pseudo-random number sequence. <stdlib.h> void srand(unsigned int seed); starting value for the pseudo-random number sequence seed None This function sets the starting seed for the pseudo-random number sequence generated by the rand function ...

Page 114

... Output: String = "1.28 inches" float = 1.280000 Stopped at: inches String = "27.835e2i" float = 2783.500000 Stopped at: i String = "Number1" float = 0.000000 Stopped at: Number1 © 2008 Microchip Technology Inc. ...

Page 115

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Converts a partial string to a long integer. <stdlib.h> long strtol(const char *s, char **endptr, int base); string to be converted s pointer to the character at which the conversion stopped endptr number base to use in conversion base Returns the converted number if successful ...

Page 116

... Output: String = "12BGET3" long = 429164 Stopped at: T3 String = "0x1234Number" long = 4660 Stopped at: Number String = "-123abc" long = 4294967173 Stopped at: abc */ © 2008 Microchip Technology Inc. ...

Page 117

... Remarks: wcstombs Description: Include: Prototype: Arguments: Return Value: Remarks: © 2008 Microchip Technology Inc. Execute a command. <stdlib.h> int system(const char *s); command to be executed s This function must be customized to be used as described (see pic30-libs). By default system will cause a reset if called with anything other than NULL. system(NULL) will do nothing. ...

Page 118

... NULL) { res = ptr - buf1 + 1; printf("%c found at position %d\n", ch1, res); } else printf("%c not found\n", ch1); */ © 2008 Microchip Technology Inc. ...

Page 119

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. printf("\n"); ptr = memchr(buf1, ch2, 50); if (ptr != NULL) { res = ptr - buf1 + 1; printf("%c found at position %d\n", ch2, res); } else printf("%c not found\n", ch2); ...

Page 120

... Output: buf1 : Where is the time? buf2 : Where did they go? buf3 : Why? 6 characters of buf1 and buf2 are equal buf2 comes before buf1 buf1 comes before buf3 © 2008 Microchip Technology Inc. ...

Page 121

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Copies characters from one buffer to another. <string.h> void *memcpy(void *dst , const void *src , size_t n); buffer to copy characters to dst buffer to copy characters from src number of characters to copy n Returns dst. ...

Page 122

... Output: buf1 : When time marches on buf2 : Where is the time? buf3 : Why? buf1 after memmove of 6 chars of buf2: Where ime marches on buf1 after memmove of 5 chars of buf3: Why? */ © 2008 Microchip Technology Inc. ...

Page 123

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Copies the specified character into the destination buffer. <string.h> void *memset(void *s, int c, size_t n); buffer s character to put in buffer c number of times n Returns the buffer with characters written to it. The character c is written to the buffer n times. ...

Page 124

... Output: buf1 : We're here (10 characters) buf2 : Where is the time? (18 characters) buf1 after strcat of buf2: We're hereWhere is the time? (28 characters) buf1 after strcat of "Why?": We're hereWhere is the time?Why? (32 characters) */ © 2008 Microchip Technology Inc. ...

Page 125

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Locates the first occurrence of a specified character in a string. <string.h> char *strchr(const char *s, int c); pointer to the string s character to search for c Returns a pointer to the location of the match if successful; otherwise, returns a null pointer ...

Page 126

... Microchip Technology Inc. ...

Page 127

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Output: buf1 : Where is the time? buf2 : Where did they go? buf3 : Why? buf2 comes before buf1 buf1 comes before buf3 "Why?" and buf3 are equal Compares one string to another. (See Remarks.) < ...

Page 128

... Output: strcspn("hello", "aeiou" strcspn("animal", "aeiou" strcspn("animal", "xyz" © 2008 Microchip Technology Inc. ...

Page 129

... Description: Include: Prototype: Argument: Return Value: Remarks: © 2008 Microchip Technology Inc. Explanation: In the first result stops counting after h. In the second result s2. In the third result, none of the characters of s1 are all charac- ters are counted. Gets an internal error message. ...

Page 130

... If the strings overlap, the behavior is undefined. #include <string.h> /* for strncat, strlen */ #include <stdio.h> /* for printf int main(void) { char buf1[50] = "We're here"; char buf2[50] = "Where is the time?"; char buf3[50] = "Why?"; */ © 2008 Microchip Technology Inc. ...

Page 131

... Microchip Technology Inc. printf("buf1 : %s\n", buf1); printf("\t(%d characters)\n\n", strlen(buf1)); printf("buf2 : %s\n", buf2); printf("\t(%d characters)\n\n", strlen(buf2)); printf("buf3 : %s\n", buf3); printf("\t(%d characters)\n\n\n", strlen(buf3)); strncat(buf1, buf2, 6); printf("buf1 after strncat of 6 characters " ...

Page 132

... Microchip Technology Inc. ...

Page 133

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. printf("\n"); res = strncmp(buf1, buf3, 20); if (res < 0) printf("buf1 comes before buf3\n"); else if (res == 0) printf("20 characters of buf1 and buf3 " "are equal\n"); else printf(" ...

Page 134

... Where? buf1 after strncpy of 6 characters of buf2: Where here ( 10 characters) buf1 after strncpy of 18 characters of buf2: Where is the time characters) buf1 after strncpy of 5 characters of buf3: Why characters) buf1 after strncpy of 9 characters of buf4: Where characters) © 2008 Microchip Technology Inc. ...

Page 135

... Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. 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 (" ...

Page 136

... NULL) { res = ptr - buf1 + 1; printf("%c found at position %d\n", ch1, res); } else printf("%c not found\n", ch1); */ © 2008 Microchip Technology Inc. ...

Page 137

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. printf("\n"); ptr = strrchr(buf1, ch2); if (ptr != NULL) { res = ptr - buf1 + 1; printf("%c found at position %d\n", ch2, res); } else printf("%c not found\n", ch2); } Output: ...

Page 138

... NULL) { res = ptr - str1 + 1; printf("\"%s\" found at position %d\n", str2, res); } else printf("\"%s\" not found\n", str2); */ © 2008 Microchip Technology Inc. ...

Page 139

... Description: Include: Prototype: Arguments: Return Value: Remarks: © 2008 Microchip Technology Inc. printf("\n"); ptr = strstr(str1, str3); if (ptr != NULL) { res = ptr - str1 + 1; printf("\"%s\" found at position %d\n", str3, res); } else printf("\"%s\" not found\n", str3); ...

Page 140

... If the return value is greater than or equal to n, the content 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. */ © 2008 Microchip Technology Inc. ...

Page 141

... Include: Prototype: CLOCKS_PER_SEC Description: Include: Prototype: Value: Remarks: © 2008 Microchip Technology Inc. consists of types, macros and functions that manipulate time. time.h Stores processor time values. <time.h> typedef long clock_t The type of the result of the sizeof operator. <time.h> Structure used to hold the time and date (calendar time). ...

Page 142

... Returns the number of clock ticks of elapsed processor time. If the target environment cannot measure elapsed processor time, the function returns -1, cast as a clock_t. (i.e. (clock_t) -1) By default, The 16-bit compiler returns the time as instruction cycles. */ © 2008 Microchip Technology Inc. ...

Page 143

... Example: ctime Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. #include <time.h> /* for clock #include <stdio.h> /* for printf */ volatile int i; int main(void) { clock_t start, stop; int ct; start = clock(); for ( < 10; i++) stop = clock(); printf("start = %ld\n", start); ...

Page 144

... This function breaks down the tod value into the time structure of type tm. By default, the 16-bit 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. */ © 2008 Microchip Technology Inc. ...

Page 145

... Example: localtime Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. #include <time.h> /* for gmtime, asctime time_t, tm #include <stdio.h> /* for printf int main(void) { time_t timer; struct tm *newtime; timer = 1066668182; /* Mon Oct 20 16:43:02 2003 */ newtime = gmtime(&timer); ...

Page 146

... The format parameters follow: %a abbreviated weekday name %A full weekday name %b abbreviated month name %B full month name %c appropriate date and time representation %d day of the month (01-31) %H hour of the day (00-23 © 2008 Microchip Technology Inc. ...

Page 147

... Example: © 2008 Microchip Technology Inc. %I hour of the day (01-12) %j day of the year (001-366) %m month of the year (01-12) %M minute of the hour (00-59) %p AM/PM designator %S second of the minute (00-61) allowing for up to two leap seconds %U week number of the year where Sunday is the first day of week 1 ...

Page 148

... This function is customizable. See pic30-libs. #include <time.h> /* for time #include <stdio.h> /* for printf */ volatile int i; int main(void) { time_t ticks; time(0); /* start time */ for ( < 10; i++) /* waste time */ time(&ticks); /* get time */ printf("Time = %ld\n", ticks); } Output: Time = 256 */ © 2008 Microchip Technology Inc. ...

Page 149

... 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. © 2008 Microchip Technology Inc. 16-BIT LANGUAGE TOOLS or depending upon the selected object module format. ...

Page 150

... Section 1.2 “OMF-Specific Libraries/Start-up Modules” for .a ® Assembler, Linker and Utilities for PIC24 MCUs and dsPIC ® DSCs User’s Guide” (DS51284) for more information and omf omf libc- .a libm- .a ® ® C Compiler for PIC24 © 2008 Microchip Technology Inc. ...

Page 151

... Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. consists of a macro and various functions that calculate com- math.h ). errno.h if the result overflowed (return value was too large zero if the 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) ...

Page 152

... Output: Error: domain error The arccosine of 2.000000 is nan The arccosine of 0.000000 is 1.570796 */ */ © 2008 Microchip Technology Inc. ...

Page 153

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the trigonometric arc sine function of a double precision floating-point value. <math.h> double asin (double x); value between -1 and 1 for which to return the arc sine x Returns the arc sine in radians in the range of -pi/2 to +pi/2 (inclusive). ...

Page 154

... No domain or range error will occur. #include <math.h> /* for atan #include <stdio.h> /* for printf */ int main(void) { double 2. atan (x); printf("The arctangent %f\n\n" -1. atan (x); printf("The arctangent %f\n\n", x, y); } Output: The arctangent of 2.000000 is 1.107149 The arctangent of -1.000000 is -0.785398 */ © 2008 Microchip Technology Inc. ...

Page 155

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the trigonometric arc tangent function of a single precision floating-point value. <math.h> float atanf (float x); value for which to return the arc tangent x Returns the arc tangent in radians in the range of -pi/2 to +pi/2 (inclusive) ...

Page 156

... Output: The arctangent of 2.000000/0.000000 is 1.570796 The arctangent of 0.000000/-1.000000 is 3.141593 Error: domain error The arctangent of 0.000000/0.000000 is nan © 2008 Microchip Technology Inc. ...

Page 157

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the trigonometric arc tangent function of y/x. <math.h> float atan2f (float y, float x); y value for which to return the arc tangent y x value for which to return the arc tangent ...

Page 158

... The ceiling for 1.750000 is The ceiling for 1.500000 is The ceiling for 1.250000 is The ceiling for -2.000000 is The ceiling for -1.750000 is The ceiling for -1.500000 is The ceiling for -1.250000 %f\n", x[i], y); 2.000000 2.000000 2.000000 2.000000 -2.000000 -1.000000 -1.000000 -1.000000 © 2008 Microchip Technology Inc. ...

Page 159

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the ceiling of a value. <math.h> float ceilf(float x); floating-point value. x Returns the smallest integer value greater than or equal domain or range error will occur. See floorf. #include <math.h> ...

Page 160

... Microchip Technology Inc. ...

Page 161

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Output: The cosine of -1.000000 is 0.540302 The cosine of 0.000000 is 1.000000 Calculates the hyperbolic cosine function of a double precision floating-point value. <math.h> double cosh (double x); value for which to return the hyperbolic cosine ...

Page 162

... Output: The hyperbolic cosine of -1.000000 is 1.543081 The hyperbolic cosine of 0.000000 is 1.000000 Error: range error The hyperbolic cosine of 720.000000 is inf */ */ © 2008 Microchip Technology Inc. ...

Page 163

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the exponential function raised to the power x where double precision floating-point value). <math.h> double exp (double x); value for which to return the exponential x Returns the exponential overflow, exp returns inf and on an underflow exp returns 0 ...

Page 164

... Output: The exponential of 1.000000 is 2.718282 Error: range error The exponential of 1000.000000 is inf Error: range error The exponential of -1000.000000 is 0.000000 */ */ © 2008 Microchip Technology Inc. ...

Page 165

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the absolute value of a double precision floating-point value. <math.h> double fabs(double x); floating-point value for which to return the absolute value x Returns the absolute value negative number is returned as positive, a positive number is unchanged.) No domain or range error will occur. #include < ...

Page 166

... The floor for -1.250000 is -2.000000 Calculates the floor of a single precision floating-point value. <math.h> float floorf(float x); floating-point value. x Returns the largest integer value less than or equal domain or range error will occur. See ceilf. */ © 2008 Microchip Technology Inc. ...

Page 167

... Example: fmod Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. #include <math.h> /* for floorf */ #include <stdio.h> /* for printf */ 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; for (i=0; i<8; i++) { y = floorf (x[i]) ...

Page 168

... Microchip Technology Inc. ...

Page 169

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Output: For fmod(7.000000, 3.000000) the remainder is 1.000000 For fmod(7.000000, 7.000000) the remainder is 0.000000 For fmod(-5.000000, 3.000000) the remainder is -2.000000 For fmod(5.000000, -3.000000) the remainder is 2 ...

Page 170

... For fmodf (-5.000000, 3.000000) the remainder is -2.000000 For fmodf (5.000000, -3.000000) the remainder is 2.000000 For fmodf (5.000000, -5.000000) the remainder is 0.000000 Error: domain error For fmodf (7.000000, 0.000000) the remainder is nan For fmodf (7.000000, 7.000000) the remainder is 0.000000 © 2008 Microchip Technology Inc. ...

Page 171

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Gets the fraction and the exponent of a double precision floating-point number. <math.h> double frexp (double x, int *exp); floating-point value for which to return the fraction and exponent x exp pointer to a stored integer exponent Returns the fraction, exp points to the exponent ...

Page 172

... For frexpf of -2.500000 the fraction is -0.625000 and the exponent is 2 For frexpf of 0.000000 the fraction is 0.000000 and the exponent is 0 the fraction is %f\n ", the fraction is %f\n ", the fraction is %f\n ", © 2008 Microchip Technology Inc. ...

Page 173

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the result of a double precision floating-point number multiplied by an exponent of 2. <math.h> double ldexp(double x, int ex); floating-point value x integer exponent ex Returns x * 2^ex overflow, ldexp returns inf and on an underflow, ldexp returns 0 ...

Page 174

... Microchip Technology Inc. ...

Page 175

... Microchip Technology Inc. errno = 15.0F 10000 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" y); } Output: For a number = -0.625000 and an exponent = 2 ldexpf(-0.625000 -2.500000 For a number = 2.500000 and an exponent = 3 ldexpf(2 ...

Page 176

... Output: The natural logarithm of 2.000000 is 0.693147 The natural logarithm of 0.000000 is -inf Error: domain error The natural logarithm of -2.000000 is nan */ */ © 2008 Microchip Technology Inc. ...

Page 177

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the base-10 logarithm of a double precision floating-point value. <math.h> double log10(double x); any double precision floating-point positive number x Returns the base-10 logarithm of x. -inf is returned and NaN is returned negative number. A domain error occurs if x ≤ ...

Page 178

... Output: The base-10 logarithm of 2.000000 is 0.301030 Error: domain error The base-10 logarithm of 0.000000 is -inf Error: domain error The base-10 logarithm of -2.000000 is nan */ */ © 2008 Microchip Technology Inc. ...

Page 179

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the natural logarithm of a single precision floating-point value. <math.h> float logf(float x); any positive value for which to return the log x Returns the natural logarithm of x. -inf is returned and NaN is returned negative number. A domain error occurs if x ≤ ...

Page 180

... Output: For 0.707000 the fraction is 0.707000 and the integer is 0 For -15.212100 the fraction is -0.212100 and the integer is -15 */ © 2008 Microchip Technology Inc. ...

Page 181

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Splits a single precision floating-point value into fractional and integer parts. <math.h> float modff(float x, float *pint); single precision floating-point value x pointer to stored integer part pint Returns the signed fractional part and pint points to the integer part. ...

Page 182

... Microchip Technology Inc. ...

Page 183

... Description: Include: Prototype: Arguments: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Output: -2.000000 raised to 3.000000 is -8.000000 3.000000 raised to -0.500000 is 0.577350 4.000000 raised to 0.000000 is 1.000000 Error: domain error 0.000000 raised to -3.000000 is inf Calculates x raised to the power y. <math.h> ...

Page 184

... Output: The sine of -1.000000 is -0.841471 The sine of 0.000000 is 0.000000 */ */ © 2008 Microchip Technology Inc. ...

Page 185

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the trigonometric sine function of a single precision floating-point value. <math.h> float sinf (float x); value for which to return the sine x Returns the sin radians in the ranges inclusive. ...

Page 186

... Output: The hyperbolic sine of -1.500000 is -2.129279 The hyperbolic sine of 0.000000 is 0.000000 Error: range error The hyperbolic sine of 720.000000 is inf */ */ © 2008 Microchip Technology Inc. ...

Page 187

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the hyperbolic sine function of a single precision floating-point value. <math.h> float sinhf (float x); value for which to return the hyperbolic sine x Returns the hyperbolic sine range error will occur if the magnitude too large. ...

Page 188

... Output: The square root of 0.000000 is 0.000000 The square root of 9.500000 is 3.082207 Error: domain error The square root of -25.000000 is nan */ */ © 2008 Microchip Technology Inc. ...

Page 189

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. Calculates the square root of a single precision floating-point value. <math.h> float sqrtf(float x); non-negative floating-point value x Returns the non-negative square root negative, a domain error occurs. #include <math.h> /* for sqrtf #include < ...

Page 190

... Returns the tangent domain error will occur NaN or infinity. #include <math.h> /* for tanf #include <stdio.h> /* for printf, perror */ #include <errno.h> /* for errno int main(void) { float © 2008 Microchip Technology Inc. ...

Page 191

... Description: Include: Prototype: Argument: Return Value: Remarks: Example: © 2008 Microchip Technology Inc. errno = -1.0F tanf (x); if (errno) perror("Error"); printf("The tangent %f\n\n", x, y); errno = 0.0F tanf (x); if (errno) perror("Error"); printf("The tangent %f\n", x, y); } Output: The tangent of -1 ...

Page 192

... Output: The hyperbolic tangent of -1.000000 is -0.761594 The hyperbolic tangent of 0.000000 is 0.000000 */ © 2008 Microchip Technology Inc. ...

Page 193

... Functions for Erasing and Writing EEDATA Memory • Functions for Erasing and Writing Flash Memory • Functions for Specialized Copying and Initialization © 2008 Microchip Technology Inc. 16-BIT LANGUAGE TOOLS c:\Program Files\Microchip\MPLAB C30 – source code for library functions, batch file to rebuild the library – ...

Page 194

... C Compiler for PIC24 sim30 , which makelib.bat . Execute the batch file from a directory. Then copy the newly © 2008 Microchip Technology Inc. ...

Page 195

... Default Behavior: File: brk Description: Include: Prototype: Argument: Return Value: Remarks: © 2008 Microchip Technology Inc. archive and the source code (for the compiler) is available in the omf .a Terminate program execution. None void _exit (int status); exit status status This is a helper function called by the exit() Standard C Library func- tion ...

Page 196

... Returns ‘0’ if the file is successfully closed. A return value of ‘-1’ indi- cates an error. This helper function is called by the fclose() Standard C Library function. As distributed, this function passes the file handle to the simulator, which issues a close in the host file system. close.c © 2008 Microchip Technology Inc. ...

Page 197

... Description: Include: Prototype: Argument: Return Value: Remarks: Default Behavior: File: © 2008 Microchip Technology Inc. Move a file pointer to a specified location. None long lseek(int handle, long offset, int origin); refers to an opened file handle the number of characters from the origin offset the position from which to start the seek ...

Page 198

... If the incr is non-zero, the function checks that the address (__curbrk + incr) is less than the end address of the heap 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 description of brk(). sbrk.c © 2008 Microchip Technology Inc. ...

Page 199

... Description: Include: Prototype: Argument: Return Value: Remarks: Default Behavior: File: © 2008 Microchip Technology Inc. Write data to a file. None int write(int handle, void *buffer, unsigned int count); refers to an opened file handle points to the storage location of data to be written buffer the number of characters to write ...

Page 200

... Returns ‘0’ successful error, the function returns a non-zero value. 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. rename.c Execute a command. <stdlib.h> int system(const char *s); command to be executed. s © 2008 Microchip Technology Inc. ...

Related keywords