ไลบรารีมาตรฐานภาษา C อาจารย์สมเกียรติ ช่อเหมือน

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "ไลบรารีมาตรฐานภาษา C อาจารย์สมเกียรติ ช่อเหมือน"— ใบสำเนางานนำเสนอ:

1 ไลบรารีมาตรฐานภาษา C อาจารย์สมเกียรติ ช่อเหมือน
สาขาวิชาวิศวกรรมซอฟต์แวร์ คณะวิทยาศาสตร์และเทคโนโลยี

2 เนื้อหาที่สนใจ ไลบรารีมาตรฐาน C และตัวอย่างการใช้งาน
ไลบรารีไฟล์ assert.h ไลบรารีไฟล์ ctype.h ไลบรารีไฟล์ errno.h ไลบรารีไฟล์ float.h ไลบรารีไฟล์ limits.h ไลบรารีไฟล์ locale.h ไลบรารีไฟล์ math.h ไลบรารีไฟล์ setjmp.h ไลบรารีไฟล์ signal.h ไลบรารีไฟล์ stdarg.h ไลบรารีไฟล์ string.h ไลบรารีไฟล์ time.h

3 ไลบรารี (library) ไลบรารี คือ การจัดกลุ่มของฟังก์ชันและค่าคงที่ โดยจัดเก็บในรูปแบบไฟล์ไลบรารี แต่ละไฟล์จะใช้เป็นที่รวบรวมกระบวนการและฟังก์ชันย่อยในไลบรารี ภาษาซีประกาศไลบรารีไว้ที่ส่วนหัวของไฟล์โปรแกรมหลัก เมื่อคอมไพล์โปรแกรม ไลบรารีจะถูกคอมไพล์และเชื่อมโยงกับโปรแกรมหลัก

4 ไลบรารีมาตรฐาน C ไลบรารีมาตรฐาน C ได้แก่ <stdio.h>, <stdlib.h>, <math.h> เป็นต้น พื้นฐานในการเขียนโปรแกรมภาษา C คือ การทำความเข้าใจฟังก์ชันในไลบรารีที่นำมาประกอบกันเป็นโปรแกรม ไลบรารีจึงเปรียบเสมือนคำสั่งในการเขียนโปรแกรมในด้านต่างๆ เช่น การรับเข้าและส่งออกข้อมูล การจัดสรรหน่วยความจำ การคำนวณทางคณิตศาสตร์ ตัวอักษรและข้อความ เป็นกรอบแนวคิดของการเขียนโปรแกรมด้วยภาษา C

5 ไลบรารีไฟล์ assert.h assert ช่วยในการวินิจฉัยข้อผิดพลาด เพื่อเขียนลงแฟ้มการทำงานที่ผิดพลาด ใช้การประเมินเงื่อนไขในการดำเนินการ (expression) ด้วยฟังก์ชัน assert() TRUE  ไม่ทำอะไรเลย  FALSE แสดงข้อความผิดพลาดบน stderr และยกเลิกการทำงาน ช่วยยืนยันว่าจะไม่เกิดข้อผิดพลาด #include <assert.h> void main() { int a, b; printf("Input two integers to divide\n"); scanf("%d%d", &a, &b); assert(b != 0); printf("%d/%d = %.2f\n", a, b, a/(float)b); getch(); }

6 ไลบรารีไฟล์ ctype.h ctype.h ใช้ในการดำเนินการกับตัวอักษร (char)
ฟังก์ชันทั้งหมดสามารถใช้จำนวนเต็ม (int) เป็นพารามิเตอร์ได้ ASCII Code ฟังก์ชันทั้งหมดจะส่งค่ากลับเป็น ไม่เป็นศูนย์ (!=0 / true) ถ้าอาร์กิวเมนต์ตอบสนองเงื่อนไขที่อธิบาย Zero (0 / false) หากไม่ได้

7 ฟังก์ชันที่สำคัญของ ctype.h

