长期更新琐碎易忘知识点。。。。。

1. 在C语言中对于小函数的宏定义是非常好的,因为好用,也可以屏蔽类型

#include <stdio.h>  
  
#define Swap(a,b)  {a = a ^ b; \  
                    b = a ^ b; \  
                    a = a ^ b; \  
                   }   
  
#define  averge(a,b) ((a&b)+((a^b)>>1))  
  
  
int main()  
{    
    int a = 4,b = 3;  
    Swap(a,b);  
    printf("%d %d\n",a,b);  
    printf("%d\n",averge(a,b));  
    return 0;  
}  


2. 在写简单的动态数组的时候,开辟的内存需要是32的整数倍。比如,我给31,10,15你要给我变为     32,而我给我33,56你要给我变为64,也就是变为32的整数倍

#define MODE_SIZE 32

static int  adjust_size(int size)  
{  
    size += (MODE_SIZE - 1);  
    size /= MODE_SIZE;  
    size *= MODE_SIZE;  
  
    return size;  
}  

3. C语言可以变红字

printf("\n\033[31m\033[1m I love caoyanqin\033[0m\n");

4. 把文件的信息读到buf中

 struct stat buf;
 stat("./ying.sh", &buf);
 printf("file size = %d\n", buf.st_size);

5. 读取时间信息

  struct timeval tv;
 
  gettimeofday(&tv,NULL);
 
  printf("%d\t%d\n",tv.tv_usec,tv.tv_sec);


 6. 关于define的高级用法

   (1) #  :可以把字符串当做参数,#符号用作一个预处理运算符,它可以把语言符号化成字符号     

   #define  tostring(x)   #x
      char *str = tostring(521123);

   (2) ## : 可以把两个整数连接起来,但是不能连接字符串(测试环境 centos 7.0 gcc 4.8.5)
   
         #define  cat(x,y)  x##y
          int   num  = cat(125,521);
          char  *str = cat("I", "loe ying"); // 错误 

关于宏##的使用注意一点
http://www.cnblogs.com/wb-DarkHorse/p/3588787.html


7.  宏定义

定义了__DEBUG就使用 ifdef里面,否则就是else中的DEBUG,什么都输出
#include <stdio.h>

#define __DEBUG

#ifdef __DEBUG
#define  DEBUG(format,...)  printf("File: "__FILE__", Line: %05d : "format"\n", \
                                  __LINE__, ##__VA_ARGS__)
#else
#define  DEBUG(format,...)
#endif



int main(int argc, char *argv[])
{
    //DEBUG();  // 出错, 不能传入空

    DEBUG("I love ying");

    DEBUG("I love ying  %d", 1314);

    DEBUG("I love ying, %s", "ying love me too");
    return 0;
}

8.转化字符串

#include <stdio.h>

static const char sep[256] = { [' ']=1, ['.']=1, ['-']=1, ['_']=1};                                                                                                                          
#define SEP(x,y) sep[*((unsigned char *)(x) + (y))]



int main()
{

    char str[]="a_BC.d-e FAAAWOAINI";
    char *p = NULL;
    printf("%s\n",str);
    for( p = str; *p ;  p++){
      if(SEP(p, 0)){
          printf("sss\n");
        *p = '-';
      }   

      if(*p >= 'A' && *p <= 'Z'){
        *p += 'a' - 'A';
      }   
    }   

    printf("%s\n",str);


    return 0;
}


9. C++ string转换C字符串

#include <iostream>
#include <stdio.h>

using namespace std;

int main()
{
    string s = "woainicaoyanqin";

    printf("%s\n",s.c_str());
    return 0;
}

10. 综合利用函数指针、宏定义

typedef void (*testFunc)(Test *, uint64_t, unsigned char *);

struct testInfo{
   testFunc   func;
   Test      *te;
   unsigned  char *md5;
   uint64_t   testRange;   
};


pthread_t *threads = new pthread_t[threadNum];

#define THREAD_TEST(func, te, range, threadNum, md5, info) \
do{ \
    info->func = func; \
	info->te   = te; \
	info->testRange = range; \
	info->md5 = md5; \
	int i = 0; \
	for(i = 0; i < threadNum; ++i){ \
	    pthread_create(&threads[i], NULL, threadInstance; info);\
	}\
	for(i = 0; i < threadNum; ++i){ \
	    pthread_join(threads[i], NULL); \
	}\
}while(0)


void *threadInstance(void *arg)
{
	struct testInfo *test = (struct testInfo*)arg;
	
	test->func(test->te, test->testRange, test->md5);
	
	return NULL;
}


// write test
void writeTest(Test *te, uint64_t testRange, unsigned char *md5)
{
	
}


// read test
void readTest(Test *te, uint64_t testRange, unsigned char *md5)
{
	
}


// 调用函数

THREAD_TEST(writeTest, &te, testRange, threadNum, md5, info);
THREAD_TEST(readTest, &te, testRange, threadNum, md5, info);

11. 字符串ip地址转化为uint32_t ip 整数

uint32_t str2ip(const char *ip)
{
    uint32_t re = 0;
    unsigned char tmp = 0;

    while(1){
      if(*ip != '\0' && *ip !='.'){
        tmp = tmp * 10 + *ip - '0';
      }else{
        re = (re << 8) + tmp;
        if(*ip == '\0'){
          break;
        }
        tmp = 0;
      }
      ip++;
    }

    return re;
}


12. uint32_t ip 整数转化为字符串ip地址

char *ip2str(char *str, uint32_t ip)
{
  unsigned char *c = (unsigned char *)&ip;
  sprintf(str, "%u.%u.%u.%u", c[3], c[2], c[1], c[0]);

  return str;
}

char *ip2str(char *str, uint32_t ip)
{
  unsigned char *c = (unsigned char *)&ip;
  char *ipstr = str;

  if(c[3] >= 100){
    *str++ = '0' + c[3]/100;
    *str++ = '0' + (c[3] / 10) % 10;
    *str++ = '0' + c[3] % 10;
  }else if(c[3] >= 10){
    *str++ = '0' + c[3] / 10;
    *str++ = '0' + c[3] % 10;
  }else{
    *str++ = '0' + c[3];
  }
  *str++ = '.';

  if(c[2] >= 100){
    *str++ = '0' + c[2]/100;
    *str++ = '0' + (c[2] / 10) % 10;
    *str++ = '0' + c[2] % 10;
  }else if(c[2] >= 10){
    *str++ = '0' + c[2] / 10;
    *str++ = '0' + c[2] % 10;
  }else{
    *str++ = '0' + c[2];
  }
  *str++ = '.';

  if(c[1] >= 100){
    *str++ = '0' + c[1]/100;
    *str++ = '0' + (c[1] / 10) % 10;
    *str++ = '0' + c[1] % 10;
  }else if(c[1] >= 10){
    *str++ = '0' + c[1] / 10;
    *str++ = '0' + c[1] % 10;
  }else{
    *str++ = '0' + c[1];
  }
  *str++ = '.';

  if(c[0] >= 100){
    *str++ = '0' + c[0]/100;
    *str++ = '0' + (c[0]/10) % 10;
    *str++ = '0' + c[0] % 10;
  }else if(c[0] > 10){
    *str++ = '0' + c[0] / 10;
    *str++ = '0' + c[0] % 10;
  }else{
    *str++ = '0' + c[0];
  }

  return ipstr;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值