小编mug*_*896的帖子

(i + 1)<ii和(i + 1)> ii两者都是真的吗?

我正在学习C程序但是我遇到了一些奇怪的行为,我期待一个结果,但是这样打印出两个结果

$ ./a.out
yes1 0x80000000
yes3 0x80000000
Run Code Online (Sandbox Code Playgroud)

怎么可能呢?
我无法理解结果.

OS : x86_64 Ubuntu Linux
C compiler : gcc (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1

gcc -O2 weird.c

#include <stdio.h>

int main() {

    int i = 0x7fffffff;
    int ii = 0x0000000f;

    if ((i + 1) < ii)
        printf ("yes1 %#x\n", i + 1);

    if ((i + 1) == ii)
        printf ("yes2 %#x\n", i + 1);

    if ((i + 1) > ii)
        printf ("yes3 %#x\n", i + 1);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c integer-overflow undefined-behavior

6
推荐指数
1
解决办法
252
查看次数

__typeof__ 中的“(void)0”有什么用?

我目前正在查看stdatomic.hubuntu linux中的头文件。和 中的逗号
有什么用? 这不需要什么吗?(void)0__typeof__ ((void)0, *__atomic_store_ptr)

#define atomic_store_explicit(PTR, VAL, MO)             \
  __extension__                             \
  ({                                    \
    __auto_type __atomic_store_ptr = (PTR);             \
    __typeof__ ((void)0, *__atomic_store_ptr) __atomic_store_tmp = (VAL);   \
    __atomic_store (__atomic_store_ptr, &__atomic_store_tmp, (MO)); \
  })
Run Code Online (Sandbox Code Playgroud)

c

5
推荐指数
1
解决办法
136
查看次数

为什么在where子句中存在特征绑定而在函数签名中没有?

我正在阅读HashMapget功能,但我K在源代码中找不到类型参数.

为什么K特征绑定存在于where子句中,而不存在于函数签名中?

pub fn get<Q: ?Sized>(&self, k: &Q) -> Option<&V>
    where K: Borrow<Q>,
          Q: Hash + Eq
{
    self.search(k).map(|bucket| bucket.into_refs().1)
}
Run Code Online (Sandbox Code Playgroud)

syntax rust

4
推荐指数
1
解决办法
125
查看次数

标签 统计

c ×2

integer-overflow ×1

rust ×1

syntax ×1

undefined-behavior ×1