编程札记

C

C库函数 - qsort()

C库函数 - qsort() #include <stdio.h> #include <stdlib.h> int values[] = { 88, 56, 100, 2, 25 }; int cmpfunc (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int main () { int n; printf("Before sorting the list is: \n"); for( n = 0 ; n < 5; n++ ) { printf("%d ", values[n]); } qsort(values, 5, sizeof(int), cmpfunc); printf("\nAfter sorting the list is: \n"); for( n = 0 ; n < 5; n++ ) { printf("%d ", values[n]); } return(0); }

阅读全文 ↗
C

C库函数 - bsearch()

C库函数 - bsearch() #include <stdio.h> #include <stdlib.h> int cmpfunc(const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } int values[] = { 5, 20, 29, 32, 63 }; int main () { int *item; int key = 32; /* using bsearch() to find value 32 in the array */ item = (int*) bsearch (&key, values, 5, sizeof (int), cmpfunc); if( item != NULL ) { printf("Found item = %d\n", *item); } else { printf("Item = %d could not be found\n", *item); } return(0); }

阅读全文 ↗
C

C库函数 - getenv()

C库函数 - getenv() #include <stdio.h> #include <stdlib.h> int main () { printf("PATH : %s\n", getenv("PATH")); printf("HOME : %s\n", getenv("HOME")); printf("ROOT : %s\n", getenv("ROOT")); return(0); }

阅读全文 ↗
C

C库函数 - exit()

C库函数 - exit() #include <stdio.h> #include <stdlib.h> int main () { printf("Start of the program....\n"); printf("Exiting the program....\n"); exit(0); printf("End of the program....\n"); return(0); }

阅读全文 ↗
C

C库函数 - atexit()

C库函数 - atexit() #include <stdio.h> #include <stdlib.h> void functionA () { printf("This is functionA\n"); } int main () { /* register the termination function */ atexit(functionA ); printf("Starting main program...\n"); printf("Exiting main program...\n"); return(0); }

阅读全文 ↗
C

C库函数 - abort()

C库函数 - abort() #include <stdio.h> #include <stdlib.h> int main () { FILE *fp; printf("Going to open nofile.txt\n"); fp = fopen( "nofile.txt","r" ); if(fp == NULL) { printf("Going to abort the program\n"); abort(); } printf("Going to close nofile.txt\n"); fclose(fp); return(0); }

阅读全文 ↗
C

C库函数 - realloc()

C库函数 - realloc() #include <stdio.h> #include <stdlib.h> int main () { char *str; /* Initial memory allocation */ str = (char *) malloc(15); strcpy(str, "codingdict"); printf("String = %s, Address = %u\n", str, str); /* Reallocating memory */ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %u\n", str, str); free(str); return(0); }

阅读全文 ↗
C

C库函数 - free()

C库函数 - free() #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { char *str; /* Initial memory allocation */ str = (char *) malloc(15); strcpy(str, "codingdict"); printf("String = %s, Address = %u\n", str, str); /* Reallocating memory */ str = (char *) realloc(str, 25); strcat(str, ".com"); printf("String = %s, Address = %u\n", str, str); /* Deallocate allocated memory */ free(str); return(0); }

阅读全文 ↗
C

C库函数 - strtoul()

C库函数 - strtoul() #include <stdio.h> #include <stdlib.h> int main () { char str[30] = "2030300 This is test"; char *ptr; long ret; ret = strtoul(str, &ptr, 10); printf("The number(unsigned long integer) is %lu\n", ret); printf("String part is |%s|", ptr); return(0); }

阅读全文 ↗
C

C库函数 - strtol()

C库函数 - strtol() #include <stdio.h> #include <stdlib.h> int main () { char str[30] = "2030300 This is test"; char *ptr; long ret; ret = strtol(str, &ptr, 10); printf("The number(unsigned long integer) is %ld\n", ret); printf("String part is |%s|", ptr); return(0); }

阅读全文 ↗
C

C库函数 - strtod()

C库函数 - strtod() #include <stdio.h> #include <stdlib.h> int main () { char str[30] = "20.30300 This is test"; char *ptr; double ret; ret = strtod(str, &ptr); printf("The number(double) is %lf\n", ret); printf("String part is |%s|", ptr); return(0); }

阅读全文 ↗
C

C库函数 - atol()

C库函数 - atol() #include <stdio.h> #include <stdlib.h> #include <string.h> int main () { long val; char str[20]; strcpy(str, "98993489"); val = atol(str); printf("String value = %s, Long value = %ld\n", str, val); strcpy(str, "codingdict.com"); val = atol(str); printf("String value = %s, Long value = %ld\n", str, val); return(0); }

阅读全文 ↗