8 ตัวอย่างการใช้ tolower()
#include<ctype.h> #include<conio.h> #include<stdio.h> #include<string.h> void main(void) { int loop; char string[]="Hello NPRU"; size_t l=strlen(string); for(loop=0;loop<=l;loop++) string[loop]=tolower(string[loop]); printf("%s\n",string); getch(); }

9 ไลบรารีไฟล์ errno.h errno.h ใช้กำหนดตัวแปรจำนวนเต็ม errno ซึ่งถูกกำหนดโดยระบบและบางฟังก์ชัน ในกรณีที่มีข้อผิดพลาดในการบ่งบอกถึงสิ่งที่ผิด errno ถูกกำหนดค่าเริ่มต้นเป็นศูนย์ บางฟังก์ชันจะปรับเปลี่ยนค่าเมื่อเกิดข้อผิดพลาด errno.h ใช้กำหนดรายชื่อของแมโครที่ระบุรหัสข้อผิดพลาดที่แตกต่าง S.N. Macro & Description 1 extern int errno การเรียกระบบและไลบรารีบางฟังก์ชันในกรณีที่เกิดข้อผิดพลาดเพื่อระบุสิ่งที่ผิดพลาด 2 EDOM Domain Error ข้อผิดพลาดของโดเมนซึ่งเกิดขึ้นหากอาร์กิวเมนต์อินพุตอยู่นอกโดเมนซึ่งกำหนดฟังก์ชันทางคณิตศาสตร์ไว้และตั้งค่า errno เป็น EDOM 3 ERANGE Range Error ข้อผิดพลาดช่วงที่เกิดขึ้นถ้าอาร์กิวเมนต์อินพุทอยู่นอกช่วงที่กำหนดฟังก์ชันทางคณิตศาสตร์และ errno ตั้งเป็น ERANGE

10 extern int errno แสดงรหัสการ error และแสดงคำอธิบาย
#include <stdio.h> #include <conio.h> #include <errno.h> #include <string.h> extern int errno ; void main (){ FILE *fp; fp = fopen("file.txt", "r"); if( fp == NULL ){ fprintf(stderr, "Value of errno: %d\n", errno); fprintf(stderr, "Error opening file: %s\n", strerror(errno)); } else { fclose(fp); getch();

11 #define EDOM some_value
#include <stdio.h> #include <conio.h> #include <errno.h> #include <math.h> void main(){ double val=0.00; errno = 0; val = sqrt(-4.0); if(errno == EDOM){ printf("Invalid value (%.2f)\n",val); } else { printf("Valid value (%.2f)\n",val); // 2 getch(); //2 errno = 0; val = sqrt(4.0); if(errno == EDOM){ printf("Invalid value (%.2f)\n",val); } else { printf("Valid value (%.2f)\n",val);

12 #define ERANGE some_value
#include <stdio.h> #include <conio.h> #include <errno.h> #include <math.h> void main(){ double x; double value; x = 2.00; value = log(x); if( errno == ERANGE ){ printf("Log(%.2f) is out of range\n", x); } else { printf("Log(%.2f) = %.2f\n", x, value); //2 getch(); //2 x = 0.00; value = log(x); if( errno == ERANGE ){ printf("Log(%.2f) is out of range\n", x); } else { printf("Log(%.2f) = %.2f\n", x, value);

13 รายละเอียดของส่วนประกอบที่ใช้แสดงผล
ไลบรารีไฟล์ float.h float.h ประกอบด้วยชุดของค่าคงที่ของแต่ละแพลตฟอร์มที่เกี่ยวข้องกับค่าจุดทศนิยม ซึ่งค่าคงที่เหล่านี้มี 4 องค์ประกอบหลัก ส่วนประกอบ รายละเอียดของส่วนประกอบที่ใช้แสดงผล S เครื่องหมาย ( +/- ) b เลขฐานหรือรากของการแทนเลขคณิต เลขฐาน 2 , 10, 16 e ตัวชี้กำลังในรูปจำนวนเต็มสูงสุด ต่ำสุด p ความแม่นยำของจำนวนเลขฐานที่สำคัญ

