




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、 PICC庫函數(shù)本章將詳細列出PICC編譯器的庫函數(shù)。每個函數(shù)均從函數(shù)名開始,然后按照以下幾個標題給出詳細解釋。提要:函數(shù)的C語言定義以及定義函數(shù)的頭文件。描述:對函數(shù)及其目的進行敘述性描述。例程:給出一個能說明該函數(shù)的應用例子。數(shù)據(jù)類型:列出函數(shù)中使用的一些特殊的數(shù)據(jù)類型(如結構體等)的C語言定義。這些數(shù)據(jù)類型的定義包含在提要標題下列出的頭文件中。參閱:給出相關聯(lián)的函數(shù)。返回值:如果函數(shù)有返回值,則在本標題下將給出返回值的類型和性質,同時還包括錯誤返回的信息。1 ABS函數(shù)1. 提 要#include <stdlib.h>int abs (int j)2. 描 述abs( )函數(shù)
2、返回變量j的絕對值。3. 例 程#include <stdio.h>#include <stdlib.h>voidmain (void)int a = -5;printf("The absolute value of %d is %dn",a,abs(a);4. 返回值j的絕對值。2 ACOS函數(shù)1. 提 要 #include <math.h>double acos (double f)2. 描 述acos( )函數(shù)是cos( )的反函數(shù)。函數(shù)參數(shù)在-1,1區(qū)間內,返回值是一個用弧度表示的角度,而且該返回值的余弦值等于函數(shù)參數(shù)。3. 例
3、程#include <math.h>#include <stdio.h>/*以度為單位,打印-1,1區(qū)間內的反余弦值*/voidmain (void)float i,a;for(i = -1.0,i < 1.0;i += 0.1) a = acos(i)*180.0/3.141592;printf("acos(%f) = %f degreesn",i,a);4參 閱 sin( ),cos( ),tan( ),asin( ),atan( ),atan2( )5返回值返回值是一個用弧度表示的角度,區(qū)間是0,。如果函數(shù)參數(shù)超出區(qū)間-1,1,則返回值將
4、為0。3 ASCTIME函數(shù)1. 提 要#include <time.h>char * asctime (struct tm * t)2. 描 述asctime( )函數(shù)通過指針 t 從上struct tm結構體中獲得時間,返回描述當前日期和時間的26個字符串,其格式如下:Sun Sep 16 01:03:52 1973n0值得注意的是,在字符串的末尾有換行符。字符串中的每個字長是固定的。以下例程得到當前時間,通過localtime( )函數(shù)將其轉換成一個struct tm指針,最后轉換成ASCII碼并打印出來。其中,time( )函數(shù)需要用戶提供(詳情請參閱time( )函數(shù))。
5、3. 例 程#include <stdio.h>#include <time.h>voidmain (void)time_t clock;struct tm * tp;time(&clock);tp = localtime(&clock);printf("%s",asctime(tp);4. 參 閱ctime( ),gmtime( ),localtime( ),time( )5. 返回值指向字符串的指針。 注意:由于編譯器不提供time( )例行程序,故在本例程中它需要由用戶提供。詳情請參照time( )函數(shù)。6. 數(shù)據(jù)類型struc
6、t tm int tm_sec;int tm_min;int tm_hour;int tm_mday;int tm_mon;int tm_year;int tm_wday;int tm_yday;int tm_isdst;4 ASIN函數(shù)1. 提 要#include <math.h>double asin (double f)2. 描 述asin( )函數(shù)是sin( )的反函數(shù)。它的函數(shù)參數(shù)在-1,1區(qū)間內,返回一個用弧度表示的角度值,而且這個返回值的正弦等于函數(shù)參數(shù)。3. 例 程#include <math.h>#include <stdio.h>void
7、main (void)float i,a;for(i = -1.0;i < 1.0 ;i += 0.1) a = asin(i)*180.0/3.141592;printf("asin(%f) = %f degreesn",i,a);4. 參 閱sin( ),cos( ),tan( ),acos( ),atan( ),atan2( )5. 返回值本函數(shù)返回一個用弧度表示的角度值,其區(qū)間為-/2,/2。如果函數(shù)參數(shù)的值超出區(qū)間-1,1,則函數(shù)返回值將為0。5 ATAN函數(shù)1. 提 要#include <math.h>double atan (double x
8、)2. 描 述函數(shù)返回參數(shù)的反正切值。也就是說,本函數(shù)將返回一個在區(qū)間-/2,/2的角度e,而且有tan(e)=x(x為函數(shù)參數(shù))。3. 例 程#include <stdio.h>#include <math.h>voidmain (void)printf("%fn",atan(1.5);4. 參 閱sin( ),cos( ),tan( ),asin( ),acos( ),atan2( )5. 返回值返回函數(shù)參數(shù)的反正切值。6 ATAN2函數(shù)1. 提 要#include <math.h>double atan2 (double y,dou
9、ble x)2. 描 述本函數(shù)返回y/x的反正切值,并由兩個函數(shù)參數(shù)的符號來決定返回值的象限。3. 例 程#include <stdio.h>#include <math.h>voidmain (void)printf("%fn",atan2(1.5,1);4. 參 閱sin( ),cos( ),tan( ),asin( ),acos( ),atan( )5. 返回值返回y/x的反正切值(用弧度表示),區(qū)間為-,。如果y和x均為0,將出現(xiàn)定義域錯誤,并返回0。7 ATOF函數(shù)1. 提 要#include <stdlib.h>double
10、atof (const char * s)2. 描 述atof( )函數(shù)將掃描由函數(shù)參數(shù)傳遞過來的字符串,并跳過字符串開頭的空格。然后將一個數(shù)的ASCII表達式轉換成雙精度數(shù)。這個數(shù)可以用十進制數(shù)、浮點數(shù)或者科學記數(shù)法表示。3. 例 程#include <stdlib.h>#include <stdio.h>voidmain (void)char buf80;double i;gets(buf);i = atof(buf);printf("Read %s: converted to %fn",buf,i);4. 參 閱atoi( ),atol( )5
11、. 返回值本函數(shù)返回一個雙精度浮點數(shù)。如果字符串中沒有發(fā)現(xiàn)任何數(shù)字,則返回0.0。8 ATOI函數(shù)1. 提 要#include <stdlib.h>int atoi (const char * s)2. 描 述atoi( )函數(shù)掃描傳遞過來的字符串,跳過開頭的空格并讀取其符號;然后將一個十進制數(shù)的ASCII表達式轉換成整數(shù)。3. 例 程#include <stdlib.h>#include <stdio.h>voidmain (void)char buf80;int i;gets(buf);i = atoi(buf);printf("Read %s
12、: converted to %dn",buf,i);4. 參 閱xtoi( ),atof( ),atol( )5. 返回值返回一個有符號的整數(shù)。如果在字符串中沒有發(fā)現(xiàn)任何數(shù)字,則返回0。9 ATOL函數(shù)1提 要#include <stdlib.h>long atol (const char * s)2描 述atol( )函數(shù)掃描傳遞過來的字符串,并跳過字符串開頭的空格;然后將十進制數(shù)的ASCII表達式轉換成長整型。3例 程#include <stdlib.h>#include <stdio.h>voidmain (void)char buf80;
13、long i;gets(buf);i = atol(buf);printf("Read %s: converted to %ldn",buf,i);4參 閱atoi( ),atof( )5返回值返回一個長整型數(shù)。如果字符串中沒有發(fā)現(xiàn)任何數(shù)字,返回值為0。10 CEIL函數(shù)1. 提 要#include <math.h>double ceil (double f)2. 描 述本函數(shù)對函數(shù)參數(shù) f 取整,取整后的返回值為大于或等于 f 的最小整數(shù)。3. 例 程#include <stdio.h>#include <math.h>voidmain
14、 (void)double j;scanf("%lf",&j);printf("The ceiling of %lf is %lfn",j,ceil(j);11 COS函數(shù)1. 提 要#include <math.h>double cos (double f)2. 描 述本函數(shù)將計算函數(shù)參數(shù)的余弦值。其中,函數(shù)參數(shù)用弧度表示。余弦值通過多項式級數(shù)近似值展開式算得。3. 例 程#include <math.h>#include <stdio.h>#define C 3.141592/180.0voidmain (
15、void)double i;for(i = 0;i <= 180.0;i += 10)printf("sin(%3.0f) = %f,cos = %fn",i,sin(i*C),cos(i*C);4. 參 閱sin( ),tan( ),asin( ),acos( ),atan( ),atan2( )5. 返回值返回一個雙精度數(shù),區(qū)間為-1,1。12 COSH、SINH、TANH函數(shù)1. 提 要#include <math.h>double cosh (double f)double sinh (double f)double tanh (double f)
16、2. 描 述這些函數(shù)都是cos( ),sin( )和tan( )的雙曲函數(shù)。3. 例 程 #include <stdio.h>#include <math.h>voidmain (void)printf("%fn",cosh(1.5);printf("%fn",sinh(1.5);printf("%fn",tanh(1.5);4. 返回值cosh( )函數(shù)返回雙曲余弦值,sinh( )函數(shù)返回雙曲正弦值,tanh( )函數(shù)返回雙曲正切值。13 CTIME函數(shù)1. 提 要#include <time.h&g
17、t;char * ctime (time_t * t)2. 描 述ctime( )函數(shù)將函數(shù)參數(shù)所指的時間轉換成字符串,其結構與asctime( )函數(shù)所描述的相同,并且精確到秒。以下例程將打印出當前的時間和日期。3. 例 程#include <stdio.h>#include <time.h>voidmain (void)time_t clock;time(&clock);printf("%s",ctime(&clock);4. 參 閱gmtime( ),localtime( ),asctime( ),time( )5. 返回值本函
18、數(shù)返回一個指向該字符串的指針。 注意:由于編譯器不會提供time( )程序,故它需要由用戶給定。詳情請參閱time( )函數(shù)。6. 數(shù)據(jù)類型typedef long time_t14 DI、EI函數(shù)1. 提 要#include <pic.h>void ei(void)void di(void)2. 描 述ei( )和di( )函數(shù)分別實現(xiàn)全局中斷使能和中斷屏蔽,其定義在pic.h頭文件中。它們將被擴展為一條內嵌的匯編指令,分別對中斷使能位進行置位和清零。以下例程將說明ei( )函數(shù)和di( )函數(shù)在訪問一個長整型變量時的應用。由于中斷服務程序將修改該變量,所以如果訪問該變量不按照本
19、例程的結構編程,一旦在訪問變量值的連續(xù)字期間出現(xiàn)中斷,則函數(shù)getticks( ) 將返回錯誤的值。3. 例 程#include <pic.h>long count;void interrupt tick(void)count+;long getticks(void)long val; /*在訪問count變量前禁止中斷,保證訪問的連續(xù)性*/di( );val = count;ei( );return val;15 DIV函數(shù)1. 提 要#include <stdlib.h>div_t div (int numer,int demon)2. 描 述div( )函數(shù)實現(xiàn)分
20、子除以分母,得到商和余數(shù)。3. 例 程#include <stdlib.h>#include <stdio.h>voidmain (void)div_t x;x = div(12345,66);printf("quotient = %d,remainder = %dn",x.quot,x.rem);4. 返回值返回一個包括商和余數(shù)的結構體div_t。. 5. 數(shù)據(jù)類型typedef structint quot;int rem; div_t; 16 EEPROM_READ、EEPROM_WRITE函數(shù)1. 提 要#include <pic.h&
21、gt;unsigned char eeprom_read (unsigned char addr);void eeprom_write (unsigned char addr,unsigned char value);2. 描 述這些函數(shù)允許訪問片內EEPROM(如果片內有EEPROM)。EEPROM不是可直接尋址的寄存器空間,當需要訪問EEPROM時,就需要將一些特定的字節(jié)序列加載到EEPROM控制寄存器中。寫EEPROM是一個緩慢的過程。故eeprom_write( )函數(shù)在寫入下一個數(shù)據(jù)前,會通過查詢恰當?shù)募拇嫫鱽泶_保前一個數(shù)據(jù)已經寫入完畢。另外,讀EEPROM可以在一個指令周期內完成,
22、所以沒有必要查詢讀操作是否完成。3. 例 程#include <pic.h>voidmain (void)unsigned char data;unsigned char address;address = 0x10;data = eeprom_read(address); 注意:如果調用eeprom_write( )函數(shù)后需即刻調用eeprom_read( )函數(shù),則必須查詢EEPROM寄存器以確保寫入完畢。全局中斷使能位(GIE)在eeprom_write( )程序中重新恢復(寫EEPROM時需要關閉總中斷)。而且,本函數(shù)不會清EEIF標志位。17 EVAL_POLY函數(shù)1.
23、提 要#include <math.h>double eval_poly (double x,const double * d,int n)2. 描 述eval_poly( )函數(shù)將求解一個多項式的值。這個多項式的系數(shù)分別包含在x和數(shù)組d中,例如:y = x*x*d2 + x*d1 + d0該多項式的階數(shù)由參數(shù)n傳遞過來。3. 例 程#include <stdio.h>#include <math.h>voidmain (void)double x,y;double d3 = 1.1,3.5,2.7;x = 2.2;y = eval_poly(x,d,2);
24、printf("The polynomial evaluated at %f is %fn",x,y);4. 返回值本函數(shù)返回一個雙精度數(shù),該數(shù)是自變量x對應的多項式值。18 EXP函數(shù)1. 提 要#include <math.h>double exp (double f)2. 描 述exp( )函數(shù)返回參數(shù)的指數(shù)函數(shù)值,即ef(f為函數(shù)參數(shù))。3. 例 程#include <math.h>#include <stdio.h>voidmain (void)double f;for(f = 0.0;f <= 5;f += 1.0)pr
25、intf("e to %1.0f = %fn",f,exp(f);4. 參 閱log( ),log10( ),pow( )19 FABS函數(shù)1. 提 要#include <math.h>double fabs (double f)2. 描 述本函數(shù)返回雙精度函數(shù)參數(shù)的絕對值。3. 例 程#include <stdio.h>#include <math.h>voidmain (void)printf("%f %fn",fabs(1.5),fabs(-1.5);4. 參 閱abs( )20 FLOOR函數(shù)1. 提 要#in
26、clude <math.h>double floor (double f)2. 描 述本函數(shù)對函數(shù)參數(shù)取整,取整后的返回值不大于函數(shù)參數(shù)f。3. 例 程#include <stdio.h>#include <math.h>voidmain (void)printf("%fn",floor( 1.5 );printf("%fn",floor( -1.5);21 FREXP函數(shù)1. 提 要#include <math.h>double frexp (double f,int * p)2. 描 述frexp( )
27、函數(shù)將一個浮點數(shù)分解成規(guī)格化小數(shù)和2的整數(shù)次冪兩部分,整數(shù)冪部分存于指針 p 所指的 int 單元中。本函數(shù)的返回值x或在區(qū)間(0.5,1.0)內,或為 0;而且有 f=x×2p。如果f為0,則分解出來的兩部分均為0。3. 例 程#include <math.h>#include <stdio.h>voidmain (void)double f;int i;f = frexp(23456.34,&i);printf("23456.34 = %f * 2%dn",f,i);4. 參 閱ldexp( )22 GET_CAL_DATA函數(shù)
28、1. 提 要#include <pic.h>double get_cal_data (const unsigned char * code_ptr)2. 描 述本函數(shù)從PIC 14000標定空間返回一個32位的浮點標定數(shù)據(jù)。只有利用這個函數(shù)才能訪問KREF、KBG、BHTHERM和KTC單元(32位浮點參數(shù))。由于FOSC和TWDT均是一個字節(jié)長度,故可以直接訪問它們。3. 例 程#include <pic.h>voidmain (void)double x;unsigned char y;x = get_cal_data(KREF);/*獲得參考斜率(slope re
29、ference ratio)*/y =TWDT;/*獲得WDT溢出時間*/4. 返回值返回定標參數(shù)值。 注意:本函數(shù)僅用于PIC 1400023 GMTIME函數(shù)1. 提 要#include <time.h>struct tm * gmtime (time_t * t)2. 描 述本函數(shù)把指針 t 所指的時間分解,并且存于結構體中,精確度為秒。其中,t 所指的時間必須自1970年1月1日0時0分0秒起。本函數(shù)所用的結構體被定義在time.h文件中,可參照本節(jié)“數(shù)據(jù)類型”部分。3. 例 程#include <stdio.h>#include <time.h>v
30、oidmain (void)time_t clock;struct tm * tp;time(&clock);tp = gmtime(&clock);printf("Its %d in Londonn",tp->tm_year+1900);4. 參 閱ctime( ),asctime( ),time( ),localtime( )5. 返回值返回tm類型的結構體。 注意:由于編譯器不會提供time( )程序,故它需要由用戶給定。詳情請參閱time( )函數(shù)。6. 數(shù)據(jù)類型typedef long time_t;struct tm int tm_sec;
31、int tm_min;int tm_hour;int tm_mday;int tm_mon;int tm_year;int tm_wday;int tm_yday;int tm_isdst;24 ISALNUM,ISALPHA,ISDIGIT,ISLOWER 等函數(shù)1. 提 要#include <ctype.h>int isalnum (char c)int isalpha (char c)int isascii (char c)int iscntrl (char c)int isdigit (char c)int islower (char c)int isprint (char
32、 c)int isgraph (char c)int ispunct (char c)int isspace (char c)int isupper (char c)int isxdigit(char c)2. 描 述以上函數(shù)都被定義在ctype.h文件中。它們將測試給定的字符,看該字符是否為已知的幾組字符中的成員。isalnum (c) c在09、az或者AZ范圍內;isalpha (c) c在AZ或az范圍內;isascii (c) c為7位ASCII字符;iscntrl (c) c為控制字符;isdigit (c) c為十進制阿拉伯數(shù)字;islower (c) c在az范圍內;ispri
33、nt (c) c為打印字符;isgraph (c) c為非空格可打印字符;ispunct (c) c不是字母數(shù)字混合的;isspace (c) c是空格鍵、TAB鍵或換行符;isupper (c) c在AZ范圍內;isxdigit (c) c在09、af或AF范圍內。3例 程#include <ctype.h>#include <stdio.h>voidmain (void)char buf80;int i;gets(buf);i = 0;while(isalnum(bufi)i+;bufi = 0;printf("%s is the wordn",
34、buf);4參 閱toupper( ),tolower( ),toascii( )25 KBHIT函數(shù)1. 提 要#include <conio.h>bit kbhit (void)2. 描 述 如果鍵盤上的字符被按下,函數(shù)返回1;否則返回0。通常,該字符可通過getch( )函數(shù)讀取。3. 例 程#include <conio.h>voidmain (void)int i;while(!kbhit() cputs("Im waiting.");for(i = 0 ; i != 1000 ; i+)continue;4. 參 閱getch( ),ge
35、tche( )5. 返回值如果有鍵被按下,函數(shù)將返回1;否則返回0。此外,返回值為1位。 注意:程序的主體需由用戶實現(xiàn),其主要框架可以從sources目錄下直接獲得。26 LDEXP函數(shù)1. 提 要#include <math.h>double ldexp (double f,int i)2. 描 述ldexp( )函數(shù)是frexp( )的反函數(shù)。它先進行浮點數(shù) f 的指數(shù)部分與整數(shù) i 的求和運算,然后返回合成結果。3. 例 程#include <math.h>#include <stdio.h>voidmain (void)double f;f = ld
36、exp(1.0,10);printf("1.0 * 210 = %fn",f);4. 參 閱frexp( )5. 返回值本函數(shù)返回浮點數(shù) f 指數(shù)部分加上整數(shù) i 后得到的新浮點數(shù)。27 LDIV函數(shù)1. 提 要#include <stdlib.h>ldiv_t ldiv (long number,long denom)2. 描 述ldiv( )函數(shù)實現(xiàn)分子除以分母,得到商和余數(shù)。商的符號與精確商的符號一致,絕對值是一個小于精確商絕對值的最大整數(shù)。Ldiv( )函數(shù)與div( )函數(shù)類似;不同點在于,前者的函數(shù)參數(shù)和返回值(結構體ldiv_t)的成員都是長整型數(shù)
37、據(jù)。3例 程#include <stdlib.h>#include <stdio.h>voidmain (void)ldiv_t lt;lt = ldiv(1234567,12345);printf("Quotient = %ld,remainder = %ldn",lt.quot,lt.rem);4參 閱div( )5返回值返回值是結構體ldiv_t。6數(shù)據(jù)結構typedef struct long quot; /*商*/long rem; /*余數(shù)*/ ldiv_t;28 LOCALTIME函數(shù)1. 提 要#include <time.h&
38、gt;struct tm * localtime (time_t * t)2. 描 述本函數(shù)把指針t所指的時間分解并且存于結構體中,精確度為秒。其中,t所指的時間必須自1970年1月1日0時0分0秒起,所用的結構體被定義在time.h文件中。localtime( )函數(shù)需要考慮全局整型變量time_zone中的內容,因為它包含有本地時區(qū)位于格林威治以西的時區(qū)數(shù)值。由于在MS-DOS環(huán)境下無法預先確定這個值,所以,在缺省的條件下,localtime( )函數(shù)的返回值將與gmtime( )的相同。3例 程 #include <stdio.h>#include <time.h>
39、;char * wday = "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday";voidmain (void)time_t clock;struct tm * tp;time(&clock);tp = localtime(&clock);printf("Today is %sn",wdaytp->tm_wday);4. 參
40、閱ctime( ),asctime( ),time( )5. 返回值本函數(shù)返回tm結構體型數(shù)據(jù)。 注意:由于編譯器不會提供time( )程序,故它需要由用戶給定。詳情請參閱time( )函數(shù)。6. 數(shù)據(jù)結構typedef long time_t;struct tm int tm_sec;int tm_min;int tm_hour;int tm_mday;int tm_mon;int tm_year;int tm_wday;int tm_yday;int tm_isdst;29 LOG、LOG10函數(shù)1. 提 要#include <math.h>double log (double
41、 f)double log10 (double f)2. 描 述log( )函數(shù)返回f的自然對數(shù)值。log10( )函數(shù)返回f以10為底的對數(shù)值。3. 例 程#include <math.h>#include <stdio.h>voidmain (void)double f;for(f = 1.0;f <= 10.0;f += 1.0)printf("log(%1.0f) = %fn",f,log(f);4. 參 閱exp( ),pow( )5. 返回值如果函數(shù)參數(shù)為負,返回值為0。30 MEMCHR函數(shù)1. 提 要#include <s
42、tring.h>/* 初級和中級系列單片機 */const void * memchr (const void * block,int val,size_t length)/* 高級系列單片機*/void * memchr (const void * block,int val,size_t length) 2. 描 述memchr( )函數(shù)與strchr( )函數(shù)在功能上類似;但前者沒有在字符串中尋找null(空)中止字符的功能。memchr( )函數(shù)實現(xiàn)在一段規(guī)定了長度的內存區(qū)域中尋找特定的字節(jié)。它的函數(shù)參數(shù)包括指向被尋內存區(qū)域的指針、被尋字節(jié)的值和被尋內存區(qū)域的長度。函數(shù)將返回一個
43、指針,該指針指向被尋內存區(qū)域中被尋字節(jié)首次出現(xiàn)的單元。3. 例 程 #include <string.h>#include <stdio.h>unsigned int ary = 1,5,0x6789,0x23;voidmain (void)char * cp;cp = memchr(ary,0x89,sizeof ary);if(!cp)printf("not foundn");elseprintf("Found at offset %un",cp - (char *)ary);4. 參 閱strchr( )5. 返回值函數(shù)返回
44、指針。該指針指向被尋內存區(qū)域中被尋字節(jié)首次出現(xiàn)的單元;否則返回NULL。31 MEMCMP函數(shù)1. 提 要#include <string.h>int memcmp (const void * s1, const void * s2, size_t n)2. 描 述memcmp()函數(shù)的功能是比較兩塊長度為 n 的內存中變量的大小,類似strncmp()函數(shù)返回一個有符號數(shù)。與strncmp()函數(shù)不同的是,memcmp()函數(shù)沒有空格結束符。ASCII碼字符順序被用來比較;但如果內存塊中包含非ASCII碼字符,則返回值不確定。測試是否相等總是可靠的。3. 例 程#include
45、<stdio.h>#include <string.h>voidmain (void)int buf10, cow10, i;buf0 = 1;buf2 = 4;cow0 = 1;cow2 = 5;buf1 = 3;cow1 = 3;i = memcmp(buf, cow, 3*sizeof(int);if(i < 0)printf("less thann");else if(i > 0)printf("Greater thann");elseprintf("Equaln");4. 參 閱strnc
46、py(), strncmp(), strchr(), memset(), memchr()5. 返回值當內存塊變量s1分別小于、等于或大于內存塊s2變量時, 函數(shù)返回值分別為-1,0或1。32 MEMCPY函數(shù)1. 提 要#include <string.h>/* 低級或中級系列單片機 */void * memcpy (void * d, const void * s, size_t n)/* 高級系列單片機 */far void * memcpy (far void * d, const void * s, size_t n)2. 描 述memcpy()函數(shù)的功能是, 將指針s指
47、向的、內存開始的 n 個字節(jié)復制到指針d指向的、內存開始的單元。復制重疊區(qū)的結果不確定。與strcpy()函數(shù)不同的是,memcpy()復制的是一定數(shù)量的字節(jié),而不是復制所有結束符前的數(shù)據(jù)。3. 例 程#include <string.h>#include <stdio.h>voidmain (void)char buf80;memset(buf, 0, sizeof buf);memcpy(buf, "a partial string", 10);printf("buf = %sn", buf);4. 參 閱strncpy(),
48、 strncmp(), strchr(), memset()5. 返回值memcpy()函數(shù)返回值為函數(shù)的第一個參數(shù)。33 MEMMOVE函數(shù)1. 提 要#include <string.h>/* 低級或中級系列單片機 */void * memmove (void * s1, const void * s2, size_t n)/* 高級系列單片機 */far void * memmove (far void * s1, const void * s2, size_t n)2. 描 述memmove()函數(shù)與memcpy()函數(shù)相似,但memmove()函數(shù)能對重疊區(qū)進行準確的復制
49、。也就是說,它可以適當向前或向后,正確地從一個塊復制到另一個塊,并將它覆蓋。3. 參 閱strncpy(), strncmp(), strchr(), memcpy()4. 返回值memmove()函數(shù)同樣返回它的第一個參數(shù)。34 MEMSET函數(shù)1. 提 要#include <string.h>/* 低級和中級系列的單片機 */void * memset (void * s, int c, size_t n)/* 高級系列單片機 */far void * memset (far void * s, int c, size_t n)2. 描 述memset()函數(shù)將字節(jié)c存儲到指針
50、s指向的,內存開始的n個內存字節(jié)。3. 例 程#include <string.h>#include <stdio.h>voidmain (void)char abuf20;strcpy(abuf, "This is a string");memset(abuf, x, 5);printf("buf = %sn", abuf);4. 參 閱strncpy(), strncmp(), strchr(), memcpy(), memchr()35 MODF函數(shù)1. 提 要#include <math.h>double mo
51、df (double value, double * iptr)2. 描 述modf()函數(shù)將參數(shù)value分為整數(shù)和小數(shù)2部分,每部分都和value的符號相同。例如,-3.17將被分為整數(shù)部分(-3)和小數(shù)部分(-0.17)。其中整數(shù)部分以雙精度數(shù)據(jù)類型存儲在指針iptr指向的單元中。3. 例 程#include <math.h>#include <stdio.h>voidmain (void)double i_val, f_val;f_val = modf( -3.17, &i_val);4. 返回值函數(shù)返回值為value的帶符號小數(shù)部分。36 PERSIS
52、T_CHECK, PERSIST_VALIDATE函數(shù)1. 提 要#include <sys.h>int persist_check (int flag)void persist_validate (void)2. 描 述persist_check()函數(shù)要用到非可變(non-volatile)的RAM變量,這些變量在定義時被加上限定詞persistent。在測試NVRAM(非可變RAM)區(qū)域時,先調用persist_validate()函數(shù),并用到一個存儲在隱藏變量中的虛擬數(shù)據(jù),且由persist_validate()函數(shù)計算得到一個測試結果。如果虛擬數(shù)據(jù)和測試結果都正確,則返回
53、值為真(非零)。如果都不正確,則返回零。在這種情況下,函數(shù)返回零并且重新檢測NVRAM區(qū)域(通過調用persist_validate()函數(shù))。函數(shù)被執(zhí)行的條件是標志變量變量不為0。persist_validate()函數(shù)應在每次轉換為永久變量之后調用。它將重新建立虛擬數(shù)據(jù)和計算測試結果。3. 例 程#include <sys.h>#include <stdio.h>persistent long reset_count;voidmain (void)if(!persist_check(1)printf("Reset count invalid - zeroe
54、dn");elseprintf("Reset number %ldn", reset_count);reset_count+; /* update count */persist_validate(); /* and checksum */for(;)continue; /* sleep until next reset */4. 返回值如果NVRAM區(qū)域無效, 則返回值為假(零);如果NVRAM區(qū)域有效, 則返回值為真(非零)。37 POW函數(shù)1. 提 要#include <math.h>double pow (double f, double p)
55、2. 描 述pow()函數(shù)表示第一個參數(shù)f的p次冪。3. 例 程#include <math.h>#include <stdio.h>voidmain (void)double f;for(f = 1.0 ; f <= 10.0 ; f += 1.0)printf("pow(2, %1.0f) = %fn", f, pow(2, f);4. 參 閱log(), log10(), exp()5. 返回值返回值為 f 的 p 次冪。38 PRINTF函數(shù)1. 提 要#include <stdio.h>unsigned char prin
56、tf (const char * fmt, .)2. 描 述printf()函數(shù)是一個格式輸出子程序,其運行的基礎是標準輸出(staout)。它有對應的程序形成字符緩沖區(qū)(sprintf()函數(shù))。printf()函數(shù)以格式字符串、一系列0及其它作為參數(shù)。格式字符串都轉換為一定的格式,每一規(guī)格化都用來輸出變量表。轉換格式的形式為%m.nc。其中表示格式,m表示選擇的字符寬度,n表示選擇的精度,c為一個字母表示規(guī)格類型。字符寬度和精度只適于中級和高級系列單片機,并且精度只對格式s有效。如果指針變量為十進制常數(shù),例如格式為%*d時,則一個整型數(shù)將從表中被取出,提供給指針變量。對低級系列單片機而言,有下列轉換格式: o x X u d 即分別為整型格式八進制、十六進制、十六進制、十進制和十進制。其中d為有符號十進
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 兒童營養(yǎng)不良的早期干預與調整
- 兒童心理健康問題的預防和干預
- 貴州省黔東南苗族侗族自治州從江縣下江中學2024-2025學年度七年級下學期期末生物學試卷(文字版含答案)
- 2024-2025學年廣東省廣州市六中高一下3月月考英語試卷(題目版)
- 小小超市活動方案
- 山西科普進校園活動方案
- 小學釘扣子活動方案
- 山西錦鯉活動方案
- 市縣招商活動方案
- 峨眉節(jié)日活動策劃方案
- 眼科院感培訓
- 2025年江蘇省公務員錄用考試公安專業(yè)科目試卷
- 2025-2030年中國空調行業(yè)市場深度調研及競爭格局與投資研究報告
- 生產工單結單管理制度
- 北京市2025年第一次普通高中學業(yè)水平合格性考試政治試題(原卷版)
- 2025年天津市南開區(qū)普通高中學業(yè)水平合格性考試數(shù)學模擬試題(解析版)
- 2025年陜西、山西、青海、寧夏高考物理試卷真題(含答案解析)
- 2025-2030中國過程自動化系統(tǒng)行業(yè)市場發(fā)展趨勢與前景展望戰(zhàn)略分析研究報告
- 2025-2030中國臘味行業(yè)市場發(fā)展現(xiàn)狀及發(fā)展趨勢與投資風險研究報告
- 2025年心理咨詢師資格考試試題及答案
- 北京市西城區(qū)三年級下學期數(shù)學期末試卷(含答案)
評論
0/150
提交評論