V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
thinkIn
V2EX  ›  C

在看 Linux 中 vsprintf() 源码时,发现似乎程序块也可以有值的?

  •  1
     
  •   thinkIn · Oct 13, 2015 · 2172 views
    This topic created in 3862 days ago, the information mentioned may be changed or developed.
    # define do_div(n, base) ({                     \
        unsigned int __base = (base);               \
        unsigned int __rem;                         \
        __rem = ((unsigned long long)(n)) % __base; \
        (n) = ((unsigned long long)(n)) / __base;   \
        __rem;                                      \
    })
    
    .......
    tmp[i++] = digits[do_div(num, base)];
    ...........
    

    do_div(n,base)的值是_rem?

    9 replies    2015-10-13 12:53:02 +08:00
    loveyu
        1
    loveyu  
       Oct 13, 2015
    这个只是宏定义而已,不是函数
    wizardoz
        2
    wizardoz  
       Oct 13, 2015
    可以的, C 语言的参考书中很容易找到说明。但是这种用法有点晦涩,而且感觉不是十分必要,所以我从来没有用过。
    47jm9ozp
        3
    47jm9ozp  
       Oct 13, 2015
    楼上的没仔细看……

    似乎返回的是最后一句的返回值?
    47jm9ozp
        4
    47jm9ozp  
       Oct 13, 2015
    楼上指一楼
    thinkIn
        5
    thinkIn  
    OP
       Oct 13, 2015
    找到资料了
    http://stackoverflow.com/questions/7117427/c-block-becomes-expression-int-a-1-int-b-2-ab-equals-3
    有评论说 ” This is a GCC extension called Statement Expressions. It's not standard C.“

    这不是标准 C 的语法。
    zonghua
        6
    zonghua  
       Oct 13, 2015
    @thinkIn unbelievable
    firemiles
        7
    firemiles  
       Oct 13, 2015
    还以为可以像 ruby 一样,所有语句都有返回值,这样就能用宏写一些函数了
    starp128
        8
    starp128  
       Oct 13, 2015
    词法分析时由程序块的对应的规则决定它的值。
    pright
        9
    pright  
       Oct 13, 2015
    嗯, GCC 的扩展语法,能实现出更像函数表现的宏
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3021 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 08:27 · PVG 16:27 · LAX 01:27 · JFK 04:27
    ♥ Do have faith in what you're doing.