14 Macro ไลบรารีไฟล์ float.h
Description FLT_ROUNDS Defines the rounding mode for floating point addition and it can have any of the following values −-1 − indeterminable 0 − towards zero 1 − to nearest 2 − towards positive infinity 3 − towards negative infinity FLT_RADIX 2 This defines the base radix representation of the exponent. A base-2 is binary, base-10 is the normal decimal representation, base-16 is Hex. FLT_MANT_DIG DBL_MANT_DIG LDBL_MANT_DIG These macros define the number of digits in the number (in the FLT_RADIX base). FLT_DIG 6 DBL_DIG 10 LDBL_DIG 10 These macros define the maximum number decimal digits (base-10) that can be represented without change after rounding. FLT_MIN_EXP DBL_MIN_EXP LDBL_MIN_EXP These macros define the minimum negative integer value for an exponent in base FLT_RADIX. FLT_MIN_10_EXP -37 DBL_MIN_10_EXP -37 LDBL_MIN_10_EXP -37 These macros define the minimum negative integer value for an exponent in base 10. FLT_MAX_EXP DBL_MAX_EXP LDBL_MAX_EXP These macros define the maximum integer value for an exponent in base FLT_RADIX. FLT_MAX_10_EXP +37 DBL_MAX_10_EXP +37 LDBL_MAX_10_EXP +37 These macros define the maximum integer value for an exponent in base 10. FLT_MAX 1E+37 DBL_MAX 1E+37 LDBL_MAX 1E+37 These macros define the maximum finite floating-point value. FLT_EPSILON 1E-5 DBL_EPSILON 1E-9 LDBL_EPSILON 1E-9 These macros define the least significant digit representable. FLT_MIN 1E-37 DBL_MIN 1E-37 LDBL_MIN 1E-37 These macros define the minimum floating-point values.

15 ตัวอย่างการใช้ค่าคงที่ของ float.h
#include <stdio.h> #include <conio.h> #include <float.h> void main() { printf("The maximum value of float = %.10e\n", FLT_MAX); printf("The minimum value of float = %.10e\n", FLT_MIN); printf("The number of digits in the number = %.10e\n", FLT_MANT_DIG); getch(); }

16 Macro ไลบรารีไฟล์ limits.h
ใช้จำกัดค่าของตัวแปรต่างๆ เช่น char, int และ long Macro Value Description CHAR_BIT 8 Defines the number of bits in a byte. SCHAR_MIN -128 Defines the minimum value for a signed char. SCHAR_MAX +127 Defines the maximum value for a signed char. UCHAR_MAX 255 Defines the maximum value for an unsigned char. CHAR_MIN Defines the minimum value for type char and its value will be equal to SCHAR_MIN if char represents negative values, otherwise zero. CHAR_MAX Defines the value for type char and its value will be equal to SCHAR_MAX if char represents negative values, otherwise UCHAR_MAX. MB_LEN_MAX 16 Defines the maximum number of bytes in a multi-byte character. SHRT_MIN -32768 Defines the minimum value for a short int. SHRT_MAX +32767 Defines the maximum value for a short int. USHRT_MAX 65535 Defines the maximum value for an unsigned short int. INT_MIN Defines the minimum value for an int. INT_MAX Defines the maximum value for an int. UINT_MAX Defines the maximum value for an unsigned int. LONG_MIN Defines the minimum value for a long int. LONG_MAX Defines the maximum value for a long int. ULONG_MAX Defines the maximum value for an unsigned long int.

17 ตัวอย่างการใช้ค่าคงที่ในไฟล์ limits.h
#include <stdio.h> #include <conio.h> #include <limits.h> void main() { printf("The number of bits in a byte %d\n", CHAR_BIT); printf("The minimum value of SIGNED CHAR = %d\n", SCHAR_MIN); printf("The maximum value of SIGNED CHAR = %d\n", SCHAR_MAX); printf("The maximum value of UNSIGNED CHAR = %d\n", UCHAR_MAX); printf("The minimum value of SHORT INT = %d\n", SHRT_MIN); printf("The maximum value of SHORT INT = %d\n", SHRT_MAX); printf("The minimum value of INT = %d\n", INT_MIN); printf("The maximum value of INT = %d\n", INT_MAX); printf("The minimum value of CHAR = %d\n", CHAR_MIN); printf("The maximum value of CHAR = %d\n", CHAR_MAX); printf("The minimum value of LONG = %ld\n", LONG_MIN); printf("The maximum value of LONG = %ld\n", LONG_MAX); getch(); }

