正文

c细节啊2007-03-31 21:58:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/wfxsky/24462.html

分享到:

** 非常好的程序:(考虑程序的可移植性) #include <stdio.h>#include <limits.h>#include <stdlib.h> void PRINTBIT(int); /* 打印 n 的每个比特位 */ void PRINTBIT(int n){ /* 这里开始我定义为 int 型,结果如下:    2 : 0000 0000 0000 0000 0000 0000 0000 0011    4 : 0000 0000 0000 0000 0000 0000 0000 01112 ^ 4 : 0000 0000 0000 0000 0000 0000 0000 01112 & 4 : 0000 0000 0000 0000 0000 0000 0000 00002 | 4 : 0000 0000 0000 0000 0000 0000 0000 0111   最后改为unsigned int 则正确,仔细想了一下   原因:   check = 1;   check <<= (i - 1);   这样如果 check 为 int 则最高为永远为 1,且会   向右移动,移完后   check=bit(1111 1111 1111 1111 1111 1111 1111 1111); */ unsigned int j; unsigned int i = sizeof(int) * CHAR_BIT; unsigned int check = 1;  check <<= (i - 1); for(j = 1; j <= i; j ++){  putchar(((check & n) ? '1' : '0'));  check >>= 1;  if(!(j % 4))   putchar(' '); } putchar('\n');} int main(int argc,char *argv[]){ int n,m;  if(argc != 3){  fprintf(stderr,"Enter: pname int1 int2\n");  exit(EXIT_FAILURE); } n=atoi(argv[1]); m=atoi(argv[2]); printf("    %d : ",n); PRINTBIT(n); printf("    %d : ",m); PRINTBIT(m); printf("%d ^ %d : ",n,m); PRINTBIT(n^m); printf("%d & %d : ",n,m); PRINTBIT(n&m); printf("%d | %d : ",n,m); PRINTBIT(n|m); return EXIT_SUCCESS;} 2 ** 动态数组 :#include <stdio.h>#include <malloc.h> void out(int a[][2]){  printf("%d",a[0][0]); printf("%d",a[0][1]); printf("%d",a[1][0]); printf("%d",a[1][1]);} int main(){ int (*array)[4]; int *test,i;  array =(int (*)[4])malloc(sizeof(int[4]) * 2); for(i = 0; i < 4; i ++){  array[0][i] = i;  array[1][i] = i + 1; } test = (int*)malloc(sizeof(int)*4); for(i = 0; i < 4; i ++){  test[i] = i; } out(test); for(i = 0; i < 4; i ++)  printf("%d %d",array[0][i],array[1][i]); free(array); free(test); return 0;} 3 **  ANSI C 允许 int a[2,3,4]; 等价于 a[4] 4 **  长跳转 :#include <stdio.h>#include <setjmp.h> jmp_buf test; void reset(){ longjmp(test,1);} int main(){  int flag = 0;     if(setjmp(test))   printf("set jmp");  else   printf("\nreturn ");  if( !flag){   int t;  分程序变量   printf("\n%d\n",t); 分程序   flag = 1;   reset();  } return 0;} 5 ** 函数指针 :#include <stdio.h>#include <string.h> int max(int a,int b){  if( a > b )  return a; else  return b;} int test(int (*pf)(int ,int )){  return pf(8,10); //也可以这样调用(*pf)(8,10);} int main(){ int (*pf)(int a,int b);  pf = max; //ANSI C 认为 pf = &max 也合法 printf("%d",test(pf));  return 0;} 6 ** 数组类型做函数的结果类型:typedef float mv[100][100];typedef float vf[100]; vf* vsum(mf m){{ int i,j;     for(j = 0; j < 100; j ++)  for(i = 0; i < 99; i ++)  m[99][j] += m[i][j];  return (&m[99]);} 7 ** 环境通信函数exit atexitabortsystemqetenv #include <stdio.h>#include <stdlib.h> int main(){//直接在编译器中运行会不认识 ac.exe 但在 MS-DOS 或直接允许可执行文件 OK     system("ac.exe"); // 挂起当前程序,转入 ac.exe中 printf("\nreturn success !\n"); printf("yes !"); system("pause"); return 0;} 8 ** 为流设置缓冲区void setbuf(FILE *stream, char *buf) //buf 必须为 BUFSIZ(512)大小int setvbuf(FILE *fp, char *buf, int type, size_t size)// buf 未规定大小 9 ** 访问静态局部变量#include <stdio.h> int *p; void test(){ static int t = 2; p = &t; printf("%d\n",t);} int main(){  test(); printf("%d\n",*p); *p = 4; test(); return 0;} 10 **内存分配函数 #include <stdio.h>#include <malloc.h> int main(){ int *pc,*pm;  pc = (int*)calloc(1,sizeof(int)); //初始化分配的空间为 0 pm = (int*)malloc(sizeof(int));   //不初始化分配的空间 pc = (int*)realloc(pc,2*sizeof(int));  *(pc+1) = 67; printf("%d  %d\n%d",pc[0],pc[1],*pm); // 0 67 /n -3924902834 *pm 的值为不确定的数 free(pc); free(pm); return 0;} 11 **时间函数使用time()gmtime()ctime()localtime()mktime()strftime()clock()difftime()mktime()asctime() 12 **动态改变域宽或精度'*' 符号对应一个整数表示域宽,当该整数为负时表示左对齐输出.*  '*'表示精度#include <stdio.h> int main(){ int j = 8; printf("%.*8f",23.23); printf("please input field width:"); scanf("%u",&j); printf("\n%*s",j,"sdkf"); printf("%*s",j,"kdsf"); printf("\n%*s",j,"dkfkdfk"); printf("%*s\n",j,"dkfkdfk"); return 0;} 13 **文件操作fopen()  打开文件  fseek()  设置文件位置的指示符值 fclose()  关闭文件  fsetpos() 设置文件位置的指示符值 rename() 重命名文件  ftell()  取文件位置指示符指向remove() 删除文件  rewind() 重置文件位置指示符指向文件开始处tmpfile() 建立临时文件tmpnam() 生成文件名字fflush() 清除输出流的缓冲区freopen() 关闭并重新打开文件流setbuf() 改变流的缓冲区位置setvbuf() 改变流的缓冲区位置及大小fgetpos() 取文件位置指示符当前值#include <stdio.h> int main(){  rename("test.txt","deng.txt");//重命名test.txt remove("deng.txt");//删除deng.txt  return 0;} 14 **数学函数三角函数: 双曲函数: 指数对数: 幂函数: 取整取余函数:  绝对值函数:acos()  cosh()  exp()  pow()  ceil()   abs()asin()  sinh()  frexp()  sqrt()  floor()   fabs()atan()  tanh()  ldexp()    fmod()   labs()atan2()    log()cos()    log10()sin()    modf()tan() 15 **头文件assert.h 运行时断言检查  limits.h 一般实现的限制ctype.h  字符处理函数  locale.h 建立或修改局部环境errno.h  错误号及有关的宏 math.h  数学函数float.h  对浮点数的限制  setjmp.h 非局部跳转signal.h 信号处理函数  stdlib.h 通用实用程序stdarg.h 处理可变数目的变元 string.h 字符串处理函数stddef.h 公共定义  time.h  日期与时间函数stdio.h  标准输入输出 16 **位域位域只能用于 int ,unsigned int,并且只能用在结构体或联合中struct field{int i:4;unsigned u:8;};i 占四位,u占八位,如果不是必须用,就不别,费时 17 **信号函数signal() 设置信号处理函数 18 **字符串函数 char *strtok(char *s1, const char *s2)该函数将s1中首次出现s2中字符的位置置为NULL,因此会破坏s1字符串。该函数一般用于分解s1字符串为用特定分隔符分隔的多个字符串,s2一般设置为s1中的分隔字符,比如空格,逗号等,例如将一条自然英文语句分解为单词。 char *strerror(int errnum)错误提示信息字符串指针 char *strrchr(const char *s, int c)得到字符串s中最后一个含有c字符的位置指针 int strspn(const char *s1, const char *s2)得到s1中第一个同时也不是s2中任意字符的字符位置 char *strpbrk(const char *s1, const char *s2)得到s1中首次出现的也是s2中字符的位置指针 int strcspn(const char *s1, const char *s2)得到s1中第一个同时也是s2中字符的字符位置 19 **按位取反符号 ~ ~ 对" 整形类 " 变量的各个二进制位 0 变 1 ,1 变 0 转变时都提升同整形大小(按位 | 或,按位 & 也都要提升)按位运算符不改变原变量。( ** 不改变原数 ** ) 20 ** 宏 __TIME__ ,__DATE__,__FILE__,__LINE__ **<---- 未完待续 ----->

阅读(2215) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册