当前位置: 主页 > 建站知识 > 软件开发

软件开发c语言-typedef字节序网络或通信的数据结构(const)总结

发布时间:2023-06-09 08:46   浏览次数:次   作者:佚名

平时主要还是C语言用的比较多软件开发c语言,对C语言做一个总结吧软件开发c语言,最基本的就不写了,把一下觉得重要的点总结一下吧。

const

最好能习惯性地给只读的参数加上const修饰符,方便理解参数用途,如以下声明:

  void strcpy(char *dst, const char *src);

typedef

typedef struct Item_st
{
    char name[20];
    int value;
}Item_t;

typedef void (*Callback_t)(void);
void Start(Callback cb);

字节序

网络或通信的数据要注意字节序的问题。

常用的C标准库

位运算(|, &, ^)

printf

多用printf输出日志调试,一般我会在工程里定义一个Log宏

#define LOG(...) printf("%s::%s[%d]", __FILE__, __func__, __LINE__);printf(__VA_ARGS__);printf("\n")

单片机相关常用数据结构

这里说说常用的数据结构,主要还是静态循环队列和链表两种。

具体的这里封装了两个头文件(es_fifo.h, es_list.h),可以看一下。

es_fifo.h

#ifndef __ES_FIFO_H
#define __ES_FIFO_H
#define ES_FIFO(name) (name)
#define _ES_FIFO_SIZE(fifo) (sizeof(ES_FIFO(fifo).items) / sizeof(ES_FIFO(fifo).items[0]))
#define es_fifo_def(type, fifo, size) \
struct fifo##_st\
{ \
unsigned short front, back, count; \
type items[size]; \
}ES_FIFO(fifo)
#define es_fifo_in(fifo, item) \
do{ \
if(es_fifo_has_space(fifo)) \
{ \
ES_FIFO(fifo).items[ES_FIFO(fifo).back] = item; \
ES_FIFO(fifo).back = ES_FIFO(fifo).back + 1; \
ES_FIFO(fifo).back = ES_FIFO(fifo).back == _ES_FIFO_SIZE(fifo) ? 0 : ES_FIFO(fifo).back; \
ES_FIFO(fifo).count++; \
} \
}while(0)
#define es_fifo_has_space(fifo) (ES_FIFO(fifo).count < _ES_FIFO_SIZE(fifo))
#define es_fifo_is_empty(fifo) (ES_FIFO(fifo).count == 0)
#define es_fifo_count(fifo) ES_FIFO(fifo).count
#define es_fifo_peek(fifo) ES_FIFO(fifo).items[ES_FIFO(fifo).front]
#define es_fifo_out(fifo) \
do{ \
if(!es_fifo_is_empty(fifo)) \
{ \
    ES_FIFO(fifo).front = (ES_FIFO(fifo).front + 1); \
    ES_FIFO(fifo).front = ES_FIFO(fifo).front == _ES_FIFO_SIZE(fifo) ? 0 : ES_FIFO(fifo).front; \
    ES_FIFO(fifo).count--; \
} \
}while(0)
#endif // __ES_FIFO_H

es_list.h

#ifndef __ES_LIST_H
#define __ES_LIST_H
#define ES_LIST_ENTRY(type) \
type *next;type *prev
#define es_list_first(list) ((list) ? (list) : NULL)
#define es_list_last(list) ((list) ? (list)->prev : NULL)
#define es_list_add(list, node) \
if(!list) { \
    list = node; \
    list->next = list; \
    list->prev = list; \
} \
else { \
    node->next = list; \
    list->prev->next = node; \
    node->prev = list->prev; \
    list->prev = node; \
}
#define es_list_del(list, node) \
{ \
    list = node == list ? (node->next == list ? NULL : node->next) : list; \
    (node)->prev->next = (node)->next; \
    (node)->next->prev = (node)->prev; \
} 
#define ___ESLISTV(node, line) node##line
#define __ESLISTV(node, line) ___ESLISTV(node, line)
#define _ESLISTV(node) __ESLISTV(node, __LINE__)
#define es_list_foreach(list, node) \
node = list; \
void *_ESLISTV(_next) = node ? node->next : NULL; \
void *_ESLISTV(_flag) = NULL; \
void *_ESLISTV(_list) = list; \
for(; list && (node != (list) \
    || _ESLISTV(_flag) == NULL); \
        node = _ESLISTV(_next), \
        _ESLISTV(_next) = node->next, \
        _ESLISTV(_flag) = _ESLISTV(_list) != list ? NULL : list, _ESLISTV(_list) = list)
#endif // __ES_LIST_H