18 ไลบรารีไฟล์ locale.h locale.h ใช้กำหนดการตั้งค่าเฉพาะแต่ละสถานที่ เช่น รูปแบบ วันที่และสัญลักษณ์สกุลเงิน รูปแบบที่กำหนดไว้ได้หลากหลาย ความสำคัญของโครงสร้าง struct ผ่าน lconv ฟังก์ชั่นที่สำคัญคือ setlocale(LC_ALL, ""); localeconv();

19 ตัวอย่างการใช้ฟังก์ชัน setlocale()
#include <locale.h> #include <stdio.h> #include <conio.h> #include <time.h> void main () { time_t currtime; struct tm *timer; char buffer[80]; time( &currtime ); timer = localtime( &currtime ); printf("Locale is: %s\n", setlocale(LC_ALL, "")); strftime(buffer,80,"%c", timer ); printf("Date is: %s\n", buffer); getch(); }

20 โครงสร้าง lconv และฟังก์ชัน localeconv()
#include <locale.h> #include <stdio.h> #include <conio.h> void main () { struct lconv * lc; setlocale(LC_MONETARY, "it_IT"); lc = localeconv(); printf("Local Currency Symbol: %s\n",lc->currency_symbol); printf("International Currency Symbol: %s\n",lc->int_curr_symbol); setlocale(LC_MONETARY, "en_US"); setlocale(LC_MONETARY, "en_GB"); printf ("Local Currency Symbol: %s\n",lc->currency_symbol); printf ("International Currency Symbol: %s\n",lc->int_curr_symbol); printf("Decimal Point = %s\n", lc->decimal_point); getch(); } โครงสร้าง lconv และฟังก์ชัน localeconv() LANG Default value for unspecified categories LC_ALL If defined, override all locale categories LC_COLLATE The name of the locale for collation information LC_CTYPE The name of the locale for character classification LC_MONETARY The name of the locale for money related information LC_NUMERIC The name of the locale for numeric editing LC_TIME The name of the locale for date- and time-formatting information LC_MESSAGES The name of the locale for messages

21 ไลบรารีไฟล์ math.h math.hใช้สำหรับการคำนวณทางคณิตศาสตร์
ฟังก์ชันทั้งหมดใช้ double เป็นอาร์กิวเมนต์และการคืนค่ากลับ

22 ตัวอย่างการใช้ฟังก์ชัน floor ใน math.h
#include <stdio.h> #include <conio.h> #include <math.h> void main(){ float val1, val2, val3, val4; val1 = 1.6; val2 = 1.2; val3 = 2.8; val4 = 2.3; printf("Value1 = %.1lf\n", floor(val1)); printf("Value2 = %.1lf\n", floor(val2)); printf("Value3 = %.1lf\n", floor(val3)); printf("Value4 = %.1lf\n", floor(val4)); getch(); } acos(x)               asin(x)                atan(x) sin(x)                  cos(x)                 tan(x) sqrt(x)                 exp(x)                pow(x,y) log(x)                  log10(x)             ceil(x) floor(x)                fabs(x)

23 ไลบรารีไฟล์ setjmp.h setjmp.h ประกอบด้วยฟังก์ชัน setjmp() และ longjmp()  setjmp ใช้ชนิดตัวแปร jmp_buf เป็นอินพุตเพียงอย่างเดียวและจะคืนค่า 0 เมื่อเรียกใช้โดยตรง longjmp ใช้ชนิดตัวแปร jmp_buf และส่งกลับค่าผ่านอาร์กิวเมนต์ที่สอง

