C语言 不常见用法汇总(自整理,持续更新ing)

1.enum   FreeModbus中的mb.c

static enum 
{
    STATE_ENABLED,
    STATE_DISABLED,
    STATE_NOT_INITIALIZED
}eMBState = STATE_NOT_INITIALIZED;

 

2.Keil MDK 注释时,字符串前加+,则该字符串及@会变颜色。主要作用是方便查看。

 

3.为了编写的C库能同时被C++调用,在C库的头文件中使用:

#ifdef __cplusplus
extern "C" {
#endif

#ifdef __cplusplus
}
#endif

 

4.##                lwip memp.h中   

名称:预处理拼接符,或者称其为宏拼接符

作用:用于类似函数的宏的替换部分,还可以用于类似对象的宏的替换部分。

///  memp.h
/** Create the list of all memory pools managed by memp. MEMP_MAX represents a NULL pool at the end */
typedef enum {
#define LWIP_MEMPOOL(name, num, size, desc) MEMP_##name,
#include "lwip/priv/memp_std.h"
MEMP_MAX
}memp_t;

 

5.##__VA_ARGS__    C99编译器标准支持                                庆科  MiCO_A_v3.2.0

__VA_ARGS__的前面加上##是为了用来支持出现0个可变参数的情况。

缺省号代表一个可以变化的参数表。使用保留名__VA_ARGS__ 把参数传递给宏。当宏的调用展开后,实际的参数就传递给printf()了。

可变参数宏不被ANSI/ISO C++所正式支持

// 定义
#define custom_log(N, M, ...) do {printf("[%s: %s:%4d] " M "\r\n", N, SHORT_FILE, __LINE__, ##__VA_ARGS__);}while(0==1)
#define tcp_client_log(M,...)  custom_log("TCP", M, ##__VA_ARGS__)

// 使用
tcp_client_log("Client fd: %d, recv data %d", tcp_fd, len);

参考:

C语言 ## __VA_ARGS__ 宏
http://www.cnblogs.com/alexshi/archive/2012/03/09/2388453.html

 

6.指定数组项定义    庆科  MiCO_A_v3.2.0 

//  platform.c
static const platform_gpio_t internal_bt_control_pins[] = 
{
    /* Reset pin unavailable */
    [MICO_BT_PIN_POWER      ] = {GPIOC, 3};
    [MICO_BT_PIN_HOST_WAKE  ] = {GPIOC, 2};
    [MICO_BT_PIN_DEVICE_WAKE] = {GPIOC, 1};
};

 

7.  goto        庆科  MiCO_A_v3.2.0    这样写方便扩展代码,条理清楚便于理解。

// debug.h
#if (!defined( unlikely))
#define unlikely(EXPRESSSION)  !!(EXPRESSSION)
#endif

#if (!defined( require_noerr))
    #define require_noerr(ERR, LABEL)
    do
    {
        OSStatus localErr;
        localErr = (OSStatus) (ERR);
        if (unlikely(localErr  != 0))
        {
            ......
            goto LABEL; 
        }
    } while (1 == 0)
#endif

// tcp.client.c 
int application_start(void)
{
    OSStatus err = kNoErr;
    err = mico_system_notify_register(mico_notify_WIFI_STATUS_CHANGED,
                                        (void *) micoNotify_WifiStatusHandler, NULL);
    require_noerr(err, exit);
    .......

    exit:
    .......
    return err;
}

 

8. makefile  .PHONY

.PHONY后面的target表示的也是一个伪造的target, 而不是真实存在的文件target,注意Makefiletarget默认是文件。

.PHONY: clean

    o means the word "clean" doesn't represent a file name in this Makefile;

    o means the Makefile has nothing to do with a file called "clean"

      in the same directory.

参考:

Makefile中.PHONY的作用

https://www.cnblogs.com/idorax/p/9306528.html

 

9. makefile     $@  $^  $<  

给个例子你分析一下(来自国内开源项目g-bios
g-bios/app/net/Makefile
----------------------------------------------------------------------------------------------------------------------------------------
OBJS = ifconfig.o tftp_util.o ping.o
SRCS = $(OBJS:.o=.c)

PHONY += all
all : $(BUILT_IN_OBJ)

$(BUILT_IN_OBJ) : $(OBJS)
        $(LD) $(LDFLAGS) -r $^ -o $@

$(OBJS) : $(SRCS)
        $(CC) $(CFLAGS) -c $^

PHONY += clean
clean :
        @rm -vf *.o

.PHONY : $(PHONY)
----------------------------------------------------------------------------------------------------------------------------------------
说明:$(BUILT_IN_OBJ) 和 $(PHONY)来自上层目录g-bios/app/Makefile,可以不用理解。
$@ 代表目标 上例为$(BUILT_IN_OBJ)
$^ 代表所有的依赖对象 上例第一个$^为$(OBJS),即 ifconfig.o tftp_util.o ping.o,上例第二个$^为$(SRCS),即$(OBJS:.o=.c) (Makefile解释后为: ifconfig.c tftp_util.c ping.c)
$< 代表第一个依赖对象 前面例子修改如下:
$(BUILT_IN_OBJ) : $(OBJS)
        $(LD) $(LDFLAGS) -r $< -o $@
则$<表示为ifconfig.o

参考:

Makefile 中$@  $^  $<  解释 

http://bbs.chinaunix.net/thread-1596089-1-1.html

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值