assert、atexit、bsearch、tolower、offsetof及va_list
来源:互联网 发布:如何办淘宝卡 编辑:程序博客网 时间:2024/05/07 16:16
1.屏蔽assert函数
#define NDEBUG ("no debug") //shield the function of assert ... #include <assert.h>#include <stdlib.h>voidopen_record(char *record_name){ assert(record_name!=NULL); /* Rest of code */ printf("\nRecord name is:%s\n", record_name);}intmain(void){ open_record(NULL); open_record("I love janesnf..."); open_record(NULL); return 0;}
2. 调用atexit
#include <stdio.h>#include <stdlib.h>voidtest_atexit0(void){ printf("\nHosfore 000000 is using atexit function ...\n");}voidtest_atexit(void){ printf("\nHosfore is using atexit function ...\n");}intmain(){ int i = 0; atexit(test_atexit); atexit(test_atexit0); atexit(test_atexit); atexit(test_atexit0); for (; i < 100; i++) printf("\n----\n"); return 0;}
3.调用二分搜索函数,搜索从数组的平均值开始
#include <stdlib.h>#include <stdio.h>#include <assert.h>#define DEBUG 0intcompare(const void *key, const void *current){ assert(key != NULL); assert(current != NULL);#if(DEBUG) printf("\nIn %s and key val:%d, current val:%d\n",\ __func__, *(int*)key, *(int*)current);#endif if (*(int*)key > *(int*)current) return 1; else if (*(int*)key == *(int*)current) return 0; else return -1;}intmain(){ int key = 2, arr[10], i = 0, *rt; for (; i < 10; i++) arr[i] = 2+i;#if(DEBUG) printf("\nIn %s and sizeof arr is:%d, sizeof int is:%d\n",\ __func__, sizeof(arr)/sizeof(arr[0]), sizeof(int));#endif rt = bsearch(&key, arr, sizeof(arr)/sizeof(arr[0]), sizeof(int), compare); if (rt) printf("\nbsearch result of key:%d is:%d and memory addr is:%p\n",\ key, *rt, rt); return 0;}
4.大小写字母转换函数调用
#include<ctype.h>#include<stdio.h>#include<string.h>int main(void){ int loop; char string[]="THIS IS A TEST"; printf("Orig: %s\n",string); for(loop=0;loop<strlen(string);loop++) string[loop]=tolower(string[loop]); printf("Tolowered: %s\n",string); return 0;}
5.求结构体属性在其内部地址偏移函数offsetof
#include<stddef.h>#include<stdio.h>int main(void){ struct user{ char name[50]; char alias[50]; int level; }; printf("level is the %d byte in the user structure.\n", offsetof(struct user,level));}
6.支持不定参数个数的函数(源自网络)
#include<stdarg.h>#include<stdio.h>voidsum(char *, int, ...);intmain(void){ sum("The sum of 10+15+13 is %d.\n",3,10,15,13); return 0;}voidsum(char *string, int num_args, ...){ int sum=0; va_list ap; int loop; va_start(ap,num_args); for(loop=0;loop<num_args;loop++) sum+=va_arg(ap,int); printf(string,sum); va_end(ap);}
- assert、atexit、bsearch、tolower、offsetof及va_list
- tolower
- tolower
- tolower
- tolower
- tolower
- tolower()
- linux 出错处理: assert abort exit atexit
- bsearch
- bsearch
- va_list原理及用法
- va_list原理及用法
- va_list原理及用法
- va_list使用及原理
- va_list原理及用法
- va_list原理及用法
- va_list原理及用法
- va_list原理及用法
- 孤儿进程和僵尸进程[详解]
- xargs
- C#中修饰符使用
- Linux下C++的通用Makefile与解析
- 代码
- assert、atexit、bsearch、tolower、offsetof及va_list
- 黑马程序员 — 容器
- java中类的各个成员的执行顺序
- sencha touch之panel和tabpanel
- struts2 动态方法调用 感叹号 !
- 部分函数ORACLE
- (1)Mysql乱码问题
- xml换行符
- maven 程序包com.sun.image.codec.jpeg不存在的解决方案