24 ตัวอย่างการใช้ไลบรารีไฟล์ setjmp.h
#include <stdio.h> #include <conio.h> #include <stdlib.h> #include <setjmp.h> void jmpfunction(jmp_buf env_buf); int main() { int val; jmp_buf env_buffer; val = setjmp( env_buffer ); if( val != 0 ) printf("Returned from a longjmp() with value = %s\n", val); exit(0); } printf("Jump function call\n"); jmpfunction( env_buffer ); getch(); void jmpfunction(jmp_buf env_buf) longjmp(env_buf,1); //return 1 ตัวอย่างการใช้ไลบรารีไฟล์ setjmp.h

25 ไลบรารีไฟล์ signal.h signal.h ถือเป็นการกำหนดชนิดของตัวแปร sig_atomic_t ประกอบด้วยสองฟังก์ชันและอีกหลายแมโครเพื่อจัดการกับสัญญาณที่แตกต่างรายงานระหว่างการดำเนินการของโปรแกรม #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <signal.h> void sighandler(int); int main() { signal(SIGINT, sighandler); while(1) { printf("Going to sleep for a second...\n"); sleep(1); } return(0); void sighandler(int signum) { printf("Caught signal %d, coming out...\n", signum); exit(1); }

26 ไลบรารีไฟล์ stdarg.h stdarg.h หัวกำหนดชนิดตัวแปร va_list และสามแมโครซึ่งสามารถนำมาใช้เพื่อให้ได้ข้อโต้แย้งในการทำงานเมื่อจำนวนของการขัดแย้งยังไม่ทราบจำนวนตัวแปรเช่นของการขัดแย้ง int sum(int num_args, ...) { int val = 0; va_list ap; int i; va_start(ap, num_args); for(i = 0; i < num_args; i++) val += va_arg(ap, int); } va_end(ap); return val; #include<stdarg.h> #include<stdio.h> int sum(int, ...); int main(void) { printf("Sum of 10, 20 and 30 = %d\n", sum(3, 10, 20, 30) ); printf("Sum of 4, 20, 25 and 30 = %d\n", sum(4, 4, 20, 25, 30) ); return 0; }

27 ไลบรารีไฟล์ stddef.h #include <stddef.h> #include <stdio.h> struct address { char name[50]; char street[50]; int phone; }; int main() printf("name offset = %d byte in address structure.\n", offsetof(struct address, name)); printf("street offset = %d byte in address structure.\n", offsetof(struct address, street)); printf("phone offset = %d byte in address structure.\n", offsetof(struct address, phone)); return(0); } ช่วยให้ตรวจสอบขนาดของข้อมูลที่ใช้ในการจัดเก็บในหน่วยความจำ

28 ไลบรารีไฟล์ stdio.h stdio.h กำหนดสามประเภทตัวแปรและฟังก์ชั่นต่าง ๆ สำหรับการป้อนข้อมูลที่มีประสิทธิภาพและการส่งออกผ่านไฟล์ S.N. Variable & Description 1 size_t This is the unsigned integral type and is the result of the sizeof keyword. 2 FILE This is an object type suitable for storing information for a file stream. 3 fpos_t This is an object type suitable for storing any position in a file.

29 ไลบรารีไฟล์ stdlib.h stdlib.h ประกอบด้วยตัวแปร แมโครและฟังก์ชันต่าง ๆ สำหรับการปฏิบัติหน้าที่โดยทั่วไป เช่น

30 ชนิดของตัวแปรสำหรับจัดการข้อความใน stdlib.h
S.N. Variable & Description 1 size_t This is the unsigned integral type and is the result of the sizeof keyword. 2 wchar_t This is an integer type of the size of a wide character constant. 3 div_t This is the structure returned by the div function. 4 ldiv_t This is the structure returned by the ldiv function.

31 แมโครหรือค่าคงที่ของไฟล์ stdlib.h
S.N. Macro & Description 1 NULL This macro is the value of a null pointer constant. 2 EXIT_FAILURE This is the value for the exit function to return in case of failure. 3 EXIT_SUCCESS This is the value for the exit function to return in case of success. 4 RAND_MAX This macro is the maximum value returned by the rand function. 5 MB_CUR_MAX This macro is the maximum number of bytes in a multi-byte character set which cannot be larger than MB_LEN_MAX.

32 ไลบรารีไฟล์ string.h string.h ส่วนจัดเก็บประเภทตัวแปรและฟังก์ชันสำหรับการจัดการชุดข้อมูลแบบเรียงแถวของตัวอักษร

33 ค่าคงที่และฟังก์ชันของไฟล์ string.h

34 ไลบรารีไฟล์ time.h time.h ประกอบด้วยตัวแปร แมโคร ฟังก์ชันสำหรับจัดการวันที่และเวลา SN ตัวแปรและคำอธิบาย 1 size_t เป็นชนิดของตัวแปรที่ได้จากการใช้ sizeof () 2 clock_t เป็นชนิดของตัวแปรที่เหมาะสำหรับการจัดเก็บเวลาในการประมวลผล 3 time_t เป็นชนิดของตัวแปรที่เหมาะสำหรับการจัดเก็บเวลาตามปฏิทิน 4 struct tm เป็นโครงสร้างที่ใช้ในการระบุวันที่และเวลา

35 ฟังก์ชันการทำงานในไฟล์ time.h
S.N. Function & Description 1 char *asctime(const struct tm *timeptr) Returns a pointer to a string which represents the day and time of the structure timeptr. 2 clock_t clock(void) Returns the processor clock time used since the beginning of an implementation defined era (normally the beginning of the program). 3 char *ctime(const time_t *timer) Returns a string representing the localtime based on the argument timer. 4 double difftime(time_t time1, time_t time2) Returns the difference of seconds between time1 and time2 (time1-time2). 5 struct tm *gmtime(const time_t *timer) The value of timer is broken up into the structure tm and expressed in Coordinated Universal Time (UTC) also known as Greenwich Mean Time (GMT). 6 struct tm *localtime(const time_t *timer) The value of timer is broken up into the structure tm and expressed in the local time zone. 7 time_t mktime(struct tm *timeptr) Converts the structure pointed to by timeptr into a time_t value according to the local time zone. 8 size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr) Formats the time represented in the structure timeptr according to the formatting rules defined in format and stored into str. 9 time_t time(time_t *timer) Calculates the current calender time and encodes it into time_t format.

36 การใช้งานตัวแปรและฟังก์ชันใน time.h
เวลาเป็นวินาที ถ้าไม่ใช่ค่าว่าง (NULL) ค่าที่ส่งกลับจะถูกเก็บไว้ในตัวแปร เวลาที่นำมาใช้เริ่มต้นที่ (00:00:00 UTC, January 1, 1970) #include <stdio.h> #include <conio.h> #include <time.h> void main() { time_t seconds; seconds = time(NULL); printf("Hours since January 1, 1970 = %ld\n", seconds/3600); getch(); }

37 สรุป ภาษาซีจัดเก็บไลบรารีมาตรฐานในไฟล์นามสกุล .h
ไลบรารีจัดเก็บกลุ่มของฟังก์ชันและค่าคงที่ในการประมวลผลข้อมูล การจัดเก็บไลบรารี ช่วยในการแยกและประกอบชิ้นส่วนของซอฟต์แวร์ เพิ่มความสามารถในการนำกลับมาใช้ ช่วยให้การจัดการฟังก์ชันพื้นฐานได้สะดวกและรวดเร็วขึ้น การเรียกใช้งานไลบรารีต่างๆ ใช้การ #include <*.h> เรียกใช้ตัวแปร มาโคร ค่าคงที่และฟังก์ชัน ที่กำหนดไว้ภายใน


ดาวน์โหลด ppt ไลบรารีมาตรฐานภาษา C อาจารย์สมเกียรติ ช่อเหมือน

งานนำเสนอที่คล้ายกัน


Ads by Google